How to test if a time is within an interval of time (template)

You have more than one sensor section?

Yes. Is it that the problem ?

it you are not running packages you are only allow one sensor: section

Well spotted petro :+1:

Yes, it was the problem !

Thanks to all !

There is no real need to create the sensor.time, now() works fine…

So in case it is usefull to anyone:

  {%if '7.30' > now().strftime('%H.%M') < '17.30' %}
    True, it is between 7.30 and 17.30
  {% else %}
    False, it is NOT between 7.30 and 17.30
  {% endif %}

templates didn’t behave that way back when this was originally posted, you were forced to add sensor.time to entity_id. However, that’s no longer needed because now() updates templates once per minute. Also that templates a bit off.

  {%if '07.30' < now().strftime('%H.%M') < '17.30' %}
    True, it is between 7.30 and 17.30
  {% else %}
    False, it is NOT between 7.30 and 17.30
  {% endif %}

I’m really into efficiency.
My way calculates sensor.time 1/min and uses it all over my system.
When I use it I then do a single comparison and we are done.
Yours asks for now() (1 operation which used to resolve instantly (and still does if triggered) but since … dang … can’t remember the dev who updated the template resolving engine (cgtobi or something ???) Anyway that’s now 1 / min in templates
You then apply it through strftime to format it
AND then you do the comparison

That just takes longer to evaluate

(Edit: I know I’m splitting hairs here, but any nano-second I can take)

I don’t have a way of testing its performance but this seems fairly efficient (it compares the time expressed as a tuple).

  {{ (7,30) < (now().hour, now().minute) < (17,30) }}

FWIW, I use that method in several of my automations.

Full Disclosure: I have no proof that constructing a tuple is any faster than using strftime or if comparing tuples is faster than comparing strings.

1 Like

Easy test in the template editor.
Capture Now()
Do evaluation
Capture Updated Now()
Get difference

or datetimes

{{ today_at("07:30") < now() < today_at("17:30") }}
1 Like

Neat! :+1:

I’ve employed your latest contribution (today_at) several times already but not that way yet.

That’s what it was meant for. I’m kicking myself for not changing the filter version of it to today which would have been nicer to read using a filter instead of "07:30" | today_at

It’s still early days, perhaps a small breaking change to rename it?

:man_shrugging: usually template changes are set in stone when it comes to method/filter names

Talked with some of the guys, might actually be able to push that through.

Rofl…seems i stirred up something :grin:

sorry to revive an old thread, but do any of these comparisons account for next day? for example, i want to create a sensor that tells the house it is night time between 11:00 p.m. and 5:00 a.m. it seems that the sensor above only account for time ranges within that day

i think i may have figured it out. i broke up the times with an or statement as

{{ (states('input_datetime.ac_night_start') < states('sensor.time') < states('sensor.time') < '23:59' ) or ('00:00' > states('sensor.time') > states('input_datetime.ac_night_end') )}}

{{ now() <= today_at("05:00") or now() >= today_at("11:00") }}
1 Like

@petro you can tell the people that have been doing this for a while ;)! thank you, much cleaner than mine!