True, but an int can remedy that whereas converting UTC to local time is messy.
Misunderstood your point about the microseconds.
All this to say that states.sensor.time.lastupdated is more like using utcnow() than now(). The user needs to keep that in mind if they intend to use it in their date calculations.
I think the takeaway is that states.sensor.time.last_updated can be useful but should be used judiciously because it isn’t the equivalent of either now() or utcnow(). As you mentioned, it’s sensor.time after all so it only has 1-minute resolution and last_updated is UTC-based.
That’s also good advice for existing Template Sensors that use this:
entity_id: sensor.time
and then proceed to use now() to perform time arithmetic that look for sub 1 minute differences. The darn thing is only going to be evaluated every minute so looking for, as you mentioned, a 30-second difference would be a mistake. You’re obliged to look for differences in whole minutes.
Hopefully, very few people have made that mistake.
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.