Hi,
I am a little bit lost with template…
I would like to set an input_boolean to on if the current time (i.e. now) is within [time1, time2] interval.
time1 and time2 are constants (e.g 15:30:00).
Thank you for any help.
Hi,
I am a little bit lost with template…
I would like to set an input_boolean to on if the current time (i.e. now) is within [time1, time2] interval.
time1 and time2 are constants (e.g 15:30:00).
Thank you for any help.
Are the time constraints hard coded or set with an input_datetime ?
hard coded
If your values are hardcoded, this should work:
value_template: >
{% set time = states('sensor.time') %}
{{ '17:00' < time < '17:30' }}
Please be informed that you need to setup the time sensor in order for this to update the state of the binary_sensor on a regular basis (here every minute). If you use now(), the binary_sensor will not update.
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.
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”) }}
Thank you for the tip !
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
.