# MQTT Toggle, to change ZWAVE Bulb state

**URL:** <https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618>\
**Category:** General\
**Created:** [4 March 2020 14:14 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618 "2020-03-04T14:14:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [4 March 2020 14:14 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/1 "2020-03-04T14:14:32Z")

</div>

I am pretty new in Node-Red, I'm driven here because Home Assistant's automation is slow.

I made my first flow! and want to make sure I didn't ignore some basics of Node Red and didn't overcomplicate

Scenario:  
I have an Aeotec nanomote. This is a 4 keys button switch. I want to enable with One switch to toggle some devices (without using Zwave associatons).

Nanomote uses Central Scenes. When you press a button the MQTT payload receives for one second the Value "Pressed" and after a bit Updates the topic with value "Inactive"  
This is my Trigger state

I want to achieve the following:

- If bulb which is another MQTT device is on, turn it off...
- If bulb is off, turn it on.

My flow is not EndState one, I miss sending the state update to the MQTT Topic. But I do debug it using this way.

 ![Screenshot 2020-03-04 at 15.13.03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/62599c44785255af39a92e00c0b0a5402fff78f1.png)  
My flow:

```auto
[
  {
    "id": "74fe1247.6a93cc",
    "type": "switch",
    "z": "6f8f89ff.9374a8",
    "name": "",
    "property": "payload.value",
    "propertyType": "jsonata",
    "rules": [
      {
        "t": "eq",
        "v": "Pressed",
        "vt": "str"
      }
    ],
    "checkall": "true",
    "repair": true,
    "outputs": 1,
    "x": 570,
    "y": 180,
    "wires": [
      [
        "c0d20793.f78738"
      ]
    ]
  },
  {
    "id": "c0d20793.f78738",
    "type": "switch",
    "z": "6f8f89ff.9374a8",
    "name": "",
    "property": "lightState",
    "propertyType": "flow",
    "rules": [
      {
        "t": "eq",
        "v": "Off",
        "vt": "str"
      },
      {
        "t": "eq",
        "v": "On",
        "vt": "str"
      }
    ],
    "checkall": "true",
    "repair": false,
    "outputs": 2,
    "x": 750,
    "y": 180,
    "wires": [
      [
        "c6b492d.4d5a57"
      ],
      [
        "416e38dc.c14b18"
      ]
    ]
  },
  {
    "id": "cd33b334.98644",
    "type": "debug",
    "z": "6f8f89ff.9374a8",
    "name": "Debug end state",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "payload",
    "targetType": "msg",
    "x": 1270,
    "y": 180,
    "wires": []
  },
  {
    "id": "c6b492d.4d5a57",
    "type": "change",
    "z": "6f8f89ff.9374a8",
    "name": "Is OFF set ON",
    "rules": [
      {
        "t": "set",
        "p": "payload",
        "pt": "msg",
        "to": "Is OFF, set ON",
        "tot": "str"
      }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 940,
    "y": 140,
    "wires": [
      [
        "cd33b334.98644",
        "6dd10397.87ef5c"
      ]
    ]
  },
  {
    "id": "416e38dc.c14b18",
    "type": "change",
    "z": "6f8f89ff.9374a8",
    "name": "Is On, Set OFF",
    "rules": [
      {
        "t": "set",
        "p": "payload",
        "pt": "msg",
        "to": "Is ON, Set to OFF",
        "tot": "str"
      }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 940,
    "y": 220,
    "wires": [
      [
        "cd33b334.98644",
        "f97ecdbc.71d4a"
      ]
    ]
  },
  {
    "id": "95676085.92d26",
    "type": "mqtt in",
    "z": "6f8f89ff.9374a8",
    "name": "",
    "topic": "zwave2mqtt/MasterBedroom/VassilisSwitch/91/1/4",
    "qos": "2",
    "datatype": "json",
    "broker": "96bab34d.d97b5",
    "x": 270,
    "y": 180,
    "wires": [
      [
        "74fe1247.6a93cc"
      ]
    ]
  },
  {
    "id": "e0e11546.5730d8",
    "type": "inject",
    "z": "6f8f89ff.9374a8",
    "name": "send Pressed State",
    "topic": "",
    "payload": "{\"value_id\":\"60-91-1-1\",\"node_id\":60,\"class_id\":91,\"type\":\"list\",\"genre\":\"user\",\"instance\":1,\"index\":1,\"label\":\"Scene 1\",\"units\":\"\",\"help\":\"\",\"read_only\":true,\"write_only\":false,\"min\":0,\"max\":0,\"is_polled\":false,\"values\":[\"Inactive\",\"Pressed 1 Time\",\"Key Released\",\"Key Held down\"],\"value\":\"Pressed\",\"lastUpdate\":1583079850053}",
    "payloadType": "json",
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 370,
    "y": 260,
    "wires": [
      [
        "74fe1247.6a93cc"
      ]
    ]
  },
  {
    "id": "6dd10397.87ef5c",
    "type": "change",
    "z": "6f8f89ff.9374a8",
    "name": "Set flow.state",
    "rules": [
      {
        "t": "set",
        "p": "lightState",
        "pt": "flow",
        "to": "On",
        "tot": "str"
      }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 1180,
    "y": 120,
    "wires": [
      []
    ]
  },
  {
    "id": "f97ecdbc.71d4a",
    "type": "change",
    "z": "6f8f89ff.9374a8",
    "name": "Set flow.state",
    "rules": [
      {
        "t": "set",
        "p": "lightState",
        "pt": "flow",
        "to": "Off",
        "tot": "str"
      }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 1180,
    "y": 260,
    "wires": [
      []
    ]
  },
  {
    "id": "96bab34d.d97b5",
    "type": "mqtt-broker",
    "z": "",
    "name": "Main MQTT",
    "broker": "mqtt",
    "port": "1883",
    "clientid": "",
    "usetls": false,
    "compatmode": false,
    "keepalive": "60",
    "cleansession": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthPayload": "",
    "closeTopic": "",
    "closeQos": "0",
    "closePayload": "",
    "willTopic": "",
    "willQos": "0",
    "willPayload": ""
  }
]

```

I will in the end replace the end flow state with the MQTT Target.

I also have another Flow which updates flow.lightState using an mqtt subscription on the Bulb Endpoint.

Thanks,  
V

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 March 2020 14:59 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/2 "2020-03-04T14:59:56Z")

</div>

The reason why you don't get an output is because `flow.lightState` has not been set, thus it is neither `On` or `Off`, no message will be passed.

There are many possibilities for this situation

```auto
[{"id":"4058842f.e81794","type":"switch","z":"97b82393.6aa3d8","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"Pressed","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":458,"y":264,"wires":[["5e4ceec3.e65f28"]]},{"id":"5e4ceec3.e65f28","type":"switch","z":"97b82393.6aa3d8","name":"","property":"lightState","propertyType":"flow","rules":[{"t":"eq","v":"Off","vt":"str"},{"t":"eq","v":"On","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":578,"y":264,"wires":[["a0cb69c8.d3f368"],["bf5ba5a9.765cb"],["a0cb69c8.d3f368"]]},{"id":"4692754c.43c71c","type":"debug","z":"97b82393.6aa3d8","name":"Debug end state","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1122,"y":240,"wires":[]},{"id":"a0cb69c8.d3f368","type":"change","z":"97b82393.6aa3d8","name":"Is OFF set ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"Is OFF, set ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":752,"y":216,"wires":[["4692754c.43c71c","74726e1a.588d18"]]},{"id":"bf5ba5a9.765cb","type":"change","z":"97b82393.6aa3d8","name":"Is On, Set OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"Is ON, Set to OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":752,"y":264,"wires":[["4692754c.43c71c","bb812f0d.d10778"]]},{"id":"cfb1f98b.3695","type":"inject","z":"97b82393.6aa3d8","name":"send Pressed State","topic":"","payload":"{\"value_id\":\"60-91-1-1\",\"node_id\":60,\"class_id\":91,\"type\":\"list\",\"genre\":\"user\",\"instance\":1,\"index\":1,\"label\":\"Scene 1\",\"units\":\"\",\"help\":\"\",\"read_only\":true,\"write_only\":false,\"min\":0,\"max\":0,\"is_polled\":false,\"values\":[\"Inactive\",\"Pressed 1 Time\",\"Key Released\",\"Key Held down\"],\"value\":\"Pressed\",\"lastUpdate\":1583079850053}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":282,"y":264,"wires":[["4058842f.e81794"]]},{"id":"74726e1a.588d18","type":"change","z":"97b82393.6aa3d8","name":"Set flow.state","rules":[{"t":"set","p":"lightState","pt":"flow","to":"On","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":992,"y":168,"wires":[[]]},{"id":"bb812f0d.d10778","type":"change","z":"97b82393.6aa3d8","name":"Set flow.state","rules":[{"t":"set","p":"lightState","pt":"flow","to":"Off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":992,"y":312,"wires":[[]]}]

```

Just add an `otherwise` to the switch node.  
Or add an inject node that will set the initial flow value.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 March 2020 15:01 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/3 "2020-03-04T15:01:56Z")

</div>

Not looked through the flow in detail but it seems rather complex for a simple toggle.

The on/off processing should just be something like:

```auto
receive btn-pressed topic -> change node: ! current variable -> output & update variable

```

Where `! current variable` is flipping the state variable as a boolean.

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [4 March 2020 20:09 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/4 "2020-03-04T20:09:07Z")

</div>

@bakman2 It works, I have a second part in flow which injects the initial switch (faking a device) and will replace it with an MQTT Subscription.  
The question was more on the flow itself.

The button has 4 states reported on MQTT: Pressed, Long Press, Released, Inactive  
Short press triggers MQTT Pressed and after a second Inactive  
Where Long press: Long Press, on release: Released and after a second Inactive

I want to trigger sending an ON/OFF based on ANOTHER MQTT topic state  
which has two states: On/OFF (true false) and change by publishing the target state.

Making my device Flip based on current state, this is the simplest flow I can think  
On this flow I would remove

1. Debug
2. Send Pressed state  
and replace the set flow.state with the zwave target topic

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [4 March 2020 20:20 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/5 "2020-03-04T20:20:59Z")

</div>

Can you give an example of how to flip this? I might doing something wrong

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 March 2020 23:15 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/6 "2020-03-04T23:15:21Z")

</div>

It is not clear to me what you are trying to achieve.

You talk about "Pressed, Long Press, Released, Inactive, Short press triggers MQTT Pressed and after a second Inactive", but then you trigger ON/OFF from somewhere else.

Still it is as TotallyInformation indicated, simply a toggle. You don't need a flow variable for that. (note that you can use link nodes to link over tabs).

Try this flow

```auto
[{"id":"8d172071.967088","type":"inject","z":"feac9826.d15078","name":"Pressed","topic":"","payload":"{\"value_id\":\"60-91-1-1\",\"node_id\":60,\"class_id\":91,\"type\":\"list\",\"genre\":\"user\",\"instance\":1,\"index\":1,\"label\":\"Scene 1\",\"units\":\"\",\"help\":\"\",\"read_only\":true,\"write_only\":false,\"min\":0,\"max\":0,\"is_polled\":false,\"values\":[\"Inactive\",\"Pressed 1 Time\",\"Key Released\",\"Key Held down\"],\"value\":\"Pressed\",\"lastUpdate\":1583079850053}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":132,"y":240,"wires":[["ea140426.77b01"]]},{"id":"b437f4c2.a9b518","type":"debug","z":"feac9826.d15078","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":578,"y":240,"wires":[]},{"id":"a0b6f874.7b7408","type":"change","z":"feac9826.d15078","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":306,"y":120,"wires":[[]]},{"id":"c97e9ebc.95e058","type":"inject","z":"feac9826.d15078","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":122,"y":96,"wires":[["a0b6f874.7b7408"]]},{"id":"ea140426.77b01","type":"switch","z":"feac9826.d15078","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"Pressed","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":266,"y":240,"wires":[["2db0d950.6c9b26"]]},{"id":"2db0d950.6c9b26","type":"change","z":"feac9826.d15078","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"$flowContext(\"lightState\") =\"ON\" ? \"OFF\" : \"ON\"\t","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"destination_target","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"lightState\") ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":416,"y":240,"wires":[["b437f4c2.a9b518"]]},{"id":"1d707202.4db556","type":"inject","z":"feac9826.d15078","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":122,"y":144,"wires":[["a0b6f874.7b7408"]]}]

```

It will send a toggled ON/OFF with a topic and sets the flow var

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 March 2020 23:15 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/7 "2020-03-04T23:15:56Z")

</div>

In a function node:

For a global variable:

```auto
var test1 = global.get('switchvar')
test1 = ! test1
global.set('switchvar', test1)

```

Easily adjusted for a msg property.

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 11:37 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/8 "2020-03-05T11:37:00Z")

</div>

> [@bakman2](#):
>
> ```auto
> [{"id":"8d172071.967088","type":"inject","z":"feac9826.d15078","name":"Pressed","topic":"","payload":"{\"value_id\":\"60-91-1-1\",\"node_id\":60,\"class_id\":91,\"type\":\"list\",\"genre\":\"user\",\"instance\":1,\"index\":1,\"label\":\"Scene 1\",\"units\":\"\",\"help\":\"\",\"read_only\":true,\"write_only\":false,\"min\":0,\"max\":0,\"is_polled\":false,\"values\":[\"Inactive\",\"Pressed 1 Time\",\"Key Released\",\"Key Held down\"],\"value\":\"Pressed\",\"lastUpdate\":1583079850053}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":132,"y":240,"wires":[["ea140426.77b01"]]},{"id":"b437f4c2.a9b518","type":"debug","z":"feac9826.d15078","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":578,"y":240,"wires":[]},{"id":"a0b6f874.7b7408","type":"change","z":"feac9826.d15078","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":306,"y":120,"wires":[[]]},{"id":"c97e9ebc.95e058","type":"inject","z":"feac9826.d15078","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":122,"y":96,"wires":[["a0b6f874.7b7408"]]},{"id":"ea140426.77b01","type":"switch","z":"feac9826.d15078","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"Pressed","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":266,"y":240,"wires":[["2db0d950.6c9b26"]]},{"id":"2db0d950.6c9b26","type":"change","z":"feac9826.d15078","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"$flowContext(\"lightState\") =\"ON\" ? \"OFF\" : \"ON\"\t","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"destination_target","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"lightState\") ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":416,"y":240,"wires":[["b437f4c2.a9b518"]]},{"id":"1d707202.4db556","type":"inject","z":"feac9826.d15078","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":122,"y":144,"wires":[["a0b6f874.7b7408"]]}]
> 
> ```

I've done most of it as you mentioned above. The only bit is i didn't know i can have ternary expression.  
So node.js basics apply here too?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 March 2020 11:40 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/9 "2020-03-05T11:40:58Z")

</div>

> So node.js basics apply here too?

This is not javascript, but jsonata

> **[Programming constructs · JSONata](https://docs.jsonata.org/programming#conditional-logic)**
>
> So far, we have introduced all the parts of the language that allow us to extract data from an input JSON document, combine the data using string and numeric operators, and format the structure of the output JSON document. What follows are the parts...

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 11:48 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/10 "2020-03-05T11:48:06Z")

</div>

cool will read more

this is my final with your help. I think pretty Awesome and simplified

```auto
[{"id":"74fe1247.6a93cc","type":"switch","z":"6f8f89ff.9374a8","name":"Pressed 1 Time","property":"payload.value","propertyType":"jsonata","rules":[{"t":"eq","v":"Pressed 1 Time","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":360,"y":180,"wires":[["3882b615.0288ba"]]}]

```

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 11:48 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/11 "2020-03-05T11:48:57Z")

</div>

I have to repeat this for 4 different buttons and 4 targets, better to use separate flows?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 March 2020 12:18 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/12 "2020-03-05T12:18:02Z")

</div>

is there particular reason why you use the expression property in the switch node ?  
your flow is only 1 node btw

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 12:20 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/13 "2020-03-05T12:20:36Z")

</div>

The source node returns two states when the button is pressed for short time

1. Pressed 1 time
2. Inactive

The second state comes after 1 second.

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 12:23 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/14 "2020-03-05T12:23:29Z")

</div>

Additionally:  
This is a NanoMote switch (Z-wave device) with 4 buttons.  
All buttons behave the same way, but each updates different topic.

I want to:  
button one : Turn 2 bulbs on  
Button 4: turn 2 bulbs off  
button 2: toggle on/off bulb A  
button 3: toggle on/off bulb B

What I made is:

```auto
[{"id":"74fe1247.6a93cc","type":"switch","z":"6f8f89ff.9374a8","name":"Pressed 1 Time","property":"payload.value","propertyType":"jsonata","rules":[{"t":"eq","v":"Pressed 1 Time","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":360,"y":180,"wires":[["3882b615.0288ba"]]},{"id":"4fd7b937.164fe8","type":"change","z":"6f8f89ff.9374a8","name":"Set flow.state","rules":[{"t":"set","p":"MBfloorLightState","pt":"flow","to":"payload.value","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":60,"wires":[[]]},{"id":"95676085.92d26","type":"mqtt in","z":"6f8f89ff.9374a8","name":"vMote-Scene2","topic":"zwave2mqtt/MasterBedroom/VassilisSwitch/91/1/2","qos":"2","datatype":"json","broker":"96bab34d.d97b5","x":120,"y":180,"wires":[["74fe1247.6a93cc"]]},{"id":"1ead55d6.484a3a","type":"mqtt out","z":"6f8f89ff.9374a8","name":"MasterBedroom FloorLight","topic":"zwave2mqtt/MasterBedroom/Floorlight/37/1/0/set","qos":"1","retain":"","broker":"96bab34d.d97b5","x":880,"y":180,"wires":[]},{"id":"50afaa18.1b6a84","type":"mqtt in","z":"6f8f89ff.9374a8","name":"MasterBedroom FloorLight","topic":"zwave2mqtt/MasterBedroom/Floorlight/37/1/0","qos":"2","datatype":"json","broker":"96bab34d.d97b5","x":170,"y":60,"wires":[["4fd7b937.164fe8"]]},{"id":"3882b615.0288ba","type":"change","z":"6f8f89ff.9374a8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"MBfloorLightState\") = true ? false : true","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":180,"wires":[["1ead55d6.484a3a"]]},{"id":"323bfec4.8fe192","type":"switch","z":"6f8f89ff.9374a8","name":"Pressed 1 Time","property":"payload.value","propertyType":"jsonata","rules":[{"t":"eq","v":"Pressed 1 Time","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":360,"y":420,"wires":[["df209dfa.42bcd"]]},{"id":"88bc8853.7687d8","type":"mqtt in","z":"6f8f89ff.9374a8","name":"vMote-Scene3","topic":"zwave2mqtt/MasterBedroom/VassilisSwitch/91/1/3","qos":"0","datatype":"json","broker":"96bab34d.d97b5","x":120,"y":420,"wires":[["323bfec4.8fe192"]]},{"id":"aa8cea20.6cf6b8","type":"mqtt out","z":"6f8f89ff.9374a8","name":"Babyroom FloorLight","topic":"zwave2mqtt/Babyroom/Floorlight/37/1/0/set","qos":"1","retain":"","broker":"96bab34d.d97b5","x":860,"y":420,"wires":[]},{"id":"df209dfa.42bcd","type":"change","z":"6f8f89ff.9374a8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"BBfloorLightState\") = true ? false : true","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":420,"wires":[["aa8cea20.6cf6b8"]]},{"id":"a55e6965.1e4578","type":"change","z":"6f8f89ff.9374a8","name":"Set flow.state","rules":[{"t":"set","p":"BBfloorLightState","pt":"flow","to":"payload.value","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":120,"wires":[[]]},{"id":"3228f185.0c2e4e","type":"mqtt in","z":"6f8f89ff.9374a8","name":"Babyroom FloorLight","topic":"zwave2mqtt/Babyroom/Floorlight/37/1/0","qos":"2","datatype":"json","broker":"96bab34d.d97b5","x":160,"y":120,"wires":[["a55e6965.1e4578"]]},{"id":"d9b3f340.73562","type":"mqtt in","z":"6f8f89ff.9374a8","name":"vMote-Scene1","topic":"zwave2mqtt/MasterBedroom/VassilisSwitch/91/1/1","qos":"0","datatype":"json","broker":"96bab34d.d97b5","x":120,"y":260,"wires":[["a35f55d3.c38d88"]]},{"id":"a35f55d3.c38d88","type":"switch","z":"6f8f89ff.9374a8","name":"Pressed 1 Time","property":"payload.value","propertyType":"jsonata","rules":[{"t":"eq","v":"Pressed 1 Time","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":360,"y":260,"wires":[["6536093.6717ff8"]]},{"id":"6536093.6717ff8","type":"change","z":"6f8f89ff.9374a8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":260,"wires":[["1ead55d6.484a3a","aa8cea20.6cf6b8"]]},{"id":"b3944bb0.dfe778","type":"mqtt in","z":"6f8f89ff.9374a8","name":"vMote-Scene4","topic":"zwave2mqtt/MasterBedroom/VassilisSwitch/91/1/4","qos":"0","datatype":"json","broker":"96bab34d.d97b5","x":120,"y":340,"wires":[["796ed3c6.9dbb2c"]]},{"id":"796ed3c6.9dbb2c","type":"switch","z":"6f8f89ff.9374a8","name":"Pressed 1 Time","property":"payload.value","propertyType":"jsonata","rules":[{"t":"eq","v":"Pressed 1 Time","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":360,"y":340,"wires":[["b9618ae7.7d8e58"]]},{"id":"b9618ae7.7d8e58","type":"change","z":"6f8f89ff.9374a8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":340,"wires":[["1ead55d6.484a3a","aa8cea20.6cf6b8"]]},{"id":"96bab34d.d97b5","type":"mqtt-broker","z":"","name":"Main MQTT","broker":"mqtt","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 March 2020 12:27 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/15 "2020-03-05T12:27:24Z")

</div>

> is there particular reason why you use the expression property in the switch node ?

I see you use jsonata in all change nodes, why ?

This:  
 ![Screenshot 2020-03-05 at 13.26.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9ace46352a662f7770690186ebc4cafca907ed28.jpeg)

vs:  
 ![Screenshot 2020-03-05 at 13.26.52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2509d919c2973f225ee36f7dac96ea6655a52d8.jpeg)

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 12:28 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/16 "2020-03-05T12:28:05Z")

</div>

no reason, didn't know it works without jsonata... and seen it on some examples when searched.

does msg parse json?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [5 March 2020 12:28 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/17 "2020-03-05T12:28:33Z")

</div>

Its' it sole purpose.

---

<div class="post-metadata">

**Author:** ![billiaz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/billiaz/32/17921_2.png) [@billiaz](https://discourse.nodered.org/u/billiaz)\
**Post date:** [5 March 2020 12:30 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/18 "2020-03-05T12:30:45Z")

</div>

Fixed that! thanks.

Keep in mind, it is my first flow Ever / First use of Node-red  
Wanted to replace HomeAssistant Automations

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 May 2020 12:30 UTC](https://discourse.nodered.org/t/mqtt-toggle-to-change-zwave-bulb-state/22618/19 "2020-05-04T12:30:46Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
