I counted up my template sensors and binary template sensors. 19 uses of sensor.time and 5 uses of sensor.date.
Not too onerous a change.
In fact I think I could replace the majority of them with the custom scheduler card and significantly less input booleans. Might have to have a bit of an experiment with it at some stage.
This is exactly what Iām confused about (even sober!). The answer appears to be to shoehorn the entity_id into the template, but I canāt see how thatās better than the current approach, and would actually go so far as to say that itās a lot less user friendly
It will work. (Oh if it doesnāt you can add a dummy time templateā¦ that has been discussed above) However from what I have read it will parse the template and know what to doā¦
You shouldnāt have needed those for that because the template is only based on states(āsensor.next_alarm_timestampā). As long as states(āsensor.next_alarm_timestampā) updates properly, then you should be good.
yes, Thats what we thought at the time. However, time has taught updating of these sensors was rather peculiar, and had me add entity_idās consecutively. Oddly enough, the sensor.next_alarm_timestamp itself already needs these same entity_idās listed, even while these are in the template itself, except the sensor.time.
this has been the case since sometime ago, I could give it (taking out the entity_idās) another try in the current HA.
of course, the timestamp sensor is quite the one:
- platform: template
sensors:
next_alarm_timestamp:
friendly_name: Next alarm timestamp
entity_id:
- sensor.time
- input_boolean.alarmclock_wd_enabled
- input_boolean.alarmclock_we_enabled
- input_number.alarmclock_wd_hour
- input_number.alarmclock_wd_minute
- input_number.alarmclock_we_hour
- input_number.alarmclock_we_minute
value_template: >
{% macro getalarm(offsetday=0) %}
{% set day = (now().weekday() + offsetday) % 7 %}
{% set offset = offsetday * 86400 %}
{% set fmat = 'd' if day in range(5) else 'e' %}
{% set hour = 'input_number.alarmclock_w{}_hour'.format(fmat) %}
{% set minute = 'input_number.alarmclock_w{}_minute'.format(fmat) %}
{% set alarm = states(hour)|float|multiply(3600) + states(minute)| float|multiply(60) %}
{% set time = now().hour * 3600 + now().minute * 60 %}
{{(offset - time) + alarm if offsetday else alarm - time}}
{% endmacro %}
{% set weekday = is_state('input_boolean.alarmclock_wd_enabled','on') %}
{% set weekend = is_state('input_boolean.alarmclock_we_enabled','on') %}
{% set weekdays = [0,1,2,3,4] %}
{% set weekends = [5,6] %}
{% set day = now().weekday() %}
{% set nextday = day + 1 if day != 6 else 0 %}
{% if nextday in weekdays %}
{% if weekday %}
{% set offsetday = nextday %}
{% elif weekend %}
{% set offsetday = weekends[0] %}
{% else %}
{% set offsetday = None %}
{% endif %}
{% elif nextday in weekends %}
{% if weekend %}
{% set offsetday = nextday %}
{% elif weekday %}
{% set offsetday = weekdays[0] %}
{% else %}
{% set offsetday = None %}
{% endif %}
{% else %}
{% set offsetday = None %}
{% endif %}
{% if offsetday != None %}
{% set offset = offsetday-day if offsetday > day else offsetday - day + 7 %}
{% if (day in weekdays and weekday) or (day in weekends and weekend) %}
{% set today_alarm = getalarm()|float %}
{% else %}
{% set today_alarm = -1 %}
{% endif %}
{% set next_alarm = getalarm(offset)|float %}
{% set time = now().replace(second=0).replace(microsecond=0) %}
{% if today_alarm > 0 %}
{{as_timestamp(time) + today_alarm}}
{% else %}
{{as_timestamp(time) + next_alarm}}
{% endif %}
{% else %}
0
{% endif %}
jut did a quick test, and commented all entity_ids of all sensors, except for using
- sensor.time
- sensor.next_alarm_timestamp
on them, which works out fine for now.
the big one posted above now only has sensor.time, and seems to fare well at itā¦ Big changes must have happened behind the screens for this to be possible now. Which is cool indeed.
As long as the state is accessed it doesnāt matter how the string is built now as it will still find it. We donāt do analysis on the template string anymore, the analysis is done by looking at which states we touch during rendering of the template.
[ā¦stops typing mid post to say āthank youā your reworked template seemed to work with initial tests.]
So, just to be completely clearā¦
You are now saying my original templates will work untouched, after I remove the entity_id?
(albeit that I might prefer yours anyway )
EDIT: If that is so then it seems the whole discussion does āsimplyā come down to one of when in time sensors are evaluated and how to control that?