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

I will try it asap. Thanks you !

Keep in mind, this won’t account for times before and after midnight in the same condition

(i.e. “23:00” < time < “00:30” }}

Also, keep in mind, Burningstone’s answer is to create a template binary_sensor, NOT an input_boolean. I would highly recommend their approach so you don’t have to create and maintain an separate automation to turn the input_boolean on/off.

Even if you really really wanted an input_boolean, I’d still create the binary sensor, then set an automation to follow that sensor. But I suspect the input_boolean will be redundant in this case…unless you really wanted a way to turn it off while it’s between that time.

1 Like

You are right, I need a template binary_sensor, not an input_boolean.

How can I configure several time intervals: [t1, t2] or [t3, t4], … ?

Here’s a tip that I think will help you a ton.

Open up Home Assistant, go to the Developer Tools, then click on Template at the top.

You can go crazy here trying it all out yourself!

You are right with multiples:

{{ (“17:00” < time < “18:00”) or (“13:30” < time < “15:25”) }}

1 Like

Thank you for the tip !

Here is my solution.

You mean like this:

binary_sensor:
  - platform: template
    sensors:
      time_in_range:
        friendly_name: "Time is in one of the predefined ranges"
        value_template: >-
          {% set time = states('sensor.time') %}
          {% set t1 = '17:00' %}
          {% set t2 = '17:30' %}
          {% set t3 = '11:00' %}
          {% set t4 = '12:30' %}
          {{ (t1 < time < t2) or (t3 < time < t4) }}

If you want this hardcoded values to be adjustable, you should check out the post @AhmadK linked.
And create some input_datetimes to easily set your times in the frontend.

Well, thanks for all these answers.
Right now, I fail to configure sensor.time updated each minute.
I configured

sensor:
  - platform: time_date
    display_options:
      - 'time'

What I have to do to have sensor.time updated ?

Do you mean you have a sensor called sensor.time and it doesn’t show the current time? It automatically updates every minute and then shows the current time.

My binary_sensor is off with an interval time such as {{ '00:00' < time < '23:59' }} so I suppose that ‘sensor.time’ is not updated even I configured

sensor:
  - platform: time_date
    display_options:
      - 'time'

So, what I am missing ?

Sorry, I don’t understand.

The time sensor sensor.time updates every minute and it shows the current time.
This means if you use sensor.time in your binary_sensor, it will check every minute if the value template that you configure for the binary_sensor is true or not. If the template evaluates to true, the binary_sensor changes to ‘on’ and if it is false the binary_sensor changes to ‘off’.

My binary_sensor is off with an interval time such as {{ '00:00' < time < '23:59' }} ?!

My (test) configuration:

sensor:
  - platform: time_date
    display_options:
      - 'time'

binary_sensor:
  - platform: template
    sensors:
      test:
        value_template: >-
          {% set time = states('sensor.time') %}
          {{ '00:00' < time < '23:59' }}

sensor.test is alway off in UI.

you need to set your variable ‘time’ as per the example you were given

value_template: >
  {% set time = states('sensor.time') %}
  {{ '17:00' < time < '17:30' }}

I did it (see my previous post).

Ah ! you edited your post, it was not clear before as you just posted the print statement

When you goto DEVELOPER TOOLS --> TEMPLATE
What do you get when your write

{{ states('sensor.time') }}

in the box ?

I see unknown.

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 !