MQTT Toggle, to change ZWAVE Bulb state

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.


My flow:

[
  {
    "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

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

[{"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.

1 Like

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:

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

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

1 Like

@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

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

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

[{"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

In a function node:

For a global variable:

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

Easily adjusted for a msg property.

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?

So node.js basics apply here too?

This is not javascript, but jsonata

cool will read more

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

[{"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"]]}]

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

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

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.

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:

[{"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":""}]

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

vs:
Screenshot 2020-03-05 at 13.26.52

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

does msg parse json?

Its' it sole purpose.

Fixed that! thanks.

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

3 Likes

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