# Image Switch Widget

**General Setting**

User can change following things in Image Switch widget:

* Name for widget which will be used data referenceFor 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 Image Switch Widget](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2F33AvvMj84xqRKMZModvO%2FimgSwitchGeneral.png?alt=media\&token=a083b2ab-a216-4541-a1ed-6d4c58f14b32)

#### 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.
* Switch Style - Select one from available Switch styles

![Configure Image Switch Widget](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FPMjwuSzakkZH2riJNxHI%2FimgSwitchWidget.png?alt=media\&token=0cf02645-1690-408a-8199-b129a941dec8)

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.
```

#### Source <a href="#source" id="source"></a>

User can select a source mode as MQTT or Action.

**Using MQTT**

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

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

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

![Configure Image Switch Widget](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FCLXczEBXPZOzo8eXckNY%2FmqttSource.png?alt=media\&token=0a2090f3-6cd5-401b-8502-2ab19a75b1a7)

**Using Action**

Here, to change state of Image 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 Image Switch Widget](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2F5uJ2sdzhV4qL2TBZWZ8o%2FactionSource.png?alt=media\&token=945da8cc-ca5c-4e91-aa55-16c8ce6147b7)

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

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

**Using MQTT**

User needs to provide following parameters:

* event - Select **OnClick** 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 the user clicks the button, it will publish configured data parameters on given MQTT topic.​

![Image Switch Widget - Event Handling using MQTT](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FCRvwbBkAnr7VEx9athQu%2FmqttEvent1.png?alt=media\&token=ff7f39cf-d276-48f3-b111-499f743dcd80)

![Image Switch Widget - Event Handling using MQTT](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FR61RbeJfCpRse36Z702I%2FmqttEvent2.png?alt=media\&token=21b41f61-0696-4296-a92a-e83988fa1a0a)

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

![Image Switch Widget - Event Handling using MQTT](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FV8hvVtiMkp6zLVDpH6CU%2FmqttEvent3.png?alt=media\&token=73aa9be5-7aa9-4b2a-9a07-75d83e6bcc96)

**Using Action**

User needs to provide following parameters:

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

As shown in below images, for onClick event, **action** mode is selected. It means when user clicks on the button, it will trigger given Action with configured action parameters.

![Image Switch Widget - Event Handling using Action](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FQCgncIoAw0ebYPJE9XkV%2FactionEvent.png?alt=media\&token=2a29ed44-a7d8-43e9-a5b4-18fd24a0a67c)

![Image Switch Widget - Event Handling using Action](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FKiM7DgK2APkPfAHTcsrW%2FactionEvent2.png?alt=media\&token=df5856c7-b7ab-4f92-b5aa-31fc17b4c173)

#### Example

If we configure Image Switch Widget as below:

1. key - imgKey

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

```
// Data over configured MQTT Topic

{ 
    "imgKey":true/1
}

OR

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

![Enabled Image Switch Widget](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FF1CPDzZIwGxF3elDumMc%2FScreenshot%202022-01-15%20at%205.16.48%20PM.png?alt=media\&token=5116965b-f25a-448f-a18b-f6a4848e364d) ![Disabled Image Switch Widget](https://365492316-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7aTljSjWsfbXZ_OP_9%2Fuploads%2FwQ6vPhgY6kZ3MV1ahdRj%2FScreenshot%202022-01-15%20at%205.16.58%20PM.png?alt=media\&token=685c68a8-b969-46dd-b220-96ddeccdb5f6)
