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

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 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 :thinking:

Any help please ?

Thanks

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

[{"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":[]}]

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 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

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 :wink:

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

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

Just my 2 cents

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

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 :slightly_smiling_face:
I like that it displays the on until time, as you can see i have set this to be on until 10.00am

06

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

Thanks all for helping :+1:

1 Like