Zigbee2mqtt to Domoticz flow help

Hi, (newbie in node-red)

I have created a node-red flow from Domoticz/out to Zigbee2mqtt/in which does the following:
When I turn on/off a virtual switch in Domoticz, a command is send to Zigbee2mqtt which sets a group of ikea GU10 bulbs in a certain state. This is working.

However the zigbee2mqtt group is also bound to a physical ikea dimmer remote.
Now if I change the state of the group with the remote to e.g. ON/OFF (which is a toggle button), the virtual switch in Domoticz doesn't know that the group state is changed to ON/OFF.

So I need to create a flow from Zigbee2mqtt/out to Domoticz/in to get the current state of the group or one of the devices in it and set the Domoticz virtual switch accordingly to the same state (e.g. ON/OFF) .

Should this be a separate flow or is it possible to combine this with the first flow?

I thought Domoticz could control those groups directly ? It is home automation software with mqtt support.

Although this doesn't help you with your flow, my tip would be to keep it as simple as possible. The more software in between the more potential problems. (note - i have all my home-automation in node-red only, including states and interface(s))

Nope sadly not for zigbee2mqtt groups.. :cry:

However, if you do everything with node-red i have some questions because my javascript skills (from scratch) are bad. I can read scripts and with trial and error i'll manage, but i don't have good examples

How would my flow and javascript function look like when I want to
Get the state of a zigbee2mqtt bulb and set the virtual switch in Domoticz to the same state:

  1. add an mqtt object to the flow (zigbee2mqtt location and login credentials)
    and set the topic zigbee2mqtt/Keuken_Spot1/get/state
    which should return something like this (can i check this in node red?)
    {"state": "OFF", "brightness": 200, "color_temp": 350}.

  2. add a JSON obect to the flow

  3. add a Function to the flow (The javascript funtion must execute something like this:)
    If the "state": "OFF"
    Then set the nvalue of the virtual domoticz switch (idx=183) to "0"
    And If the "state": "ON"
    Then set the nvalue of the virtual domoticz switch (idx=183) to "1"

  4. Add an mqtt object to the flow (domoticz location and login credentials)
    and set the topic to domoticz/in

  5. Connect the objects from 1 to 4 and deploy

(this flow is needed when I turn on the group of bulbs with the remote instead of the virtual switch)

I have some ideas, but could you elaborate on:

Get the state of a zigbee2mqtt bulb and set the virtual switch in Domoticz to the same state

When do you need to get this state ?

Is it only in this situation ?:

when I turn on the group of bulbs with the remote instead of the virtual switc

and do you receive information from that virtual switch as well ?

Every time the group of spots is turned on/off with the remote instead of the Domoticz virtual switch.

Yes, but since it is a normal switch only ON/OFF information i guess?
I already have a working flow which turns on/off the zigbee2mqtt group of bulbs when the Domoticz virtual switch is turned on/off. I based this flow on an existing example on the domoticz forum.

But I also need a flow (the one I am asking your help for) which tells the virtual switch, that the group is turned on/off with the remote so that the ON / OFF state is set equal to the actions that the remote does

The problem is that zigbee2mqtt can't send commands to the IKEA remote (apparently a limitation of the remote :cry:) the buttons only act as a sensor.

(I also use google home voice commands to turn on/off the virtual switch, but my wife is always using the remote :stuck_out_tongue_winking_eye:)

I am now at work, but I will send the flow I already have this weekend

Thanx for helping anyway :+1:

[{"id":"56035fe2.21c4e8","type":"tab","label":"Keuken_Licht","disabled":false,"info":""},{"id":"17b6b703.ebe381","type":"mqtt in","z":"56035fe2.21c4e8","name":"","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"b90f182d.2e6838","x":413.5,"y":215,"wires":[["452631d0.ef45a8"]]},{"id":"52d623f.852d35c","type":"comment","z":"56035fe2.21c4e8","name":"Domoticz Switch for Kitchen Light Group","info":"","x":165,"y":215,"wires":[]},{"id":"452631d0.ef45a8","type":"json","z":"56035fe2.21c4e8","name":"","property":"payload","action":"","pretty":false,"x":567.5000305175781,"y":215,"wires":[["96dc8119.c7418"]]},{"id":"96dc8119.c7418","type":"function","z":"56035fe2.21c4e8","name":"Turn On/Off Z2M Group","func":"if(msg.payload.idx == '183') {\nif(msg.payload.nvalue == '0') {\n    msg.payload.state = 'OFF'\n}\nif(msg.payload.nvalue == '1') {\n    msg.payload.state = 'ON',\n    msg.payload.brightness = 200,\n    msg.payload.color_temp = 350\n}\nreturn msg;\n}","outputs":1,"noerr":0,"x":768.7713623046875,"y":215.00000381469727,"wires":[["5eba5b0b.72b304"]]},{"id":"5eba5b0b.72b304","type":"mqtt out","z":"56035fe2.21c4e8","name":"zigbee2mqtt/in","topic":"zigbee2mqtt/Keuken_Spots/set","qos":"","retain":"","broker":"1ec43ef1.2b0269","x":1010.8785247802734,"y":216.3785514831543,"wires":[]},{"id":"b90f182d.2e6838","type":"mqtt-broker","z":"","name":"Domoticz","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1ec43ef1.2b0269","type":"mqtt-broker","z":"","name":"Zigbee2mqtt","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"domoticz/bridge/state","birthQos":"0","birthPayload":"online","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Please export your flow again, read this post and edit your post.

It is different then described in the link.
I first had to select the preformatted text format button and then paste the code inside.

This is the flow I was trying but functiondoes not work.
I need some examples how I get the information (variables) of the mqtt topic and then use them in a function

[{"id":"35e7b173.9567ee","type":"function","z":"80f07896.7cae2","name":"Set Virtual Switch State equal to Group State","func":"if(msg.payload.state == 'OFF')\n{\n    if(msg.payload.idx == '183') {msg.payload.state = '0'}\n    return msg;\n}\n\nif(msg.payload.state == 'ON')\n{\n    if(msg.payload.idx == '183') {msg.payload.state = '1'}\n    return msg;\n}","outputs":1,"noerr":0,"x":536.7666320800781,"y":412.23333740234375,"wires":[["b83a6cfb.4910a"]]}]

I actually want to get the state of the zigbee2mqtt group, but that is not possible with zigbee2mqtt therefor I get the state of of one of the group members

[Edit] This flow maybe already OK, but to run it, it needs to be triggered if one of the Remote Buttons is used. The remote has 5 buttons but only 3 are recognized by zigbee2mqtt when paired (known issue).
when I subscribe to mqtt topic zigbee2mqtt/Remote_E1524-E1810
And I press the buttons the following JSON the following messages are send to the topic:

{"linkquality" : 31, "battery" : 74, "action" : "toggle"} = Toggle ON/OFF button
{"linkquality" : 36, "battery" : 74, "action" : "brightness_up_click"}
{"linkquality" : 36, "battery" : 74, "action" : "brightness_up_hold"}
{"linkquality" : 36, "battery" : 74, "action" : "brightness_up_release"}
{"linkquality" : 36, "battery" : 74, "action" : "brightness_down_click"}
{"linkquality" : 36, "battery" : 74, "action" : "brightness_down_hold"}
{"linkquality" : 36, "battery" : 74, "action" : "brightness_down_release"}
{"linkquality" : 39, "battery" : 47, "action" : "arrow_right_click"}
{"linkquality" : 39, "battery" : 47, "action" : "arrow_right_hold"}
{"linkquality" : 39, "battery" : 47, "action" : "arrow_right_release"}
{"linkquality" : 39, "battery" : 47, "action" : "arrow_left_click"}
{"linkquality" : 39, "battery" : 47, "action" : "arrow_left_hold"}
{"linkquality" : 39, "battery" : 47, "action" : "arrow_left_release"}

Both brightness and toggle commands of the remote trigger the group of bulbs

  1. you have a physical remote that produces mqtt messages, the toggle does not state on/off, only toggle ?
  2. you have a "virtual switch" in Domoticz which produces ON/OFF ?

Your goal is to have them in sync ?
If domo is ON and physical remote is toggled, the virtual switch needs to turn OFF ?

To keep track the state you can work with context.

I am missing the complete picture in your topic. I see some JSON, some part of a flow, some function node.
Describe the flow in simple terms with the json output of each part.

Yes and yes

And how is the other way around handled: If physical remote toggles group ON, the virtual switch also should to be turned ON to stay in sync, but then there are 2 commands that wants to turn ON the group?

Where and how do I use context?

This is why I ask help, I don't know exactly where and how to start.
I have a working flow that turns on/off a group of bulbs when a virtual switch is set to on/off, thats basically it. But implementing physical remote and virtual switch in sync is much more compicated.

I even don't know if both ations can be combined in 1 flow or in separate flows, and how to do this?

What is the output from domoticz, the virtual switch (mqtt in -> debug - complete msg object)
Output from the physical switch (mqtt in -> debug - complete msg object)
What is the expected input towards domoticz ?


I am not sure of this is a debugged output, but here you go!

Virtual Switch (Domoticz):
In Domoticz I have installed MQTT Client Gateway with LAN Interface plugin to communicate with the mqtt server. The default "Publish Topic" setting of the plugin is domoticz/out (and domoticz/in)

Publish Topic: domoticz/out

{"Battery":255,"RSSI":12,"description":"","dtype":"Lighting 1","id":"73","idx":183,"name":"Keuken Licht","nvalue":0,"stype":"X10","switchType":"On/Off","unit":14,"state":"OFF"}

Physical Remote (Zigbee2mqtt):

Publish Topic: zigbee2mqtt/Remote_E1524-E1810


Remote_E1524-E1810 as a Sensor device (Domoticz) :
In Domoticz I also installed a zigbee2mqtt-domoticz plugin which discovers the zigbee2mqtt devices and turns them into Domoticz devices. The Remote_E1524-E1810 is integrated here as a sensor. Therefor when the physical remote toggle button is pressed, domoticz/out is also sending an output of the remote device.

Publish Topic: domoticz/out

{"Battery" : 47, "RSSI" : 1, "description" : "", "dtype" : "Light/Switch", "id" : "0x000d6ffffe5e7ec7_switch", "idx" : 261, "name" : "Remote_E1524-E1810", "nvalue" : 0, "stype" : "Switch", "svalue1" : "Off", "switchType" : "On/Off", "unit" : 10}

LOG File of (Zigbee2)MQTT:
Here the LOG file (with debug info) produced by the mqtt server after restart and setting both remote and virtual switch to ON and OFF.
log.txt (8.8 KB)

If the remote toggle button is used, the virtual switch in domoticz must sync ON/OFF with the remote (topic: domoticz/in)
If the Virtual Switch is used the remote must sync ON/OFF with the virtual switch.(topic: domoticz/out)

But the remote can not set/get ON/OFF information because it acts as a sensor (toggle button), therefor the zigbee2mqtt group state* info must be used to get the ON/OFF infomation.

*Get group state is not a function in zigbee2mqtt, therefor one of the devices in the group is used to get the ON/OFF info.
topic: zigbee2mqtt/Keuken_Spot1/get/state

And then?

Sorry for the delay.
And then....it is still confusing.

You can get the status from the topic: zigbee2mqtt/Keuken_Spot1/get/state ? so that would be the single truth of the current status whether or not the virtual switch/group is on or ff ?

is it not easier to move the controls to node-red only ? you can update the virtual switch from node-red

Yes, because not only the virtual switch but also
the (toggle) remote can change the state of the Zigbee2mqtt group.
The remote has no On/Off status but only a toggle, therefor I can’t use it to get the status of the group.

This is what I am trying to achieve.

I already figured out that with an injection node I can get the status of zigbee2mqtt/Keuken_Spot1/get/state
but this injection node needs to be triggered by a toggle action of the remote, which can change the group state too.
So I want to achieve that:

If remote button (toggle) is used
Then zigbee2mqtt/Keuken_Spot1/get/state
If state = on
Then set virtual switch on
Else virtual switch = off

Check this flow.
The key is to store the state of the "keuken_spot" into a context variable.

The toggle inject will check the context state and change it to on/off (if on then off, if off then on).

[{"id":"4ed0283c.0c14d8","type":"inject","z":"9a68d73d.80d63","name":"","topic":"","payload":"toggle","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":122,"y":96,"wires":[["a815f7d7.b0d2d8"]]},{"id":"9329a5dc.7c4a78","type":"mqtt in","z":"9a68d73d.80d63","name":"","topic":"zigbee2mqtt/Keuken_Spot1/get/state","qos":"0","datatype":"json","broker":"228872c0.fd94d6","x":198,"y":168,"wires":[["b99004e9.4c27e"]]},{"id":"b99004e9.4c27e","type":"change","z":"9a68d73d.80d63","name":"","rules":[{"t":"set","p":"light_state","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":474,"y":168,"wires":[[]]},{"id":"397b4971.eabf7e","type":"mqtt out","z":"9a68d73d.80d63","name":"","topic":"zigbee2mqtt/Keuken_Spot1/get/state","qos":"","retain":"","broker":"228872c0.fd94d6","x":438,"y":408,"wires":[]},{"id":"492f688e.13375","type":"inject","z":"9a68d73d.80d63","name":"Spot state ON","topic":"","payload":"{\"state\": \"ON\", \"brightness\": 200, \"color_temp\": 350}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":142,"y":384,"wires":[["397b4971.eabf7e"]]},{"id":"a815f7d7.b0d2d8","type":"change","z":"9a68d73d.80d63","name":"Toggle on/off","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"light_state.state\") =\"OFF\" ? \"ON\" : \"OFF\" \t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":262,"y":96,"wires":[["fb58b972.c98e6"]]},{"id":"fb58b972.c98e6","type":"debug","z":"9a68d73d.80d63","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":96,"wires":[]},{"id":"b8688af1.bf5a18","type":"inject","z":"9a68d73d.80d63","name":"Spot state OFF","topic":"","payload":"{\"state\": \"OFF\", \"brightness\": 200, \"color_temp\": 350}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":152,"y":432,"wires":[["397b4971.eabf7e"]]},{"id":"e163737f.109a78","type":"comment","z":"9a68d73d.80d63","name":"Ignore - emulation only","info":"","x":148,"y":336,"wires":[]},{"id":"228872c0.fd94d6","type":"mqtt-broker","z":"","name":"mqtt","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"connections","birthQos":"0","birthRetain":"false","birthPayload":"connected","closeTopic":"connections","closeQos":"0","closeRetain":"false","closePayload":"disconnected","willTopic":"connections","willQos":"0","willRetain":"false","willPayload":"disconnected"}]

This flow assumes this payload from the keuken spot (on/off):

{"state": "ON", "brightness": 200, "color_temp": 350}

Thanx for the flow, I tested it but ran into some problems because it didn't work

when a zigbee2mqtt/Keuken_Spot1/get/state is published, zigbee2mqtt/Keuken_Spot1 will get the output. (group zigbee2mqtt/Keuken_Spots can also be used to subscribe to)

zigbee2mqtt/Keuken_Spot1/get/state or zigbee2mqtt/Keuken_Spot1/get both return this json string {"state": "ON", "brightness": 200, "color_temp": 350}
Not "ON" or "OFF" which you may have expected.

As a result "set flow.light_state" change node returns
instead of ON or OFF

So the JSON string should be splitted up with some code in order to get ON or OFF I guess?
Where and how is that done?
I modified the flow to the correct topics:

[{"id":"4ff533af.0e6fbc","type":"inject","z":"f7ed534b.a4e54","name":"","topic":"","payload":"toggle","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":409.8833312988281,"y":195.88333129882812,"wires":[["a9174dcd.63141"]]},{"id":"9799f8fa.a0d96","type":"mqtt in","z":"f7ed534b.a4e54","name":"","topic":"zigbee2mqtt/Keuken_Spots","qos":"0","datatype":"json","broker":"1ec43ef1.2b0269","x":455.8833312988281,"y":267.8833312988281,"wires":[["5d19ba20.7f0cbc"]]},{"id":"5d19ba20.7f0cbc","type":"change","z":"f7ed534b.a4e54","name":"","rules":[{"t":"set","p":"light_state","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":761.8833312988281,"y":267.8833312988281,"wires":[["d0d45c46.1b2a98"]]},{"id":"49f621ee.a07268","type":"mqtt out","z":"f7ed534b.a4e54","name":"","topic":"zigbee2mqtt/Keuken_Spot1/get/state","qos":"","retain":"","broker":"1ec43ef1.2b0269","x":725.8833312988281,"y":507.8833312988281,"wires":[]},{"id":"fccc5630.5a0ef8","type":"inject","z":"f7ed534b.a4e54","name":"zigbee2mqtt/Keuken_Spot1/get/state","topic":"zigbee2mqtt/Keuken_Spot1/get/state","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":333.8833312988281,"y":507.8833312988281,"wires":[["49f621ee.a07268"]]},{"id":"a9174dcd.63141","type":"change","z":"f7ed534b.a4e54","name":"Toggle on/off","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"light_state.state\") =\"OFF\" ? \"ON\" : \"OFF\" \t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":549.8833312988281,"y":195.88333129882812,"wires":[["c2639e15.36732"]]},{"id":"c2639e15.36732","type":"debug","z":"f7ed534b.a4e54","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":717.8833312988281,"y":195.88333129882812,"wires":[]},{"id":"f19a96d6.52b75","type":"comment","z":"f7ed534b.a4e54","name":"Ignore - emulation only","info":"","x":435.8833312988281,"y":435.8833312988281,"wires":[]},{"id":"d0d45c46.1b2a98","type":"debug","z":"f7ed534b.a4e54","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":962.2666015625,"y":267.4499816894531,"wires":[]},{"id":"1ec43ef1.2b0269","type":"mqtt-broker","z":"","name":"Zigbee2mqtt","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"domoticz/bridge/state","birthQos":"0","birthPayload":"online","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Dombo71 here....

First flows with NR and i work with Domoticz....

[{"id":"976d0acd.c2c738","type":"mqtt in","z":"ac795f5f.c3495","name":"","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"734a852b.3ac33c","x":410,"y":160,"wires":[["1b904ebd.d592c1"]]},{"id":"1b904ebd.d592c1","type":"json","z":"ac795f5f.c3495","name":"","property":"payload","action":"","pretty":false,"x":567.5000305175781,"y":215,"wires":[["a44861ee.30264"]]},{"id":"a44861ee.30264","type":"function","z":"ac795f5f.c3495","name":"Ikea tussenstekker1","func":"if(msg.payload.idx == '12984') {\nif(msg.payload.nvalue == '0') {\n    msg.payload.state = 'OFF'\n}\nif(msg.payload.nvalue == '1') {\n    msg.payload.state = 'ON'\n   // msg.payload.brightness = 200,\n    //msg.payload.color_temp = 350\n}\nreturn msg;\n}","outputs":1,"noerr":0,"x":758.7713623046875,"y":215.00000381469727,"wires":[["53df0851.f5b228"]]},{"id":"53df0851.f5b228","type":"mqtt out","z":"ac795f5f.c3495","name":"domoticz/in","topic":"zigbee2mqtt/Ik: Tustek-1","qos":"1","retain":"true","broker":"734a852b.3ac33c","x":990,"y":200,"wires":[]},{"id":"734a852b.3ac33c","type":"mqtt-broker","z":"","name":"Domoticz mqtt","broker":"","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"Domoticz","birthQos":"2","birthRetain":"true","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Will try just a simple on off device from ikea switching in Domoticz
And perhaps possible te other value from a sensor..

Only this flow is not working...?
Do someone have perhaps some noce flows that are working in Domoticz as an example.[ so i can build from that one ]