# Toggle Switch Widget

**General Setting**

User can change following things in Toggle Switch widget:

* Name for widget which will be used data reference&#x20;

  For Example, if widget name is 'Switch', then data of widget is referenced as Switch.data
* Show title - User can show/hide widget title
* Title Color
* Background Color

![Configure Toggle Switch Widget](/files/17SPKNGbraC02uRTUXgm)

#### Widget Setting <a href="#widget-setting" id="widget-setting"></a>

In Widget Setting option, user can change following parameters:

* Key - key parameter of JSON Object which will define state of switch.

User can manage state of switch using MQTT or Actions. To change state of switch, user need to send data in following format.

```
{"imageKey":true/false}   
OR
{"imageKey":1/0} 

// 'imageKey' is set as key in widget form.
```

![Configure Toggle Switch Widget](/files/7GMMkTDgO4RGNK9cSGee)

#### Source

User can select a source mode as MQTT or Action.

**Using MQTT**

Here, to change state of switch user can publish JSON object on given **imageData** MQTT topic. The JSON object will be as below. No need to provide \<account\_id> while providing the MQTT topic.

```
{"imageKey":true/false}   
OR
{"imageKey":1/0} 

// 'imageKey' is set as key in widget form.
```

![Configure Toggle Switch Widget](/files/tLlcNwk2DsKHYgVgi5oA)

**Using Action**

Here, to change state of switch, output of the action should contain key-value pair with 'imageKey' key. User can also update the widget either onLoad or periodically by providing refresh interval.

![Configure Toggle Switch Widget](/files/LsjeyJtDhSBRgxROkpmV)

#### Event Handling <a href="#event-handling" id="event-handling"></a>

Button onChange event is provided. User can choose MQTT or Action mode for onChange event.

**Using MQTT**&#x20;

User needs to provide following parameters:

* event - Select **onChange** event from dropdown
* mode - Select mode as **MQTT**
* topic - Enter topic name directly. No need to provide accountID.
* Data parameters - Add new data parameter item. Each data parameter item represent a key-value pair. So select required parameter and give key name to it.

As shown in below images, for onClick event, MQTT mode is selected. It means when state of switch changes, it will publish configured data parameters on given MQTT topic.

![Toggle Switch Widget - Event handling using MQTT](/files/8ItFnQEHyXRJTrGNJxug)

![Toggle Switch Widget - Event handling using MQTT](/files/Ay6TaWJqImDAEtTjgQP0)

So, as per the configuration shown in above images, when button state changes, JSON object with **imgKey-UserRole** as key-value pair gets published on **buttonData** topic. Please refer below image for final output.

![Toggle Switch Widget - Event handling using MQTT](/files/XSBohFka12hFriNPDRtj)

**Using Action**

User needs to provide following parameters:

* event - Select **onChange** event from dropdown
* mode - Select mode as **action**
* Action ID - Select required Action ID from dropdown

As shown in below images, for onChange event, **action** mode is selected. It means when switch state changes, it will trigger given Action with configured action parameters.​

![oggle Switch Widget - Event handling using Actions](/files/OOLuiIFNHDBeS8Kp0Gs4)

![Toggle Switch Widget - Event handling using Actions](/files/5CuIv4bAf8HLNYmP0zJA)

#### Example

If we configure Toggle Switch Widget as below:

1. key - toggleKey

and set source type for widget as MQTT and topic configured is 'data'. And if we publish following JSON Object over 'data' topic then Toggle Switch Widget will look as shown in image.

```
// Data over configured MQTT Topic

{ 
    "toggleKey":true/1 
}

OR

{ 
    "toggleKey":false/0
}
```

![Enabled Toggle Switch](/files/WOR7CXj4PldSpXHqTrPQ) ![Disabled Toggle S](/files/7A4sGOy2Ty4W98kYua7W)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.thingsup.io/widgets/toggle-switch-widget.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
