Bob_NL
(Bob Visser)
August 21, 2017, 3:12pm
1
I have a custom sensor setup which calculates the time left untill my next alarm. Unfortunately the sensor is only updated when the alarmtime changes. I would like it to update every minute.
sensors:
- platform: template
sensors:
nextalarm:
friendly_name: Seconds untill next alarm
entity_id:
- input_slider.alarm_hour
- input_slider.alarm_minutes
value_template: >
{% set relative_time = (states.input_slider.alarm_hour.state|float|multiply(60) + states.input_slider.alarm_minutes.state|float) - (now().hour|float|multiply(60) + now().minute) %}
{%- if relative_time < 0 -%}
{{23*60+relative_time}}
{%- else -%}
{{ relative_time-60}}
{%- endif %}
time_until_alarm:
friendly_name: Time untill next alarm
entity_id:
- sensor.nextalarm
value_template: '{{ (states.sensor.nextalarm.state.split(" ")[0] | int *60 ) | timestamp_custom("%H:%M") }}'
I tried adding the following but that gives an error which tells me that isn’t a valid option in the sensor_template:
scan_interval: 60
If I add this to my customize section (for both sensors) it doesn’t error but it also doesn’t update my sensor.
Help would be appreciated.
Hi @Bob_NL , not sure, but i think you need an entity that changes frequently in your first template.
Maybe add the Time & Date sensor to your config
- platform: time_date
display_options:
- 'date_time'
and use it instead of ‘now()’ in your calculation.
Bob_NL
(Bob Visser)
August 21, 2017, 4:30pm
3
@VDRainer thanks for the tip. I’m using the time_date sensor allready but can’t figure out how to use it in my template. The sensor doesn’t have any attributes so I’m having a hard time splitting the hours and minutes.
This for example does nothing:
nextalarm2:
friendly_name: Seconds untill next alarm
entity_id:
- input_slider.alarm_hour
- input_slider.alarm_minutes
value_template: >
{% set relative_time = (states.input_slider.alarm_hour.state|float|multiply(60) + states.input_slider.alarm_minutes.state|float) - (states.sensor.time.state.hour|float|multiply(60) + states.sensor.time.state.minute) %}
{%- if relative_time < 0 -%}
{{23*60+relative_time}}
{%- else -%}
{{ relative_time-60}}
{%- endif %}
p.s. I’m using ‘time’ instead of ‘date_time’.
Bob_NL
(Bob Visser)
August 21, 2017, 5:06pm
4
Well I figured out how to use the date_time sensor in the template:
nextalarm2:
friendly_name: Seconds untill next alarm
entity_id:
- input_slider.alarm_hour
- input_slider.alarm_minutes
value_template: >
{% set relative_time = (states.input_slider.alarm_hour.state|float|multiply(60) + states.input_slider.alarm_minutes.state|float) - (states.sensor.time.state.split(":")[0]|float|multiply(60) + states.sensor.time.state.split(":")[1]|float) %}
{%- if relative_time < 0 -%}
{{23*60+relative_time}}
{%- else -%}
{{ relative_time-60}}
{%- endif %}
Unfortunately it gives exactly the same output (recalculation is only done when alarmtime changes).
From the template sensor docs
“entity_id (Optional): Add a list of entity IDs so the sensor only reacts to state changes of these entities. This will reduce the number of times the sensor will try to update it’s state.”
Maybe you can forget my first post.
Bob_NL
(Bob Visser)
August 21, 2017, 6:15pm
6
Finally working .
Loosing the entity id’s did the trick. Thanks a lot for your help, this has been bothering me for months!