I know that title might be confusing but I couldn't think of another way to say it. I have an application where I will be receiving events from dozens (possibly hundreds) of devices. I don't care about the event if it is one-off but I do care if the same event from the same device has been seen a certain number of times in a specified time frame. As an example, here is the same event coming into Node-RED from two different devices.
In this case the event is "Motion Started" but it could be any number of things such as device offline or something else. As an example, I need something that will say "if the Motion Started events comes from the same ID 5 times in 120 seconds, then send X payload". I believe I need to do the following:
- Have a timer start each time an event comes in for a specified device.
- I don't know if this means storing the time in a DB since there could potentially be dozens or hundreds or if there is another way to do it.
- Each time an event comes in for a specified device, it needs to check if there is already a timer for that event/device combination and, if so, increment the count for that event. It will also need to start its own timer in case the time for the previous event expires before the specified time.
- If there is an existing timer and the count gets incremented to 5 (in the example above), then send X payload.
I'm struggling to figure out how to even start this. I have seen some of the counter nodes but they don't do what I need so I'm assuming I will need to use a Function node and write some JavaScript. I'm by no means a pro at JavaScript but I'm learning. I'm not looking for someone to write something for me (although, if you have something already written I would gladly take it) but more just looking for someone to point me in the right direction. I've done some Googling around but I'm coming up empty (I think that is more because of me not using the proper terminology when searching as opposed to there not being an answer).
Any help/input would be greatly appreciated.