Extract and use dates and times

Can someone please help me? I am trying to get a template to use in node-red. Hopefully, I can explain clearly enough what I am looking for.

I would like a node that reads “sensor.bedroom_echo_next_alarm”
If the state is “unavailable” or if the state is NOT (which reads as date and time) is between 04:00 - 07:00 = false

if the state IS between 04:00 - 07:00 (but also less than 12 hours away) = true
(If its Monday, and the next alarm is set for Wed between those times, I don’t want it to be able to read true, just because the next alarm is set between those values) if that makes sense.

and then the node passes that info through a true and false output to the next node.

Date and time how?
yyyy-mm-dd hh:ii:ss?
It makes difference since not all formats are supported, so you may need to convert it.
But what is supposed to be true/false?
If I was in your position I probably would have made a sensor that returns true/false in HA and just use that in the node red sequence.

When there is a “next alarm” it appears as the following.

2021-03-20T03:56:00-05:00

I have no problem setting a True/False sensor. But, how do I set the rule for the sensor to be true when the alarm is set between (??:?? and ??:??), and also (<?? hours) from (current time)

I’m a bit busy right now but here is how my next alarm sensor works.
It will probably solve some of your issues and give you hints to other.
Or I can help you when I have time.

Something like this is probably the sensor you need.

{% set timestamp = as_timestamp(states("sensor.bedroom_echo_next_alarm"))| int %}
{% if states("sensor.bedroom_echo_next_alarm") == 'unavailable' %}
false
{% elif timestamp | timestamp_custom('%H', true) | int >= 4 and timestamp | timestamp_custom('%H', true) | int <= 6 and (timestamp - now().timestamp() |int) <= (60*60*12) %}
true 
{%else%}
false
{% endif %}

Thank you sooooooooo much! I just loaded the template and seems to be working perfectly. I will configure it and get it working. Thank you again!!!

Hellis81,

I’ve been trying to edit this to add minutes and can’t figure it out. Is it possible for you to help me with this? :pray:

Add minutes? In what way?

So you gave me this example which has been AMAZING!
Unfortunately, my work schedule has changed. I was hoping to create two separate templates. One for if the alarm was set between 3:45am-4:50am and the other to be set between 4:50am and 5:30am.

This one gives me the template if set between specific hours only.

Ok…
How about this?
Two input_text one with value 3:45-4:50 and one with 4:50-5:30.
Then a boolean to “flip” between them.
This means you get two times to choose from and each time is customizable.

Is that a good solution?

Or perhaps a input_select?
That way you could just add more times. And probably more intuitive.

You can use the date time format node to parse and convert the time to a usable format. Then send that value through a switch node. In the following flow you would set the inject node to fire after midnight. It will convert to a 3 number value (hmm). You can use any format moment supports.

[{"id":"336bb951.7b1376","type":"inject","z":"d6a46901.ebee1","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":270,"y":540,"wires":[["5e6a99b2.20e07"]]},{"id":"5e6a99b2.20e07","type":"api-current-state","z":"d6a46901.ebee1","name":"","server":"6b1110b5.183a4","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.pixel_3_next_alarm","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":520,"y":540,"wires":[["17064c5b.95395c"]]},{"id":"17064c5b.95395c","type":"moment","z":"d6a46901.ebee1","name":"","topic":"","input":"","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"hmm","locale":"C","output":"","outputType":"msg","outTz":"America/New_York","x":840,"y":540,"wires":[["28770132.5262be"]]},{"id":"28770132.5262be","type":"switch","z":"d6a46901.ebee1","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"450","vt":"str"},{"t":"lt","v":"450","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1110,"y":540,"wires":[[],[]]},{"id":"6b1110b5.183a4","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}]

https://momentjs.com/docs/#/displaying/format/