# Looking for something to happen within a time range

**URL:** <https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423>\
**Category:** General\
**Created:** [21 December 2019 14:34 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423 "2019-12-21T14:34:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RugbyAl](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@RugbyAl](https://discourse.nodered.org/u/RugbyAl)\
**Post date:** [21 December 2019 14:34 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/1 "2019-12-21T14:34:25Z")

</div>

Hi. Impressed with Node Red, but can't get my mind round how to do this -  
I want to trigger an event if the kettle (monitored by POW2) has not been used between 06:00 and 08:00 - that would be an anomaly.  
Any guidance / ideas please?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 December 2019 14:56 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/2 "2019-12-21T14:56:00Z")

</div>

To achieve that I would keep track of each time the kettle is used (probably I would save the last time in a function node's context) then at 0800 use an inject node to trigger a check on when it was last used. If that is more than two hours ago then send a message to take appropriate action.

---

<div class="post-metadata">

**Author:** ![RugbyAl](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@RugbyAl](https://discourse.nodered.org/u/RugbyAl)\
**Post date:** [21 December 2019 15:11 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/3 "2019-12-21T15:11:49Z")

</div>

Thanks Colin, hadn't thought of the logic in that way, I'll give it a go.

---

<div class="post-metadata">

**Author:** ![RugbyAl](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@RugbyAl](https://discourse.nodered.org/u/RugbyAl)\
**Post date:** [21 December 2019 19:05 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/4 "2019-12-21T19:05:56Z")

</div>

Looking at function node, can't see how to save last time in context? Could you expand on this please Colin?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 December 2019 21:09 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/5 "2019-12-21T21:09:45Z")

</div>

Have a look at the [docs on writing functions](https://nodered.org/docs/user-guide/writing-functions), in particular the section on storing data (but read all the rest if you have not already done so). The current date/time is available using something like  
`let now = new Date()`  
You can use different values of `topic` to distinguish between kettle operations and the input from the inject node.

---

<div class="post-metadata">

**Author:** ![RugbyAl](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@RugbyAl](https://discourse.nodered.org/u/RugbyAl)\
**Post date:** [21 December 2019 21:26 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/6 "2019-12-21T21:26:10Z")

</div>

Thanks Colin. That's my Christmas project sorted. Need some strong coffee and quiet time.

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [22 December 2019 06:02 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/7 "2019-12-22T06:02:23Z")

</div>

Different way of thinking.

- At 6AM set a flag to warning (perhaps an mqtt topic called node-red/flag)
- Any time the kettle is used the set the flag to normal
- Check flag at 8AM if the flag is not normal set it to anomaly
- Another flow can monitor flag (subscribe to node-red/flag). Perhaps a notification is set (email, tweet, txt, traffic light that follows the flag ...)

I use MQTT to allow multiple applications to use the information without interfering with each other.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 December 2019 08:32 UTC](https://discourse.nodered.org/t/looking-for-something-to-happen-within-a-time-range/19423/8 "2019-12-22T08:32:47Z")

</div>

Or....  
At 6 fire a trigger node to send nothing right away, and its second output 2 hours later (8am). If the kettle is detected send a msg.reset to it to clear the waiting warning.
