Run something if more msg are at false

How can I sent a mqtt message if msg.Terr1, msg.Gar1, msg.Gar2 and msg.Ht1 are false?

As MQTT messages dont arrive at exactly the same time, what would be the trigger?

Anyway, here is one way...

  • In those change nodes (set msg.Terr1 etc), instead, set the msg.topic to Terr1 or Gar1 or Gar2 etc THEN connect all four set nodes to a JOIN node set like this...
    image

When the join node fires, you will have access to msg.Terr1, msg.Ger1 etc that you can use in a function node (to check they all === false) or 4 SWITCH nodes set in series.

1 Like

Thanks. I tried this in a function but it won't work. I think I have to extract it somehow, but how?

if(msg.Terr1 === "false"){
    msg.payloa2d = "false"
}else{
    msg.payloa2d = "invalid"
}

return msg;

what is msg.payloa2d ?

And also, are your values actual strings? === 'false' will never match a Boolean false

Im sure its a string.

The flow:

[{"id":"d2bcff9d.29653","type":"change","z":"bdba9a4a.735618","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"Terr1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":300,"wires":[["90a65946.9ce5b8"]]},{"id":"320f9ae7.32def6","type":"switch","z":"bdba9a4a.735618","name":"contains signalrestoration","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"signalrestoration","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":300,"wires":[["d2bcff9d.29653"]]},{"id":"f0806aae.574a58","type":"change","z":"bdba9a4a.735618","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"Gar1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":340,"wires":[["90a65946.9ce5b8"]]},{"id":"dcd9a1ea.09035","type":"switch","z":"bdba9a4a.735618","name":"contains signalrestoration","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"signalrestoration","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":340,"wires":[["f0806aae.574a58"]]},{"id":"c4cee8ac.904058","type":"switch","z":"bdba9a4a.735618","name":"contains signalrestoration","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"signalrestoration","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":380,"wires":[["8f93a18a.ba4c5"]]},{"id":"8f93a18a.ba4c5","type":"change","z":"bdba9a4a.735618","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"Gar2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":380,"wires":[["90a65946.9ce5b8"]]},{"id":"b027f2bf.3c642","type":"switch","z":"bdba9a4a.735618","name":"contains signalrestoration","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"signalrestoration","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":420,"wires":[["1aac70bd.a4f7bf"]]},{"id":"1aac70bd.a4f7bf","type":"change","z":"bdba9a4a.735618","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"Ht1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":420,"wires":[["90a65946.9ce5b8"]]},{"id":"130490b8.2c379f","type":"debug","z":"bdba9a4a.735618","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payloa2d","targetType":"msg","x":1140,"y":460,"wires":[]},{"id":"71c38d46.2ad354","type":"inject","z":"bdba9a4a.735618","name":"","topic":"trigger","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":560,"wires":[["d2bcff9d.29653","f0806aae.574a58","8f93a18a.ba4c5","1aac70bd.a4f7bf"]]},{"id":"90a65946.9ce5b8","type":"join","z":"bdba9a4a.735618","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":830,"y":460,"wires":[["ccd6d274.405aa"]]},{"id":"ccd6d274.405aa","type":"function","z":"bdba9a4a.735618","name":"f","func":"if(msg.Terr1 === \"false\"){\n    msg.payloa2d = \"false\"\n}else{\n    msg.payloa2d = \"invalid\"\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":970,"y":460,"wires":[["130490b8.2c379f","7e78706e.88f36"]]},{"id":"7e78706e.88f36","type":"debug","z":"bdba9a4a.735618","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1130,"y":420,"wires":[]}]

Whenever a node is not doing what you expect then add a debug node showing what is going into your it (the function node in this case). Then you can immediately see what you have. Post the result here if you can't see the problem.

I did:
I'm talking about this function:

if(msg.Terr1 === false){
    msg.payloa2d = "false";
}else{
    msg.payloa2d = "invalid";
}

return msg;

It won't work because I can't use msg.Terr1 because I have to extract it first from: {"Terr1":"false","Gar1":"false","Gar2":"false","Ht1":"false"} (I think). But I don't know how to do this.

You can reduce your flow quite a bit.

Starting with the mqtt nodes (depending on the variety of input topics), you could use a single mqtt node with a topic of:

BlueIris/state/#
That will receive everything on this level.
You already have a topic, so you don't have to set it.

Then you can search of the signalrestoration with a single node, the topic remains in tact and then directly into the join. Turn off the "subsequent messages" checkbox.

You set all values to false, so they are always false.

It would look like:

Example flow

[{"id":"229b0682.974ad2","type":"change","z":"b371db5d.e78f5","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":578,"y":288,"wires":[["7c880430.ebbb44"]]},{"id":"e25218df.b4a46","type":"switch","z":"b371db5d.e78f5","name":"signalrestoration ?","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"signalrestoration","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":402,"y":288,"wires":[["229b0682.974ad2"]]},{"id":"79cb156b.497884","type":"inject","z":"b371db5d.e78f5","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":108,"y":192,"wires":[["4570e325.88c8ec"]]},{"id":"7c880430.ebbb44","type":"join","z":"b371db5d.e78f5","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":698,"y":288,"wires":[["52a9ba18.effca4"]]},{"id":"52a9ba18.effca4","type":"function","z":"b371db5d.e78f5","name":"allFalse ?","func":"msg.payload = \"All false: send message\"\ndelete msg.topic\nreturn msg","outputs":1,"noerr":0,"x":828,"y":288,"wires":[["b548ab4.3b7f058"]]},{"id":"b548ab4.3b7f058","type":"debug","z":"b371db5d.e78f5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":962,"y":288,"wires":[]},{"id":"4570e325.88c8ec","type":"function","z":"b371db5d.e78f5","name":"","func":"node.send({topic:'BlueIris/state/Terr1sd',payload:\"signalrestoration\"})\nnode.send({topic:'BlueIris/state/Gar1sd',payload:\"signalrestoration\"})\nnode.send({topic:'BlueIris/state/Gar2sd',payload:\"signalrestoration\"})\nnode.send({topic:'BlueIris/state/Ht1sd',payload:\"signalrestoration\"})","outputs":1,"noerr":0,"x":242,"y":192,"wires":[["e25218df.b4a46"]]},{"id":"b9caf106.b153f8","type":"comment","z":"b371db5d.e78f5","name":"Emulate topics","info":"","x":176,"y":144,"wires":[]}]

Dumb question/observation:

I'll do this only for one thing. Not all 4.

You are getting messages coming out of the MQTT node.

Fair enough, you then have a switch node. If it contains (is equal) to a desired condition
So why then have the change node?

After the switch node you have the condition for which you are testing.
I am failing to see why you are changing the message (payload?) to something else.

Get the message after the switch node and do the and comparison on it at that stage.

Can you add a debug AFTER the join node (set the debug to "show complete message") - and show us that the Join is operating AND that it contains .Terr1, .Gar1, .Gar2 and .Ht1


First, a reminder of what you want to do...

Answer...

If the Join IS working and you have all 4 properties inside the msg, then the logic is simple:

Function node code...

//first, get all 4 variables into local booleans
// (as I havent seen debug output, I wont guess - instead, i'll check for string AND boolean!)
var t1_is_false  = (msg.Terr1 === false || msg.Terr1 === "false"); //will be true if "false" or false
var g1_is_false  = (msg.Gar1  === false || msg.Gar1 === "false");
var g2_is_false  = (msg.Gar2  === false || msg.Gar2 === "false");
var ht1_is_false = (msg.Ht1   === false || msg.Ht1 === "false");

//now to see if all 4 are false...
if(t1_is_false && g1_is_false && g2_is_false && ht1_is_false){
  return msg; // All 4 ARE false -  send msg to next node (i.e. keep the msg moving)
}
return null;// halt flow (dont send msg to next node)

... the function node will ONLY pass a msg out if all 4 are false - feed that to your MQTT Out nodes as required.

lastly...

As others said, use the debug window and read the help - it shows you how to access any part of a object, how to copy its exact path - and its really easy

  • I recommend you watch this playlist: Node-RED Essentials - it is by one of the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in less than an hour. A small investment for a lot of gain.
  • read this Working with messages : Node-RED
2 Likes

Thanks. But the problem is that the values dont go after the other. What I thought was if all cameras are online the alarm turns off. The state of each camera has to be stored in nodered and if it hasnt got a state it says that the camera is true and if it got signallost it store also true. Signalrestoration is false.

Actually, can you show what feeds into the "contains signalrestoration" nodes?

Add 4 debug nodes & show us what comes out of the debug

I suspect this is far easier

PS: watch those videos - you will solve this yourself if you do.

1 Like

Simplified...

When "All == signalrestoration" fires, they ALL === "signalrestoration"

I achieved this by knowing how to copy the path from debug window - that I learned from those videos I strongly recommend you watch.

The flow...

[{"id":"4505dd33.755264","type":"join","z":"e0ea8082.2b88d","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":220,"wires":[["38f06a6e.44d236","7b059be6.e93534"]]},{"id":"46ee298.98056d8","type":"inject","z":"e0ea8082.2b88d","name":"Terr1sd signallost","topic":"BlueIris/state/Terr1sd","payload":"signallost","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":120,"wires":[["4505dd33.755264"]]},{"id":"38f06a6e.44d236","type":"debug","z":"e0ea8082.2b88d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":530,"y":220,"wires":[]},{"id":"7b059be6.e93534","type":"function","z":"e0ea8082.2b88d","name":"f","func":"//first, get all 4 variables into local booleans\nvar t1_is_ok  = msg.payload[\"BlueIris/state/Terr1sd\"] === \"signalrestoration\";\nvar g1_is_ok  = msg.payload[\"BlueIris/state/Gar1sd\"]  === \"signalrestoration\";\nvar g2_is_ok  = msg.payload[\"BlueIris/state/Gar2sd\"]  === \"signalrestoration\";\nvar ht1_is_ok = msg.payload[\"BlueIris/state/Ht1sd\"]   === \"signalrestoration\";\n\n//now to see if all 4 are false...\nif(t1_is_ok && g1_is_ok && g2_is_ok && ht1_is_ok){\n    msg.payload = true;\n} else {\n    msg.payload = false;\n}\nmsg.topic = \"connectionstate\"\nreturn msg;// halt flow (dont send msg to next node)\n","outputs":1,"noerr":0,"x":470,"y":280,"wires":[["806957.832a36a8","4e8caa2a.2c0854"]]},{"id":"28705f89.68959","type":"inject","z":"e0ea8082.2b88d","name":"Terr1sd signalrestoration","topic":"BlueIris/state/Terr1sd","payload":"signalrestoration","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":80,"wires":[["4505dd33.755264"]]},{"id":"e3e3d2a5.08524","type":"inject","z":"e0ea8082.2b88d","name":"Gar1sd signallost","topic":"BlueIris/state/Gar1sd","payload":"signallost","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":220,"wires":[["4505dd33.755264"]]},{"id":"3cc48588.a285da","type":"inject","z":"e0ea8082.2b88d","name":"Gar1sd signalrestoration","topic":"BlueIris/state/Gar1sd","payload":"signalrestoration","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["4505dd33.755264"]]},{"id":"35fe39d6.7c0496","type":"inject","z":"e0ea8082.2b88d","name":"Gar2sd signallost","topic":"BlueIris/state/Gar2sd","payload":"signallost","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":320,"wires":[["4505dd33.755264"]]},{"id":"91feeb31.f6a8f8","type":"inject","z":"e0ea8082.2b88d","name":"Gar2sd signalrestoration","topic":"BlueIris/state/Gar2sd","payload":"signalrestoration","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":280,"wires":[["4505dd33.755264"]]},{"id":"1691fc6f.0bc214","type":"inject","z":"e0ea8082.2b88d","name":"Gar2sd signallost","topic":"BlueIris/state/Gar2sd","payload":"signallost","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":420,"wires":[["4505dd33.755264"]]},{"id":"5a0e0dde.9173b4","type":"inject","z":"e0ea8082.2b88d","name":"Ht1sd signalrestoration","topic":"BlueIris/state/Ht1sd","payload":"signalrestoration","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":380,"wires":[["4505dd33.755264"]]},{"id":"50aa2a0a.5f3174","type":"comment","z":"e0ea8082.2b88d","name":"Simulate MQTT","info":"","x":120,"y":40,"wires":[]},{"id":"806957.832a36a8","type":"switch","z":"e0ea8082.2b88d","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":360,"wires":[["5fd7a88d.5659e8"],["5258cec5.c066c"]]},{"id":"4e8caa2a.2c0854","type":"debug","z":"e0ea8082.2b88d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":650,"y":280,"wires":[]},{"id":"5fd7a88d.5659e8","type":"debug","z":"e0ea8082.2b88d","name":"All == signalrestoration","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":700,"y":340,"wires":[]},{"id":"5258cec5.c066c","type":"debug","z":"e0ea8082.2b88d","name":"some do not == signalrestoration","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":740,"y":380,"wires":[]}]
1 Like

Thanks! I definitely have to watch those videos. Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.