# Zigbee2mqtt to Domoticz flow help

**URL:** <https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453>\
**Category:** General\
**Created:** [16 January 2020 13:39 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453 "2020-01-16T13:39:42Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [26 January 2020 13:42 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/21 "2020-01-26T13:42:08Z")

</div>

> [@Dylantje](#):
>
> Do someone have perhaps some noce flows that are working in Domoticz as an example.[so i can build from that one]

This is a flow which changes the state of a zigbee2mqtt group (of bulbs) with an vritual on/off switch in domoticz. The idx=183 is the domoticz object

```auto
[{"id":"56035fe2.21c4e8","type":"tab","label":"Keuken_Licht_Group","disabled":false,"info":""},{"id":"17b6b703.ebe381","type":"mqtt in","z":"56035fe2.21c4e8","name":"","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"b90f182d.2e6838","x":134.50003051757812,"y":210,"wires":[["452631d0.ef45a8"]]},{"id":"52d623f.852d35c","type":"comment","z":"56035fe2.21c4e8","name":"Domoticz Switch for Kitchen Light Group","info":"","x":224.00003051757812,"y":157,"wires":[]},{"id":"452631d0.ef45a8","type":"json","z":"56035fe2.21c4e8","name":"","property":"payload","action":"","pretty":false,"x":288.50006103515625,"y":210,"wires":[["96dc8119.c7418"]]},{"id":"96dc8119.c7418","type":"function","z":"56035fe2.21c4e8","name":"Turn On/Off Z2M Group","func":"if(msg.payload.idx == '183') {\n//if(msg.payload.idx == '268') {\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 = 120,\n msg.payload.color_temp = 350\n}\nreturn msg;\n}","outputs":1,"noerr":0,"x":489.7713928222656,"y":210.00000381469727,"wires":[["5eba5b0b.72b304","25b6c7eb.71f88"]]},{"id":"5eba5b0b.72b304","type":"mqtt out","z":"56035fe2.21c4e8","name":"","topic":"zigbee2mqtt/Keuken_Spots/set","qos":"","retain":"","broker":"1ec43ef1.2b0269","x":762.8785705566406,"y":210.37855529785156,"wires":[]},{"id":"25b6c7eb.71f88","type":"debug","z":"56035fe2.21c4e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":716.2666320800781,"y":323.4499816894531,"wires":[]},{"id":"b90f182d.2e6838","type":"mqtt-broker","z":"","name":"Domoticz","broker":"127.0.0.1","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":"127.0.0.1","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":""}]

```

---

<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:** [26 January 2020 13:42 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/22 "2020-01-26T13:42:49Z")

</div>

Perhaps you are missing something.

The flow I posted above had a comment "ignore emulation only"  
You have changed the inject node topic, while the mqtt node topic was already set. The point of those inject nodes was to emulate your output: `{"state": "ON", "brightness": 200, "color_temp": 350}` (or OFF)

> when a `zigbee2mqtt/Keuken_Spot1/get/state` is published, `zigbee2mqtt/Keuken_Spot1` will get the output.

And is that correct ? I would suspect something like: a set and a get (and indeed the [documentation describes this as well](https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html)). If you subscribe to a topic-layer above, you will get everything, be specific and get specific output, or generalize and filter.

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [26 January 2020 16:26 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/23 "2020-01-26T16:26:28Z")

</div>

On your initial flow I added a debug node after the "set flow.light\_state" change node, i got no output. After changing the mqtt topic to `zigbee2mqtt/Keuken_Spots` or `zigbee2mqtt/Keuken_Spot1` i did get an output after running emulation injection node set to `zigbee2mqtt/Keuken_Spot1/get/state` or without state

---

<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:** [26 January 2020 16:38 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/24 "2020-01-26T16:38:34Z")

</div>

> "set flow.light\_state" change node, i got no output.

Because there is no output, it sets a flow variable, ie: to keep track. (there is output, but that is not relevant)  
The point was to click the `toggle` which emulates your physical remote (and yes it requires an initial state)

To see the flow variables:

 ![Screenshot 2020-01-26 at 17.41.25](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10798861fe6ae8e74811b99319013a7b0c625425.jpeg)

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [26 January 2020 16:44 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/25 "2020-01-26T16:44:56Z")

</div>

And this variable should be ON or OFF?  
(Not at home to check)

---

<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:** [26 January 2020 16:50 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/26 "2020-01-26T16:50:44Z")

</div>

- Import the flow
- Click the "Spot state ON" inject button (this is to set the initial state)
- check the context data (you may need to click refresh button for the flow context) - context = ON
- click the toggle inject button, result: OFF

converting this to real life:

- Remove the "ignore" part.
- switch your light once manually (check the context data while doing that)
- replace the toggle inject with an mqtt node, set topic to: `zigbee2mqtt/Remote_E1524-E1810`

your remote should work.

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [28 January 2020 22:57 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/27 "2020-01-28T22:57:17Z")

</div>

> [@bakman2](#):
>
> check the context data (you may need to click refresh button for the flow context) - context = ON

This is the context data I see:

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/eff660224dc077ec1fcc4601483818c1c0726fcb.png)

I told you it is not ON or OFF!  
It doesn't matter if the topic is set to:

```auto
zigbee2mqtt/Keuken_Spot1/get/state
zigbee2mqtt/Keuken_Spot1/get

```

Both topics return `{"state": "ON", "brightness":200, "color_temp": 350}`

So the JSON string needs to be split up first....how do I do that?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 January 2020 23:32 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/28 "2020-01-28T23:32:56Z")

</div>

if you mean how do you get the 'state' from the flow variable it will depend on if you are using a `change` node or `function` node. In a change node you would select 'flow' then use 'light\_stats.state`. In a `function` node you would use something like

> var x = flow.get("light\_stats.state");

---

<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:** [28 January 2020 23:35 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/29 "2020-01-28T23:35:21Z")

</div>

Read the post above where i link to the documentation.

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [29 January 2020 09:23 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/30 "2020-01-29T09:23:05Z")

</div>

So if I get it right then the

```auto
zigbee2mqtt/Keuken_Spot1/get/state

```

Returns `{"state": "ON", "brightness":200, "color_temp": 350}` to the change node **"set flow.light\_state"**

the variable `$flowContext("light_state.state")` will return "ON"? (or OFF of course)  
In a Function node `var x = flow.get("light_state.state");` will also return "ON"?

and `$flowContext("light_state.brightness")` would return "200"?  
also `var x = flow.get("light_state.brightness");` would return "200"?

and `$flowContext("light_state.color_temp")` would return "350"?  
also `var x = flow.get("light_state.color_temp");` would return "350"?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 January 2020 11:07 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/31 "2020-01-29T11:07:38Z")

</div>

If you are looking directly at the msg coming in from MQTT, then no, that is not right bcause it is not in the flow context. You could access the values with msg.payload.state and msg.payload.brightness etc.

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [29 January 2020 13:02 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/32 "2020-01-29T13:02:46Z")

</div>

What does the flow context node **"set flow.light\_state"** expect to get (or extract) from the mqtt topic: `zigbee2mqtt/Keuken_Spot1/get/state` then?  
It does not only get the state, as the topic name does suggest.

There is no valid topic in zigbee2mqtt to **get** only the state of a device (maybe the zigbee2mqtt topic and message structure documentation is misleading here?).

`zigbee2mqtt/Keuken_Spot1/get/state` or `zigbee2mqtt/Keuken_Spot1/get`  
produce the same incoming message from mqtt:`{"state": "ON", "brightness":200, "color_temp": 350}`

What does work is **set** a state `zigbee2mqtt/Keuken_Spot1/set/state` with payload ON or OFF

Or `zigbee2mqtt/Keuken_Spot1/set` with a JSON payload `{"state": "ON", "brightness":200, "color_temp": 350}`

I am trying to understand here

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 January 2020 14:26 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/33 "2020-01-29T14:26:47Z")

</div>

In the `mqtt-in` node, you specify a topic you want to subscribe to. That is all the topic is used for.

The device that publishes using that topic, will (hopefully) be sending data. How that data comes in is _controled_ by the device that publishes the data using that topic. (Note, a device could publish different formats of data using different topics but hopefully the data published using a certian topic will remain the same format.)

In your case it is sending a json object which ends up in msg.payload. If you put a `debug` node (set to display the 'Complete msg object') on the output of the `mqtt-in` node, you will be able to see this.

From there on, it is just a matter of accessing the various items in the json object.

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [29 January 2020 15:54 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/34 "2020-01-29T15:54:51Z")

</div>

That's what I thought too...The idea of this thread was this:

1. I already have a flow running which sets a zigbee2mqtt Group of bulbs in a certain state when a Domoticz virtual switch is set to ON or OFF....this flow:

```auto
[{"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":"127.0.0.1","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":"127.0.0.1","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":""}]

```

1. The zigbee2mqtt Group is also controlled with a dimmer Remote (IKEA E1524/E1810) which is bound to the zigbee2mqtt Group. But since this remote is a "toggle" ON/OFF device, it is not possible to get the ON/OFF state of this device. see JSON: `{"linkquality":42,"battery":47,"action":"toggle"}`

2. When the toggle button of the Remote is used to change the state of the Group to ON/OFF, the Domoticz virtual switch does not know that the zigbee2mqtt Group is set to OFF or ON.

3. So I was looking for a flow example which sets the Domoticz virtual switch in "ON/OFF" sync with the state of the zigbee2mqtt Group.

4. The topic & message structure of zigbee2mqtt ([https://www.zigbee2mqtt.io/information/mqtt\_topics\_and\_message\_structure.html](https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html)) does not allow to get the state of a group, but it does allow to get the state of devices. Therefor I wanted to create the following flow:

- If the physical remote toggle button is used
- Then get the state of one of the devices in the group with  
`zigbee2mqtt/Keuken_Spot1/get/state` or `zigbee2mqtt/Keuken_Spot1/get` which both return the same JSON string `{"state": "ON", "brightness":200, "color_temp": 350}`
- If "state": "ON" Then set Domoticz virtual switch to ON (if not already)
- If "state": "OFF" Then set Domoticz virtual switch to OFF (if not already)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 January 2020 18:56 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/35 "2020-01-29T18:56:06Z")

</div>

Not having eithor a Zigbee or Domoticz I'm forced to bow out here. Maybe someone else will be able to help you.

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [29 January 2020 22:17 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/36 "2020-01-29T22:17:11Z")

</div>

Well maybe you could help me with small function?

When the remote toggle button is used it sends a message: `{"linkquality":26,"battery":21,"action":"toggle"}`

Now only when this message is received I want to publish `zigbee2mqtt/Keuken_Spot1/get`, to get the state of the Bulb....something like:

```auto
if (msg.payload.action == "toggle"){

activate a publish to zigbee2mqtt/Keuken_Spot1/get

return msg;
}

```

How is that done in this function node? or should I use a different method to achieve this?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 January 2020 22:59 UTC](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453/37 "2020-01-29T22:59:05Z")

</div>

Use a switch node for the if, a change node to set the payload as you need it and then an mqtt node to publish it

[Previous page](https://discourse.nodered.org/t/zigbee2mqtt-to-domoticz-flow-help/20453.md?page=1)
