Node-Red and Zigbee2MQTT Integration

I wouldn’t get it aggressive.
If you want to find a way with NodeRed alone - the best choice is to ask on NodeRed forum. If you have Zigbee2Mqtt related questions, maybe it’s better to ask on z2m forum.

If you are asking on HA forum we are assuming you mainly use HA as a core system. Then z2m and NR are considered as “plugins” to HA. From this POV using NR installed in HA ecosystem but directly processing mqtt data generated by zigbee2mqtt somehow makes no sense. (since HA creates ready to use entities from the same mqtt data automatically)

And to make the info complete: there are cases where letting NR to communicate with mqtt is needed (ussually to achieve some specific goals HA is not able to do). But those are exceptions and it’s not your case imo.

BTW: code provided by Mike you can import to NR using import option (see top right “hamburger” menu.

The only reason I am using z2m is because ZHA delivered inconsistent results, so I had no choice but to give up on ZHA and move to z2m.

HA and ESPHome are at the heart of my home automation, with the use of MQTT purely historical as that’s where I started before HA, and I mostly use MQTT to interface with NodeRed because again that setup is historical. And NodeRed interfaces to my Blynk app.

So yes, I agree it can appear to make no sense… but right now I am attempting to bridge a technology gap until I can find the time to move towards closer HA/NR integration.

I am also asking for advice on the NodeRed forum, and thus far have received some great suggestions… none of which included telling me I am on the wrong forum. I guess that can sometimes be the difference between various forums.

I am not an expert in this, so learning and experimenting takes me time… which can be painful for the gurus on here because I appear to ask dumb questions.

My questions are not designed to annoy… they are purely to help me learn… and if that is wasting the time of some people on here… then they dont need to read the question or reply.

Hi @Mikefila (thanks to @maxym for the tips on loading) the code works great and splits the message coming out of the aqara sensor. I now want to extract just the temperature, so will go to the NodeRed forum to ask that question.

I assume this can be done more easily using the HA nodes in NodeRed instead of mqtt, but I will need to do some testing. Thanks again for the guidance, much appreciated.

There is no need to parse the data manually with split.
Use the json node.

[{"id":"f91c0961d1a8d7db","type":"inject","z":"14ca354715bc92f3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"battery\":100,\"humidity\":56.09,\"linkquality\":111,\"pressure\":1040,\"temperature\":21.35,\"voltage\":3065}","payloadType":"str","x":230,"y":640,"wires":[["b1664fd5d8d9d187"]]},{"id":"ed0a70bcd258ea58","type":"debug","z":"14ca354715bc92f3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":640,"wires":[]},{"id":"b1664fd5d8d9d187","type":"json","z":"14ca354715bc92f3","name":"","property":"payload","action":"","pretty":false,"x":400,"y":640,"wires":[["ed0a70bcd258ea58"]]}]
2 Likes

Hi @Hellis81, thanks for the tip… Implemented the json node and the output looks like below…

jsonoutput

I’ve asked in the NR forum if there is a way to grab just the temperature value, so I can send that to my Blynk app. Let’s see how it goes.

The way you deal with multi value mqtt strings AFAIK is to break the message up into separate messages in both nodered and HA. Here you can find 2 examples by Taras, to break the message and republish each key value pair to it’s own topic.

@Hellis81 's json node does a much better job of making the string manageable. I would still split the massage, because that is how I know how to do it. This example will show you how to use a value to trigger actions or how to create an entity in HA.

In order to create entities in HA you need the nodered companion from HACS.

[{"id":"63c813fa319b6332","type":"inject","z":"f80b6c338afd5483","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"battery\":100,\"humidity\":56.09,\"linkquality\":111,\"pressure\":1040,\"temperature\":21.35,\"voltage\":3065}","payloadType":"str","x":350,"y":260,"wires":[["8bb613f20e14c230"]]},{"id":"8bb613f20e14c230","type":"json","z":"f80b6c338afd5483","name":"","property":"payload","action":"","pretty":false,"x":510,"y":260,"wires":[["5ba2b35839d3cbe1"]]},{"id":"e99f93666987b36b","type":"switch","z":"f80b6c338afd5483","name":"","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"battery","vt":"str"},{"t":"eq","v":"humidity","vt":"str"},{"t":"eq","v":"linkquality","vt":"str"},{"t":"eq","v":"pressure","vt":"str"},{"t":"eq","v":"temperature","vt":"str"},{"t":"eq","v":"voltage","vt":"str"}],"checkall":"true","repair":false,"outputs":6,"x":810,"y":260,"wires":[[],["459e775b6f7647df"],[],["c6f847bb2248cffd"],[],[]]},{"id":"5ba2b35839d3cbe1","type":"split","z":"f80b6c338afd5483","name":"","splt":"\\n","spltType":"str","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"","x":670,"y":260,"wires":[["e99f93666987b36b"]]},{"id":"c6f847bb2248cffd","type":"ha-entity","z":"f80b6c338afd5483","name":"pressure","server":"","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1020,"y":300,"wires":[[]]},{"id":"459e775b6f7647df","type":"switch","z":"f80b6c338afd5483","name":"Is humidity above 50","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"50","vt":"num"},{"t":"lt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1060,"y":240,"wires":[["9b789c9da4bbef49"],["134d0e7b3a5b8412"]]},{"id":"9b789c9da4bbef49","type":"api-call-service","z":"f80b6c338afd5483","name":"turn something on","server":"","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1370,"y":220,"wires":[[]]},{"id":"134d0e7b3a5b8412","type":"api-call-service","z":"f80b6c338afd5483","name":"turn something off","server":"","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1370,"y":260,"wires":[[]]}]

Hi Mike, I think if I can get the switch sorted in your example then I am there. I tried the config below, but nothing goes to the debug, so I am obviously missing something.

PayloadSwitchConfig

The folks in the NodeRed forum mentioned above to try… but I figure there’s something not right?

So close :laughing:

Hi @tteck, that looks really interesting… will take a look.

If you only need the temperature then that is a good way in my opinion.

You need to use the change node, the switch node is wrong for this application.

What you are doing with the switch node is comparing msg.payload to msg.payload.temperature and only progressing the msg if these are equal.

You are right. It should be a change node. I didn’t notice that.

Hi Nic… Tried going from the switch to the change but unsuccessful. Do you have an example I could look at?

He meant you need to replace the switch node with a change node.
With a change node you will change/replace the payload value with the payload.temperature value, which means the debug node that is currently set to payload will display the temperature and not an object.

1 Like

image

Alternatively you could ditch the change node altogether and just have the debug node output msg.payload.temperature rather than msg.payload

Please reload my flow above, you left out the split node. The split node is needed before the switch.

In my opinion, using the split only makes your life harder.
When you split a message you can only work with that part of the message.
If a condition is temperature is above x and humidity above y then it will be show stopper for you.

However as long as you keep the message as one then any part can be used to any purpose independently.

It won’t be harder to use the complete message, just different.
I still have not yet seen anything where the message is used except a debug, or understood what the end purpose is.
If you tell us what you want then I believe we can help you.

I don’t know how to break out the temp value after the json node without spiting it to isolate the value. OP wants the temperature value to send to the blynk app.

Hi @Hellis81 @nicp and @Mikefila… Here’s what I implemented and it is working… sort of. I am successfully sending the temperature data to the Blynk app, but haven’t figured out how to extract something like the humidity. Which I suspect is where @nicp is going.


HouseTempSwitchNode
HouseTempChangeNode

Ultimately I’d like to be able to extract the individual elements contained within msg.payload, which I suspect may require a switch node to redirect the data? Or am I looking in the wrong place?