Compare timestamps

Hi,

I want to start a flow based on the time set in an input_datetime, which can be changed via the HA GUI.
So I need to compare sensor.time with input_datetime.pool_start_time but it is always true.
I’m comparing state_type string, which is not working.
Isn’t there a state_type timestamp?
Or even a complete different solution to start the flow based on an input_datetime.

Here is my folw

[{"id":"cfd380fa.130c98","type":"api-current-state","z":"964574f3.5f4d","name":"","server":"969aa5f8.2b8818","version":1,"outputs":2,"halt_if":"payload","halt_if_type":"msg","halt_if_compare":"is","override_topic":false,"entity_id":"input_datetime.pool_start_time","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":590,"y":220,"wires":[["a99b2c03.c88488"],["ad8fc635.4920f"]]},{"id":"969aa5f8.2b8818","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

update:
I changed the “State Location” from the surrent state node to a different msg. the pyload, now it’s actually comparing sensor.time with input_datetime.pool_start_time
but it’s always false as the sting from the sensor.time is 23:54 and the input_datetime.xxx is 23:54:00
which is not the same…
So the flow is now working as it should but not as I wanted it to.

the new folw

[{"id":"cfd380fa.130c98","type":"api-current-state","z":"964574f3.5f4d","name":"","server":"969aa5f8.2b8818","version":1,"outputs":2,"halt_if":"payload","halt_if_type":"msg","halt_if_compare":"is","override_topic":false,"entity_id":"input_datetime.pool_start_time","state_type":"str","state_location":"pool_start_time","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":590,"y":220,"wires":[["a99b2c03.c88488"],["ad8fc635.4920f"]]},{"id":"969aa5f8.2b8818","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

cheers

I am using a slightly different way to achieve the same. Maybe it works for you too:

For me I want it to trigger 10min earlier, but you can simply just remove this.
It is triggered by a timestamp on interval every minute.

[{"id":"5fd4bf94.3d203","type":"join","z":"1de400c0.5ce76f","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":810,"y":180,"wires":[["3baa06b3.7b18fa"]]},{"id":"3baa06b3.7b18fa","type":"function","z":"1de400c0.5ce76f","name":"Compare Times","func":"newmsg = {};\nif (msg.payload[0] == msg.payload[1]) {\n    newmsg.payload = \"True\";\n} else {\n    newmsg.payload = \"False\";\n}\n\nreturn newmsg;","outputs":1,"noerr":0,"x":960,"y":180,"wires":[["69f8941b.a2457c"]]},{"id":"69f8941b.a2457c","type":"switch","z":"1de400c0.5ce76f","name":"Is it Time?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1120,"y":180,"wires":[["1123ea0c.e5c056"]]},{"id":"cb3ff133.0f556","type":"api-current-state","z":"1de400c0.5ce76f","name":"Get Alarm Time","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"input_datetime.alarm","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":460,"y":120,"wires":[["ed1570d4.2671d"]]},{"id":"ed1570d4.2671d","type":"moment","z":"1de400c0.5ce76f","name":"Alarm - 10min","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Berlin","adjAmount":"10","adjType":"minutes","adjDir":"subtract","format":"YYYY-MM-DD HH:mm","locale":"C","output":"payload","outputType":"msg","outTz":"Europe/Berlin","x":640,"y":120,"wires":[["5fd4bf94.3d203"]]},{"id":"c2b4d347.fde2","type":"moment","z":"1de400c0.5ce76f","name":"Current Time","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Berlin","adjAmount":"0","adjType":"minutes","adjDir":"subtract","format":"YYYY-MM-DD HH:mm","locale":"C","output":"payload","outputType":"msg","outTz":"Europe/Berlin","x":630,"y":180,"wires":[["5fd4bf94.3d203"]]}]
1 Like

with you input I managed to kick of my flow based on input_datetime.

here is the working flow

[{"id":"a43add40.62c578","type":"server-state-changed","z":"964574f3.5f4d","name":"time now","server":"969aa5f8.2b8818","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.time","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":580,"y":280,"wires":[["841c069f.dd4b7","cfd380fa.130c98"]]},{"id":"cfd380fa.130c98","type":"api-current-state","z":"964574f3.5f4d","name":"pool start time","server":"969aa5f8.2b8818","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_datetime.pool_start_time","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":780,"y":320,"wires":[["5f2e789e.2866f"]]},{"id":"841c069f.dd4b7","type":"moment","z":"964574f3.5f4d","name":"format time","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Vienna","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm","locale":"de_AT","output":"payload","outputType":"msg","outTz":"Europe/Vienna","x":770,"y":240,"wires":[["f64643f4.ef03a8"]]},{"id":"5f2e789e.2866f","type":"moment","z":"964574f3.5f4d","name":"format time","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Vienna","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm","locale":"de_AT","output":"payload","outputType":"msg","outTz":"Europe/Vienna","x":990,"y":320,"wires":[["f64643f4.ef03a8"]]},{"id":"f64643f4.ef03a8","type":"join","z":"964574f3.5f4d","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1150,"y":280,"wires":[["a58e955.579ff68"]]},{"id":"a58e955.579ff68","type":"function","z":"964574f3.5f4d","name":"Compare Times","func":"newmsg = {};\nif (msg.payload[0] == msg.payload[1]) {\n    newmsg.payload = \"True\";\n} else {\n    newmsg.payload = \"False\";\n}\n\nreturn newmsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1320,"y":280,"wires":[["48cfa9fa.20b99"]]},{"id":"48cfa9fa.20b99","type":"switch","z":"964574f3.5f4d","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1490,"y":280,"wires":[["9d53a303.8b1ce8"]]},{"id":"9d53a303.8b1ce8","type":"api-call-service","z":"964574f3.5f4d","name":"start pool pump","server":"969aa5f8.2b8818","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.pool_pump","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1660,"y":280,"wires":[[]]},{"id":"969aa5f8.2b8818","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

thanks a lot!

I am fairly new to HA and Node Red, and working on setting up the home alarm integration, and creating notifications to email, MMS text, fire tv, mobile app, and TTS. I was wanting to add a way to prevent getting multiple alerts in a row by adding a “hold” time. You can create multiple message variables/properties that will pass through all the flow nodes if you don’t change them. Using your tips above, I came up with a slightly different way to compare 2 times, eliminating the join node. I set the Current Time, and other properties, in the initial state change node. Then I have an input_datetime entity that I check for the last alert sent time, and add a hold time to that. My compare time function uses the code:

if (msg.curtime >= msg.holdtime) {
msg.send = “True”;
} else {
msg.send = “False”;
}

return msg;

P.S. if you use the join node method, in scenario where comparing the 2 times for >= or <=, I would consider adding a 1 second delay somewhere before the 2nd time (hold time in my case) is passed to the join node to make sure it is added to the expected array position.

[{"id":"b31d60725fc9b033","type":"api-call-service","z":"3bec427eb6b0871d","name":"Set LLLA Time","server":"95c0ebe7.5d0aa8","version":3,"debugenabled":false,"service_domain":"input_datetime","service":"set_datetime","entityId":"input_datetime.lll_alerttime","data":"{\"datetime\":\"{{ curtime }}\"}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":920,"y":260,"wires":[[]]},{"id":"316815fe0cb06cb4","type":"function","z":"3bec427eb6b0871d","name":"Compare Times","func":"if (msg.curtime >= msg.holdtime) {\n    msg.send = \"True\";\n} else {\n    msg.send = \"False\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":260,"wires":[["1fb9bfd24f585dad"]]},{"id":"1fb9bfd24f585dad","type":"switch","z":"3bec427eb6b0871d","name":"Send Alert?","property":"send","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":870,"y":200,"wires":[["b31d60725fc9b033","c64b2da127276469"]]},{"id":"530574acd39ea25e","type":"api-current-state","z":"3bec427eb6b0871d","name":"Get LLLA Time","server":"95c0ebe7.5d0aa8","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.lll_alerttime","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"LLLAtime","propertyType":"msg","value":"","valueType":"entityState"}],"x":480,"y":260,"wires":[["5e583d205daee41f"]]},{"id":"5e583d205daee41f","type":"moment","z":"3bec427eb6b0871d","name":"Add Hold Time","topic":"","input":"LLLAtime","inputType":"msg","inTz":"America/New_York","adjAmount":"10","adjType":"minutes","adjDir":"add","format":"YYYY-MM-DD HH:mm","locale":"en","output":"holdtime","outputType":"msg","outTz":"America/New_York","x":660,"y":200,"wires":[["316815fe0cb06cb4"]]},{"id":"c64b2da127276469","type":"debug","z":"3bec427eb6b0871d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":200,"wires":[]},{"id":"d1a269d1e712c403","type":"inject","z":"3bec427eb6b0871d","name":"Test Notify","props":[{"p":"topic","vt":"str"},{"p":"curtime","v":"( $now('[Y0001]-[M01]-[D01] [H01]:[m01]:[s01]', '-0400') )","vt":"jsonata"},{"p":"payload"},{"p":"image1","v":"/media/Garage01.jpg","vt":"str"},{"p":"image2","v":"/local/bi/Garage01.jpg","vt":"str"},{"p":"attachments","v":"{\"path\": \"/media/Garage01.jpg\"}","vt":"str"},{"p":"URL1","v":"/lovelace-security/cameras","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Test Notification","payload":"(msg.topic & \" - \" & msg.curtime)","payloadType":"jsonata","x":300,"y":280,"wires":[["530574acd39ea25e"]]},{"id":"87e9435bbec191e0","type":"server-state-changed","z":"3bec427eb6b0871d","name":"Garage Door State","server":"95c0ebe7.5d0aa8","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.garage_door_opener","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"topic","propertyType":"msg","value":"Garage Door","valueType":"str"},{"property":"curtime","propertyType":"msg","value":"( $now('[Y0001]-[M01]-[D01] [H01]:[m01]:[s01]', '-0400') )","valueType":"jsonata"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"payload","propertyType":"msg","value":"(msg.payload & \" - \" & msg.curtime)","valueType":"jsonata"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"attachments","propertyType":"msg","value":"{\"path\": \"/media/Garage01.jpg\"}","valueType":"str"},{"property":"image1","propertyType":"msg","value":"/media/Garage01.jpg","valueType":"str"},{"property":"image2","propertyType":"msg","value":"/local/bi/Garage01.jpg","valueType":"str"},{"property":"URL1","propertyType":"msg","value":"/lovelace-security/cameras","valueType":"str"},{"property":"channel1","propertyType":"msg","value":"Win-Dr","valueType":"str"},{"property":"priority","propertyType":"msg","value":"high","valueType":"str"}],"x":270,"y":200,"wires":[["530574acd39ea25e"]]},{"id":"95c0ebe7.5d0aa8","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

NodeRedExport