Assistance with Flow

As a non-programmer, I'm struggling with a simple flow and would really appreciate some help.

I have a sensor that (through MQTT) causes a msg to be sent when a window is opened.
I want to create this:

  • If the window is opened by 11am, let the msg through.
  • If the window is NOT opened by 11am, send a different message.

I've been trying with the Moment Node, but I'm going around in circles.

Can someone help me please?

First you must make sure the requirement is clear. Do you mean that at 11am it is open, or do you mean that an open message is received some time between some time that you have not defined and 11:00?

Thanks for responding, Colin.

IF it is opened BY 11am, simply allow the message that it has been opened to go through. (I don't care what time the message goes through if it's before 11am.)

IF it not NOT opened BY 11am, send a different message (that different message will be that the window has not been opened by 11am)

Is that sufficiently clear?

Edit - I have been too quick to write code in function nodes, so as an exercise I revised this to get rid of them all.
I think you need to use a context variable to store the fact that a "window was opened" message has arrived today.
Hopefully this works:

[{"id":"d7b0203.90d37e","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d9f3808d.c4ace8","type":"group","z":"d7b0203.90d37e","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["a92532d7.894f9","72294f47.69796","add41f94.2b58a","e1d652fb.00f47"],"x":14,"y":219,"w":572,"h":122},{"id":"785db242.dc1b7c","type":"inject","z":"d7b0203.90d37e","name":"At 11:00","props":[{"p":"payload"}],"repeat":"","crontab":"00 11 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":100,"wires":[["2902cb0.878a7b6","955bf8cb.f55f4"]]},{"id":"f3d60ef3.fb9c48","type":"debug","z":"d7b0203.90d37e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":120,"wires":[]},{"id":"c3c56327.76623","type":"mqtt in","z":"d7b0203.90d37e","name":"","topic":"window/open","qos":"2","datatype":"auto","broker":"4c682b3a.2ab5c4","nl":false,"rap":true,"rh":0,"x":90,"y":180,"wires":[["a8d980ee.7b15"]]},{"id":"131d7ac5.34188d","type":"debug","z":"d7b0203.90d37e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"7a57e4a0.34ddb4","type":"inject","z":"d7b0203.90d37e","name":"At 00:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":40,"wires":[["5f363354.716814"]]},{"id":"5f363354.716814","type":"change","z":"d7b0203.90d37e","name":"flow.windowMsg = false","rules":[{"t":"set","p":"windowMsg","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":40,"wires":[[]]},{"id":"2902cb0.878a7b6","type":"change","z":"d7b0203.90d37e","name":"flow.triggerTime = now","rules":[{"t":"set","p":"triggerTime","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":80,"wires":[[]]},{"id":"a8d980ee.7b15","type":"change","z":"d7b0203.90d37e","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":180,"wires":[["39b6204f.d87cf8"]]},{"id":"2f122b9d.1b931c","type":"change","z":"d7b0203.90d37e","name":"flow.windowMsg = true","rules":[{"t":"set","p":"windowMsg","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":180,"wires":[["131d7ac5.34188d"]]},{"id":"39b6204f.d87cf8","type":"switch","z":"d7b0203.90d37e","name":"Before triggerTime?","property":"timestamp","propertyType":"msg","rules":[{"t":"lte","v":"triggerTime","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":180,"wires":[["2f122b9d.1b931c"]]},{"id":"a92532d7.894f9","type":"inject","z":"d7b0203.90d37e","g":"d9f3808d.c4ace8","name":"Test - set triggerTime now + 20sec","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":300,"wires":[["72294f47.69796"]]},{"id":"72294f47.69796","type":"function","z":"d7b0203.90d37e","g":"d9f3808d.c4ace8","name":"","func":"flow.set('triggerTime', msg.payload + 20000);\nflow.set('windowMsg', false);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":300,"wires":[[]]},{"id":"add41f94.2b58a","type":"inject","z":"d7b0203.90d37e","g":"d9f3808d.c4ace8","name":"","props":[{"p":"payload"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test","payloadType":"str","x":110,"y":260,"wires":[["a8d980ee.7b15"]]},{"id":"e1d652fb.00f47","type":"comment","z":"d7b0203.90d37e","g":"d9f3808d.c4ace8","name":"Nodes for testing","info":"","x":280,"y":260,"wires":[]},{"id":"a58a32f7.948a6","type":"comment","z":"d7b0203.90d37e","name":"Midnight - reset windowMsg","info":"","x":140,"y":40,"wires":[]},{"id":"b4e0b175.ab7fa8","type":"comment","z":"d7b0203.90d37e","name":"This inject defines triggerTime","info":"","x":140,"y":100,"wires":[]},{"id":"955bf8cb.f55f4","type":"switch","z":"d7b0203.90d37e","name":"windowMsg false?","property":"windowMsg","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":550,"y":120,"wires":[["9072ac08.77c3f"]]},{"id":"9072ac08.77c3f","type":"change","z":"d7b0203.90d37e","name":"Not Opened","rules":[{"t":"set","p":"payload","pt":"msg","to":"window was not opened before trigger time","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":120,"wires":[["f3d60ef3.fb9c48"]]},{"id":"4c682b3a.2ab5c4","type":"mqtt-broker","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Nice flow @jbudd

I think this could be simplified with a simple trigger node.

Two inputs to the node

  1. Inject at (say) 12:01AM to set the trigger for the day
  2. MQTT in from the WIndow Open event

Output from the trigger node - if it receives nothing for 10:59 hours - it sends a message saying so.

If it receives some from MQTT it can then forward that on.

Something like this

[{"id":"6d04f1ed.b2b82","type":"within-time-switch","z":"71854c3.fbc71b4","name":"Check if something received before 11AM","nameInt":"","positionConfig":"e2e4f670.cd19a8","startTime":"00:02","startTimeType":"entered","startOffset":0,"startOffsetType":"none","startOffsetMultiplier":60000,"endTime":"11:00","endTimeType":"entered","endOffset":0,"endOffsetType":"none","endOffsetMultiplier":60000,"timeDays":"*","timeOnlyOddDays":false,"timeOnlyEvenDays":false,"timeMonths":"*","timedatestart":"","timedateend":"","propertyStart":"","propertyStartType":"none","propertyStartCompare":"true","propertyStartThreshold":"","propertyStartThresholdType":"num","startTimeAlt":"","startTimeAltType":"entered","startOffsetAlt":0,"startOffsetAltType":"none","startOffsetAltMultiplier":60000,"propertyEnd":"","propertyEndType":"none","propertyEndCompare":"true","propertyEndThreshold":"","propertyEndThresholdType":"num","endTimeAlt":"","endTimeAltType":"entered","endOffsetAlt":0,"endOffsetAltType":"none","endOffsetAltMultiplier":60000,"tsCompare":"0","x":860,"y":160,"wires":[["954aa52f.3f00b8"],["51f5b1f2.81425"]]},{"id":"914b71f7.1e782","type":"trigger","z":"71854c3.fbc71b4","name":"Wait until 11AM","op1":"Start","op2":"Not Opened","op1type":"str","op2type":"str","duration":"659","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":2,"x":520,"y":180,"wires":[["6d04f1ed.b2b82"],["6d04f1ed.b2b82"]]},{"id":"4e79e965.1db508","type":"inject","z":"71854c3.fbc71b4","name":"Inject to looad trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"Start","payloadType":"str","x":290,"y":180,"wires":[["914b71f7.1e782"]]},{"id":"411a355d.c7db3c","type":"mqtt in","z":"71854c3.fbc71b4","name":"WIndow Opened","topic":"","qos":"2","datatype":"auto","broker":"b481a262.9f86e","nl":false,"rap":true,"rh":0,"x":290,"y":260,"wires":[["914b71f7.1e782"]]},{"id":"954aa52f.3f00b8","type":"debug","z":"71854c3.fbc71b4","name":"MSG Received before 11AM","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":140,"wires":[]},{"id":"51f5b1f2.81425","type":"debug","z":"71854c3.fbc71b4","name":"MSG Not Received before 11AM","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":200,"wires":[]},{"id":"e2e4f670.cd19a8","type":"position-config","name":"Sydney - Castle Cove","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"},{"id":"b481a262.9f86e","type":"mqtt-broker","name":"Craigs Laptop","broker":"192.168.1.134","port":"1883","clientid":"NR-HA","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Actually - just did more testing and my solution is not quite there - so better use @jbudd s flow

Craig

My attempt has a major problem too.

If flow.triggerTime is not set at all (at startup) then no messages will be passed through until the next day after the inject at 11:00. This problem will recur after every Deploy.

Very annoying because I can't think of a clean solution which avoids a) littering the flow with multiple references to the time and b) avoids using a JS date, with it's worries about daylight saving time.

Many thanks to all of you for your attempts. If I can take one encouraging thing from it, it's that this isn't as simple a problem as it appears on the surface. I spent hours trying to get it to work, without success. I'll play around with your solutions and see if I can figure something out.

Take a look at node-red-contrib-simple-gate.

Thanks. Will do. Right now I'm looking at your solution above. Learning a lot from it!

Curious how you put the box around the Nodes for Testing....

Craig was almost there -

[{"id":"914b71f7.1e782","type":"trigger","z":"ba9351cf.2e3e1","name":"Wait until 11AM","op1":"","op2":"Not Opened by 11","op1type":"nul","op2type":"str","duration":"659","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":580,"y":320,"wires":[["954aa52f.3f00b8"]]},{"id":"4e79e965.1db508","type":"inject","z":"ba9351cf.2e3e1","name":"Inject to load trigger at midnight","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"Start","payloadType":"str","x":230,"y":320,"wires":[["914b71f7.1e782"]]},{"id":"411a355d.c7db3c","type":"mqtt in","z":"ba9351cf.2e3e1","name":"WIndow Opened","topic":"","qos":"2","datatype":"auto","broker":"b481a262.9f86e","nl":false,"rap":true,"rh":0,"x":180,"y":420,"wires":[["954aa52f.3f00b8","1ae4ac591a7a3770"]]},{"id":"954aa52f.3f00b8","type":"debug","z":"ba9351cf.2e3e1","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":420,"wires":[]},{"id":"1ae4ac591a7a3770","type":"change","z":"ba9351cf.2e3e1","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":380,"wires":[["914b71f7.1e782"]]},{"id":"b481a262.9f86e","type":"mqtt-broker","name":"Craigs Laptop","broker":"192.168.1.134","port":"1883","clientid":"NR-HA","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

(you don't mention if you care about window opening after 11...)

To group nodes, select the ones you want - then menu - Groups - group selection ...

1 Like

I prostrate my self humbly at the feet of our wise and esteemed leader !!

Well done !!

I was going to try and come back to it later tonight !

Craig

Phew! easy when you know how!

But how would you reject window opened messages after 11?

As I said - there was no mention of blocking that in the requirements - so yes in my MVP they go through :slight_smile:

depends what the window opened signal is... if it is (or can be made to be) just a static value (eg a fixed text message) then an RBE node can filter out duplicates and the midnight inject can also send a reset to unblock the rbe.

[{"id":"914b71f7.1e782","type":"trigger","z":"ba9351cf.2e3e1","name":"Wait until 11AM","op1":"","op2":"Not Opened by 11","op1type":"nul","op2type":"str","duration":"659","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":640,"y":320,"wires":[["954aa52f.3f00b8"]]},{"id":"4e79e965.1db508","type":"inject","z":"ba9351cf.2e3e1","name":"Inject to load trigger at midnight \\n and reset rbe","props":[{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"topic":"","x":280,"y":320,"wires":[["914b71f7.1e782","ad307483fecbcf35"]]},{"id":"411a355d.c7db3c","type":"mqtt in","z":"ba9351cf.2e3e1","name":"WIndow Opened","topic":"open","qos":"2","datatype":"auto","broker":"b481a262.9f86e","nl":false,"rap":true,"rh":0,"x":240,"y":420,"wires":[["1ae4ac591a7a3770","ad307483fecbcf35"]]},{"id":"954aa52f.3f00b8","type":"debug","z":"ba9351cf.2e3e1","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":420,"wires":[]},{"id":"1ae4ac591a7a3770","type":"change","z":"ba9351cf.2e3e1","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":380,"wires":[["914b71f7.1e782"]]},{"id":"ad307483fecbcf35","type":"rbe","z":"ba9351cf.2e3e1","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":570,"y":420,"wires":[["954aa52f.3f00b8"]]},{"id":"b481a262.9f86e","type":"mqtt-broker","name":"Craigs Laptop","broker":"192.168.1.134","port":"1883","clientid":"NR-HA","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Many thanks to everyone for your help. Really appreciate it. I'm going to spend the weekend examining all your attempts to understand what worked and didn't work. Great learning experience.

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