I do it with two input_datetimes (not input_numbers) to control a binary sensor. The use of time only input_datetimes complicates things as they need today’s date added to be compared with now():
- platform: template
sensors:
lounge_ac_am_automation_time_active:
friendly_name: "Lounge AM Automation Time Active"
value_template: >-
{% set update = states('sensor.time') %}
{% set d = now().strftime("%Y-%m-%d ") %}
{% set t = now().timestamp() %}
{% set am_start = strptime(d + states('input_datetime.lounge_ac_am_on_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
{% set am_end = strptime(d + states('input_datetime.lounge_ac_am_off_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
{{ am_start <= t <= am_end }}
If you only want to match the hour with input_numbers it is a lot simpler:
binary sensor
- platform: template
sensors:
kind_of_timer:
friendly_name: "Kind of Timer"
value_template: >-
{% set update = states('sensor.time') %}
{% set t = now().hour %}
{% set start = states('input_number_start_hour')|int %}
{% set stop = states('input_number_stop_hour')|int %}
{{ start <= t <= stop }}
You can use the state of that binary sensor (trigger) in an automation to switch your input_boolean (action).
The template updates every minute curtsey of including {% set update = states('sensor.time') %}.
I took your example and modified it a bit, really just replaced some names…
But the sensor never changed value from False to True, any ideas why?
I set the start time to 17 and end time to 18, and then waited until the clock showed 17, I was hoping the sensor would show “True” then, but it didn’t.
What am I missing here?
The sensor…
- platform: template
sensors:
varmeflakt_timer:
friendly_name: "Timer"
value_template: >-
{% set update = states('sensor.time') %}
{% set t = now().hour %}
{% set start = states('input_number_varmeflakt_start')|int %}
{% set stop = states('input_number_varmeflakt_stopp')|int %}
{{ start <= t <= stop }}
Okay, so it kinda works, the sensor gets set to “on” at specified hour, but for some reason it doesn’t get set to “off” at the next specified hour…any ideas why?