Hi, its taken me a long while to debug a problem where I want to capture all the sensor info I care about from a bunch of sensors (water, temp/humidity, motion, remotes) via Zigbee2Tasmota and I've got quite stuck where my jsonata code works perfectly in the test panel, but when applied to real incoming messages it does not. I've hit a brick wall and could do with some help. This jsonata takes some getting used to! I wish there was a really full nodered/jsonata tutorial or cookbook - it would really help. Browsing the forums has been really helpful so far, but I'm needing some specific help with getting the Temperature extracted.
Any solutions, pointers or tips to making this better would be much appreciated. Currently the flow switches according to the sensor type (parsing the name) which pipes into topic creators, then to this payload node.
It's not that Steve as the **.Linkquality matches as expected, I'm just setting that to new keys, camelcased. The trouble is the temperature jsonata is not matching.
Ah ok. So are you are trying to split the value in FE03/0020 by a , and divide the last element by 100?
Why not just do it in JavaScript (function node)?
Ps, can you capture and post a real sample message that doesn't work? I suspect the order of properties are different in you incoming data & as you are using $keys[2] it may not be the property you are actually wanting.
Yep, I'm trying to get that temperature value but that string `FE03/0020' isn't static amongst all the sensors, but I could regex it. You're right about the ordering, I'd considered this but my jsonata isn't good enough to find the value matching ENV via search rather than ordering.
I'd prefer jsonata as I'm trying to master it, unless it wasn't designed for this kind of extracting. But I will try it in JS.
That was the real sample message in debug and this is what I see in MQTT.
Didn't work unfortunately. In the test panel the temp match disappears and node-red doesn't like it when the payload is passed into the node with this error message.
Its failing with that error because your jsonata received a payload that doesnt include a value that can be $split ? It worked fine if Water or Power were missing but $split didnt like the absent value
The thing is the key "FE03/0020" changes for the different sensors and I think possibly even on the one sensor. Ideally there'd be a regex for this, which I'm looking at the jsonata docs for info.
maybe possibly storing a boolean variable $temp whether the msg has a "ENV" so that means it a temperature. and after using that as a condition (?) to do the $split ?
Tried that code UnborN but unfortunately it didn't pickup the temperature. I'm struggling with the JS too, but I'd like to think jsonata can do this, searching through the value strings, to find one matching ENV and presenting that containing string, for manipulation.
It's in the payload property/object and is, afaik, a default from zigbee2tasmota
It works! Your idea with the $temp function/expression works very well, I'll use that again. Thanks very much.
You're right, that worked too, thanks! I hadn't realised I'd needed to add payload explicitly. I'm mindful not to rely on positional checks though, so the additional $temp method from @UnborN is the way to go.
Flow
As can be seen in both solutions I get lots of errors from msg.payloads that don't parse well. I take it, even though it may not be damaging, its good form in node-red to eradicate errors and have the msg flow dropped cleanly with logging maybe?
Here's the full flow with both solutions. I need to add on the import of all the measurements found to InfluxDB which is the pimary task. I'll loop through the key/values per topic/payload.
good job @E1cid that looks a lot cleaner ..
and also your solution doesn't depend on getting the key[2] property that was making the code a bit unreliable (if im reading it correctly)
Excellent. Its a better solution for me too as I realised that the Humidity was embedded in the string as well, so this jsonata easily modifiable, could pick up that variable too.