# Enable my node between certain times using something like Jsonata or a function node?

**URL:** <https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077>\
**Category:** General\
**Created:** [18 May 2024 09:11 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077 "2024-05-18T09:11:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [18 May 2024 09:11 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/1 "2024-05-18T09:11:10Z")

</div>

Hi,

I have flows that enable speech and warning me of a low battery voltage.  
I want this to be only be enabled **between** 09:10 and 10:30 every day.  
After this I want it to disabled.  
This has to operate even if I reboot or restart Node-RED.

I am using [node-red-contrib-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate) to control the enabling or disabling of the speech.  
So all i need to do is send the text **open** or **close** at the correct time.

I have looked at the Node-RED **Switch node** and it does have \>= and also \<= to compare, so could that be used with a jsonata expression or perhaps a function node, but I do not know anything about jsonata 🤔

Any help please ?

Thanks

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 May 2024 09:30 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/2 "2024-05-18T09:30:52Z")

</div>

Should be possible using standard nodes and JSONata in a switch node. The switch node will only pass when time is between the set context `time_limits`.  
e.g

```auto
[{"id":"a1d504dab3b9825b","type":"inject","z":"d1395164b4eec73e","name":"set limit 9:10 - 23:30","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"start\": 910, \"end\":2330}","payloadType":"json","x":150,"y":4600,"wires":[["b43d8e7549697d4f"]]},{"id":"b43d8e7549697d4f","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"time_limits","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":4600,"wires":[[]]},{"id":"318797728cc53fc0","type":"inject","z":"d1395164b4eec73e","name":"set limits 9:10 9:30","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"start\": 910, \"end\":930}","payloadType":"json","x":150,"y":4640,"wires":[["b43d8e7549697d4f"]]},{"id":"c776b52a87ddc2dd","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":4740,"wires":[["f1f1945150f4cc16"]]},{"id":"f1f1945150f4cc16","type":"switch","z":"d1395164b4eec73e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"(\t $limits := $flowContext(\"time_limits\");\t $now := $number($moment().format(\"Hmm\"));\t $now >= $limits.start and $now <= $limits.end\t)","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":4740,"wires":[["8be976c58cd74b30"]]},{"id":"8be976c58cd74b30","type":"debug","z":"d1395164b4eec73e","name":"debug 2489","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":4740,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [18 May 2024 09:59 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/4 "2024-05-18T09:59:25Z")

</div>

> [@questuk](#):
>
> I am using [node-red-contrib-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate) to control the enabling or disabling of the speech

Have you considered an alternative approach.

Continually save the battery voltage to context, so that the context will store the most recent value at all times ([using file context](https://nodered.org/docs/user-guide/context) so that it will survive a reboot)

Use an inject node configured to retrieve and inject the context value at say 10am every day, which is fed into a switch node to determine if the battery voltage is low.  
The output from that would be used to send your voice message.

![message](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/236409e7ac911821ed6f9da7a13bbf04963d6d37.jpeg)

The advantages of this approach would be that is would survive a reboot, you would get just one voice message instead of nagging you for an hour, and the flow would be easy to maintain.

Just a thought to consider 😉

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [18 May 2024 10:19 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/5 "2024-05-18T10:19:52Z")

</div>

Hi to both of you,

I was using this as an example in actual fact there are about 15 gates that need to be controller at various times of day.

Some are about battery and some are about rooms overheating etc.  
I have been using this setup that works well, unless i do a reboot after 10:30 of Node-RED, then the gates will not be correct until the next day. ( Because on a reboot of Node-RED, i have it set up that all gates will be open )

Thanks

 ![04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad39a0a73d2928e4a16d3ddd25feab133da90083.png)

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [18 May 2024 10:41 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/6 "2024-05-18T10:41:11Z")

</div>

There are more ways to solve your problem.  
I'm using the [node-red-contrib-light-scheduler (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-light-scheduler) node for this.  
Easy to use, multiply time slots per day etc.

Just my 2 cents

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [18 May 2024 23:08 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/7 "2024-05-18T23:08:52Z")

</div>

You could just instal this node and put it before the speech output

> **[node-red-contrib-time-range-switch](https://flows.nodered.org/node/node-red-contrib-time-range-switch)**
>
> A simple Node-RED node that routes messages depending on the time. If the current time falls within the range specified in the node configuration, the message is routed to output 1. Otherwise the message is routed to output 2.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [19 May 2024 08:15 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/8 "2024-05-19T08:15:57Z")

</div>

Thanks everyone,

As this wasn't as easy as I thought to use a function node for this, I decided to look at other nodes that would do what I needed.

This one does exactly what I want and is very simple, it just does one job and nothing complicated 🙂  
I like that it displays the **on until** time, as you can see i have set this to be on until 10.00am

![06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3eb8973761e136bf919c13530da510b900b6a9ef.png)

> **[node-red-contrib-timerswitch](https://flows.nodered.org/node/node-red-contrib-timerswitch)**
>
> An easy, intuitive and precise node-red timer that allows you to turn something on/off multiple times a day and supports schedules as low as 1 second.

Just running a test with this over the next few days to make sure I have no problems.

Thanks all for helping 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 August 2024 08:16 UTC](https://discourse.nodered.org/t/enable-my-node-between-certain-times-using-something-like-jsonata-or-a-function-node/88077/9 "2024-08-17T08:16:10Z")

</div>

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