Node-Red to open the flow between two input_datetime. (on) / (off)

Hello.

every 6 minutes I run node-red automation and I need to find out if the current time is between the values entered from the UI using input_datetime.on and input_datetime.off
Can this be done using two current states? How?
Or some better idea.

Thank you

Are you running a fairly recent version of nodered? There is a time node that will output at whatever the date time is set for. You could have it turn a boolean on or use them as triggers.

image

2 Likes

I need something like this:

[{"id":"700537048f589adb","type":"api-current-state","z":"fb8b375d.bdd7e","name":"input_datetime.on","server":"1c1ccadc.716d65","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":720,"wires":[["e718c351e006d310"]]},{"id":"e718c351e006d310","type":"api-current-state","z":"fb8b375d.bdd7e","name":"input_datetime.off","server":"1c1ccadc.716d65","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":650,"y":720,"wires":[[]]},{"id":"aba8d4b5fdc3e39b","type":"inject","z":"fb8b375d.bdd7e","name":"Ć” 6 min.","props":[{"p":"payload"}],"repeat":"360","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":200,"y":720,"wires":[["700537048f589adb"]]},{"id":"1c1ccadc.716d65","type":"server","name":"Dubak_HAss","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

You should listen to what Mike wrote. Itā€™s a much better solution.

1 Like

This is what it would look like.

OK thank you.
In the morning I will try to imitate ā€¦

I found this thread in looking for a solution to a very similar issue. My flow looks like Mikeā€™s above and usually works fineā€¦ however, if one of the input_datetime entities changes at some point during the dayā€“for example, if the ā€œtime twoā€ time passes and then gets changed to a later timeā€“the boolean remains false and doesnā€™t capture the new time.

So now Iā€™m thinking of ways to account for that activity. I thought if I set up a binary sensor entity in HA rather than using a NR flow, it would listen for changes in the input_datetimes and update on every changeā€¦ but this template doesnā€™t work for me:

I must have something wrong in the comparison of these times, because itā€™s always falseā€¦ Iā€™ve confirmed that my start and stop times are in the format %I:%M:%S.

EDIT: removed my old templateā€¦ this one works:

        {% set date = now().strftime("%Y-%m-%d ") %}
        {% set time = now().timestamp() %}
        {% set start = strptime(date + states('input_datetime.start_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {% set stop = strptime(date + states('input_datetime.stop_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {% if (time >= start) and (time <= stop) %}
          true
        {% else %}
          false
        {% endif %}

FYI

{{ today_at(states('input_datetime.start_time')) <= now() <= today_at(states('input_datetime.stop_time')) }}

Much more concise, but in Template (after adding my actual entity_ids) it gives me:

TypeError: ā€˜<=ā€™ not supported between instances of ā€˜datetime.datetimeā€™ and ā€˜functionā€™

missing () after now

1 Like

Thanks, that worksā€“but I did a direct copy from here, so Iā€™m not sure why they vanished.

cause I didnā€™t add them in my comment, I edited it

1 Like