I have a message flow from a bunch of window sensors where I get open/close events. I would like to use them to give a message if a window is open for too long. For this I currently use a delay node that gets the open events and the close events are used as reset. This works if only one window is open but fails if I get open events from more than one source during the delay period as the first close event will also reset the other event.
I already have a msg.device attribute in the event that could be used to correlate the open and close events for each device but how could I implement such a correlation?
Exactly what do you want to happen if multiple windows are being opened and closed? Under what circumstance exactly to you want to generate a message?
I want to create a message for a window if there is no close message for that window within 30 minutes after the open message. Independent from other windows. Eg. if there is a window 1 open, then a few minutes later window 2 open, then window 2 close, then there should still be a message for window 1 after 30 minute. Currently the open message for window 1 gets purged by the reset message for window 2.
I would like to avoid having to duplicate the same flow 20 times to treat every window individually just because the reset in the delay is treating messages from all devices as if they where from one source.
Try this example it will respond to separate windows. It uses a trigger node and applies the trigger to each msg.topic. You do not say what your incoming message structure is, so you will have to apply my example to your message and adjust.
[{"id":"16dd4fc968ec509a","type":"inject","z":"da8a6ef0b3c9a5c8","name":"s1 open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": \"open\",\"sensor\":\"one\"}","payloadType":"json","x":90,"y":4560,"wires":[["d673297c3c6f2a0a"]]},{"id":"d673297c3c6f2a0a","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.sensor","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.event","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":4680,"wires":[["7789949f1849da48","a1759bdfe00d7bf9"]]},{"id":"153f41b6e63e3a39","type":"inject","z":"da8a6ef0b3c9a5c8","name":"s1 close","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": \"close\",\"sensor\":\"one\"}","payloadType":"json","x":100,"y":4600,"wires":[["d673297c3c6f2a0a"]]},{"id":"4afc9175da23264c","type":"inject","z":"da8a6ef0b3c9a5c8","name":"s2 open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": \"open\",\"sensor\":\"two\"}","payloadType":"json","x":90,"y":4640,"wires":[["d673297c3c6f2a0a"]]},{"id":"8e7c6a1c44f4b9e8","type":"inject","z":"da8a6ef0b3c9a5c8","name":"s2 close","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": \"close\",\"sensor\":\"two\"}","payloadType":"json","x":100,"y":4680,"wires":[["d673297c3c6f2a0a"]]},{"id":"a532635dec9c02cc","type":"inject","z":"da8a6ef0b3c9a5c8","name":"s3 open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": \"open\",\"sensor\":\"three\"}","payloadType":"json","x":90,"y":4720,"wires":[["d673297c3c6f2a0a"]]},{"id":"c5417056345c91ee","type":"inject","z":"da8a6ef0b3c9a5c8","name":"s3 close","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"event\": \"close\",\"sensor\":\"three\"}","payloadType":"json","x":100,"y":4760,"wires":[["d673297c3c6f2a0a"]]},{"id":"7789949f1849da48","type":"trigger","z":"da8a6ef0b3c9a5c8","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"10","extend":true,"overrideDelay":false,"units":"s","reset":"close","bytopic":"topic","topic":"topic","outputs":1,"x":490,"y":4680,"wires":[["a9f0bb355e75b6d0"]]},{"id":"a1759bdfe00d7bf9","type":"debug","z":"da8a6ef0b3c9a5c8","name":"open/close","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":4600,"wires":[]},{"id":"a9f0bb355e75b6d0","type":"template","z":"da8a6ef0b3c9a5c8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"window {{topic}} still {{payload}} !","output":"str","x":660,"y":4680,"wires":[["499a07b9f423a89f"]]},{"id":"499a07b9f423a89f","type":"debug","z":"da8a6ef0b3c9a5c8","name":"open longer than trigger delay","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":4740,"wires":[]}]
This is the key. Each topic is handled separately, including the reset. You have a lot of flexibility in how you set up topics and payloads from each of the sensors.
I was able to adjust my flow to use the trigger node as you suggested but I struggle with the reset. I have adjusted the trigger to work on the msg.device attribute which in my case contains the name of the device but the events with "close" payload seem to do nothing, the window open reminder message is sent after the interval regardless if the window is still open or if it was closed in the meantime.
Do I need to set msg.reset on the close events? Your example did not do that as far as I can see:
Again i do not know your incoming messages, please provide example flow with example incoming messages in several inject nodes, as i did. Then we can maybe see your issue.
My problem seems to come from MQTT input messages with persistence flag that mess up the rest of the logic. The part from your example works fine. Thanks!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.