Hi.
I am trying to create a function node that determines if the current time (UTC time) falls between 2 input date time fields in home assistant.
The current time is in UTC format, however the other 2 times are in the following format; 06:30:00 etc.
Would anyone know how i can compare the times to determine if the current time falls between the 2 input times.
Regards
Danny
I find this sort of time comparison easier using moment and the isBetween() function
As you can easily enter the time format and moment will add the date portion of now.
$moment().isBetween($moment($$.payload.start, "HH:mm:ss"), $moment($$.payload.finish, "HH:mm:ss"))
Which should output a boolean true or false.
example flow
[{"id":"fdf121a121fc1e30","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"start\": \"06:30:00\", \"finish\": \"17:10:00\"}","payloadType":"json","x":270,"y":60,"wires":[["4c1b404fd574cf9b"]]},{"id":"4c1b404fd574cf9b","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().isBetween($moment($$.payload.start, \"HH:mm:ss\"), $moment($$.payload.finish, \"HH:mm:ss\"))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":100,"wires":[["825b1902aba22bc3"]]},{"id":"825b1902aba22bc3","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":60,"wires":[]}]
This is easy in a change node using JSONata, but can be done in a function node also. You can also use the expression direct in a switch node.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.