Hello everyone, please if you can help with my flow, I would like to put two conditions, if any devices are online state send mqtt message "off", if all devices have offline state send mqtt message "on"
Thanks for your help and have a nice day
On this kind of community forum, the "custom" is to present what you have done and doesn't work as intended and not just saying "please, can you do that for me"...
So, what have you tried so far?
Here I got stuck, I don't have the necessary knowledge to write the code for function node, I tried with change node and switch node without success.
you could use JSONata in change node
set msg.payload
to J:
"online" in $$.payload.*.state ? "off" : "on"
// may need slight editing as i have not tested against your payload
should return 'off' if any state is online, and 'on' if all are offline
What if two devices are online?
I asked for help and guidance, I did not expect the final result
the message is "off",only if all devices is offline the message is "on".
Thank you.
OK, so you meant 'if any devices are online' not 'if one or all devices are online`. I suggest you edit your initial post to make it clear.
If you want to use a Function node that you can use Object.entries() to give you an array of key/value pairs, then you can iterate through that to test the values. Something like
for (const [key, value] of Object.entries(msg.payload)) {
// key contains the key name (the IP address) and value contains the array
// do the test here
}
Thank you.
Have a nice day!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.