Help with logic switching

Hi, Im very new to NodeRed and as a result I am stuck on the following:

Im trying to create a series of timers to select different radio station during the day that will turn off and on set by the timers and also by presence.

The problem Im having is with the switching of the amp on and off.

If we look at the 'off' output, Im thinking there needs to be some sort of AND gate from the switch "off' outputs to the amp, so when all switches are off the amp turns off.
The problem with that is the 'off' outputs from the timers are not re-triggered if a timer is already off, meaning a AND gate would only receive one 'off' input at a particle time. From what I understand a AND gate would need all inputs trigged to output a trigger.

Theres probable loads of different ways of doing this but Im just not sure so any help with this would be appreciated

Ive included a mock up of part of my flow where the trouble is.

[{"id":"a9307e4b.57c1","type":"light-scheduler","z":"8c0817a6.a3ccd8","settings":"9da02f7b.19039","events":"[{\"start\":{\"dow\":6,\"mod\":360},\"end\":{\"dow\":6,\"mod\":720}}]","topic":"","name":"Timer 1 6:00  - 12:00","onPayload":"ON","onPayloadType":"str","offPayload":"OFF","offPayloadType":"str","onlyWhenDark":false,"scheduleRndMax":0,"sunElevationThreshold":6,"sunShowElevationInStatus":false,"outputfreq":"output.statechange.startup","x":260,"y":420,"wires":[["cb6fe6f8.55306"]]},{"id":"c1b8fadf.4a0ae8","type":"light-scheduler","z":"8c0817a6.a3ccd8","settings":"9da02f7b.19039","events":"[{\"start\":{\"dow\":6,\"mod\":720},\"end\":{\"dow\":6,\"mod\":960}}]","topic":"","name":"Timer 2 12:00 - 16:00","onPayload":"ON","onPayloadType":"str","offPayload":"OFF","offPayloadType":"str","onlyWhenDark":false,"scheduleRndMax":0,"sunElevationThreshold":6,"sunShowElevationInStatus":false,"outputfreq":"output.statechange.startup","x":260,"y":480,"wires":[["57c9cf8c.1aa1e8"]]},{"id":"1c6cc6ac.92b231","type":"light-scheduler","z":"8c0817a6.a3ccd8","settings":"9da02f7b.19039","events":"[{\"start\":{\"dow\":6,\"mod\":960},\"end\":{\"dow\":6,\"mod\":1260}}]","topic":"","name":"Timer 3 16:00 - 21:00","onPayload":"ON","onPayloadType":"str","offPayload":"OFF","offPayloadType":"str","onlyWhenDark":false,"scheduleRndMax":0,"sunElevationThreshold":6,"sunShowElevationInStatus":false,"outputfreq":"output.statechange.startup","x":260,"y":540,"wires":[["5b722875.af545"]]},{"id":"cb6fe6f8.55306","type":"switch","z":"8c0817a6.a3ccd8","name":"Switch 1","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":440,"y":420,"wires":[["8ca3bcda.a30e48","ea2b833c.d0df6"],["2cf23dd5.c0e91a","8ca3bcda.a30e48","9f6c3072.ef0228"]]},{"id":"57c9cf8c.1aa1e8","type":"switch","z":"8c0817a6.a3ccd8","name":"Switch 2","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":440,"y":480,"wires":[["73d7984.d9b97e8","ea2b833c.d0df6"],["2cf23dd5.c0e91a","73d7984.d9b97e8","9f6c3072.ef0228"]]},{"id":"5b722875.af545","type":"switch","z":"8c0817a6.a3ccd8","name":"Switch 3","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":440,"y":540,"wires":[["5cf5482e.25453","ea2b833c.d0df6"],["2cf23dd5.c0e91a","5cf5482e.25453","9f6c3072.ef0228"]]},{"id":"facdeb03.8bea5","type":"comment","z":"8c0817a6.a3ccd8","name":"Streams a radio station 1","info":"","x":870,"y":420,"wires":[]},{"id":"58212c2b.6e2da4","type":"comment","z":"8c0817a6.a3ccd8","name":"Streams a radio station 3","info":"","x":870,"y":545,"wires":[]},{"id":"2e7ff36a.a23f5c","type":"comment","z":"8c0817a6.a3ccd8","name":"Streams a radio station 2","info":"","x":870,"y":480,"wires":[]},{"id":"8ca3bcda.a30e48","type":"link out","z":"8c0817a6.a3ccd8","name":"","links":[],"x":745,"y":420,"wires":[]},{"id":"73d7984.d9b97e8","type":"link out","z":"8c0817a6.a3ccd8","name":"","links":[],"x":745,"y":480,"wires":[]},{"id":"5cf5482e.25453","type":"link out","z":"8c0817a6.a3ccd8","name":"","links":[],"x":745,"y":545,"wires":[]},{"id":"4da9d54f.9c0704","type":"comment","z":"8c0817a6.a3ccd8","name":"Amp on","info":"","x":830,"y":280,"wires":[]},{"id":"ea2b833c.d0df6","type":"link out","z":"8c0817a6.a3ccd8","name":"","links":[],"x":765,"y":280,"wires":[]},{"id":"756f9767.95faf","type":"comment","z":"8c0817a6.a3ccd8","name":"Amp off","info":"","x":830,"y":650,"wires":[]},{"id":"2cf23dd5.c0e91a","type":"link out","z":"8c0817a6.a3ccd8","name":"","links":[],"x":765,"y":650,"wires":[]},{"id":"acacce9.429543","type":"comment","z":"8c0817a6.a3ccd8","name":"input to timers is set by presence detection","info":"","x":200,"y":306,"wires":[]},{"id":"aa38c16e.009c38","type":"comment","z":"8c0817a6.a3ccd8","name":"and outputs 'off' if away and 'auto' if home","info":"","x":200,"y":336,"wires":[]},{"id":"2126f819.a24de","type":"comment","z":"8c0817a6.a3ccd8","name":"to reset timers","info":"","x":110,"y":366,"wires":[]},{"id":"51432cb7.3b6074","type":"inject","z":"8c0817a6.a3ccd8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":90,"y":650,"wires":[["a9307e4b.57c1","c1b8fadf.4a0ae8","1c6cc6ac.92b231"]]},{"id":"bdfbbd10.c3cca8","type":"inject","z":"8c0817a6.a3ccd8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"auto","payloadType":"str","x":90,"y":690,"wires":[["1c6cc6ac.92b231","c1b8fadf.4a0ae8","a9307e4b.57c1"]]},{"id":"9f6c3072.ef0228","type":"debug","z":"8c0817a6.a3ccd8","name":"off output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":720,"wires":[]},{"id":"ed9ba9e4.068798","type":"comment","z":"8c0817a6.a3ccd8","name":"Simulate presence detection","info":"","x":130,"y":730,"wires":[]},{"id":"9da02f7b.19039","type":"light-scheduler-settings","z":"","name":"my settings","latitude":"38.8977","longitude":"77.0365"}]

I hope Ive explains myself clearly

Welcome to the forum @trevor.

I believe node-red-contrib-bool-gate will do what you want. Make sure each of the input values that you want to combine together has a unique topic and the node will remember the most recent value for each topic and give you the answer.
In fact I think you may want an OR condition to turn it on when any of the switches are on.

Thank you Colin, I did try your contrib you refer to and yes I would need an OR gate for the 'on' condition but could get it to work putting it down to the inputs not changing together. Didn't realise the gate will remember its most recent value.

I will have another look at this. Thanks again.

The key thing is to make sure each input has a different topic. It has to remember the previous value because all nodes only get one message at a time, they never come in at the same time.

1 Like

I tried so many times to get the logic working after going through old post etc. I knew it was reliant on the topic but was and having no luck at all hence my cry for help.

It was this last comment that help clear thing up for me. Help with logic switching

Got it all working now. Thanks for your help Colin

1 Like

Just a follow up on the above.

After testing Im finding the 'OR' gate I have set up works but as I have 3 inputs 'ON', OFF' and 'OFF', when I trigger those 3 inputs at the same time, I get three output signals showing in the debug node.

The 3 inputs to the 'OR' gate from the timers are:

25/08/2020, 20:48:39 node: f8714e30.9836c
{ topic: "timer 1", payload: "ON", _msgid: "ad7cbdc7.cdb76" }

25/08/2020, 20:48:39 node: 33369e56.1874fa
{ topic: "timer 2", payload: "OFF", _msgid: "dc54c100.41e1a" }

25/08/2020, 20:48:39 node: b41a7be0.ebe1f
{ topic: "timer 3", payload: "OFF", _msgid: "49a1c471.c95e5c" }

The output on the debug node from the 'OR' gate shows:

25/08/2020, 20:48:39 node: c0059dd8.11241
{ topic: null, payload: "ON", bool: true, _msgid: "b6b24a46.d75968" }

25/08/2020, 20:48:39 node: c0059dd8.11241
{ topic: null, payload: "OFF", bool: true, _msgid: "fa3ee084.851f6" }

25/08/2020, 20:48:39 node: c0059dd8.11241
{ topic: null, payload: "OFF", bool: true, _msgid: "e9653246.64eb9" }

Is there anyway to just get the one 'true' result from the 'OR' gate?

The setting I have for the 'OR' gate is:

[{"id":"58712c7a.e03f44","type":"or-gate","z":"d45048b2.cf6ac8","name":"","rules":[{"t":"eq","v":"ON","vt":"str","propertyType":"msg","property":"payload","topic":"timer 1"},{"t":"eq","v":"ON","vt":"str","propertyType":"msg","property":"payload","topic":"timer 2"},{"t":"eq","v":"ON","vt":"str","propertyType":"msg","property":"payload","topic":"timer 3"}],"outputTopic":"","gateType":"or","emitOnlyIfTrue":true,"x":680,"y":310,"wires":[[]]}]

Regards Trevor

If you expect all three inputs to appear at approximately the same time then you can feed the output into a Trigger node set to Send Nothing then after, say 0.1 seconds send the Latest Message, with Extend Delay set. Then you will only get the last message passed on. Alternatively, if you are extracting the bool value to the payload, for example, then you could add an RBE node after that so it will only pass on a message when the value changes.

[Edit] I see that you have selected that only true messages should be generated, so my RBE suggestion won't work.

Thank you, the triggers seem to have worked in cleaning up the message and making the flow more stable.

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