Node-Red and Zigbee2MQTT Integration

This is my first attempt at integrating zigbee2mqtt with node-red and I’ve struck a strange issue.

I have an aqara temperature sensor I’ve called HouseTemp, and when it transmits across mqtt I see this in the zigbee2mqtt/HouseTemp mqtt topic. Here’s what comes from that topic…

{"battery":100,"humidity":56.09,"linkquality":111,"pressure":1040,"temperature":21.35,"voltage":3065}

I am used to individual values being transmitted, such as temperature on a separate topic, such as zigbee2mqtt/HouseTemp/temperature. But it seems zigbee2mqtt transmits everything in one string?

Is there an easier way for me to pull out the data I am seeking?

afaik z2m can be configured to store attributes in separate topics (I saw such a setting).

But even without this change, what’s the problem to extract single value from json object?

Another question, since you are asking on HA forum. Don’t you have this temperature reflected by HA entity? Then why just don’t use the HA entity in your NR flows?

1 Like

A split node will break the message using the ,

[{"id":"07951b7540900463","type":"debug","z":"f80b6c338afd5483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":240,"wires":[]},{"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":310,"y":240,"wires":[["c32b32089ab2a319"]]},{"id":"c32b32089ab2a319","type":"split","z":"f80b6c338afd5483","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":540,"y":240,"wires":[["07951b7540900463"]]}]
1 Like

Hi Mike… thank you for the help. Assume I am an idiot when it comes to this stuff. Could you suggest where that code might be used?

Thanks, Mark

Hi Max,

Haven’t ever done that before in Node-Red. Normally I have Node-Red listen to the MQTT topics and then use that for the triggering etc. Dont suppose there’s somewhere I might be find an example of the approach you are suggesting, as it sounds good so would like to give it a go.

Thanks, Mark

Why are you communicating with mqtt? A device is generated in HA. Deal with the HA device. (this is the HA forum!)

@nickrout why are you being so aggressive?

Not everyone is an expert, and some of us are exploring options. Ok, so not every question we ask is perfect, but to reply with “this is the HA forum!” in my opinion is unnecessarily aggressive, and could frighten people away from using HA… especially if this is the response from some members on the forum.

I take it you are advocating a different approach. So perhaps in a calm manner you can communicate what you believe is the approach you recommend?

3 Likes

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?