Thanks for the offer but I think I’ve got it acceptably worked out using HA scripts & automations.
I fudged the sunset transition automation 4 hour window a bit since the longest time difference for sunset throughout the year here is from 17:11 in the winter to 21:17 in the summer so it exceeds my 4 hour time by 6 minutes. The four hours is a minimum so I just bumped the delays/timers to 4:15.
And with me being the only one to restart HA I shouldn’t run into an inopportune restart anyway. and if I do it’s not anything super critical anyway.
But, OTOH, if you want to just have a play with it and see what you can come up with in AD I would be interested in seeing how it could be done there. I’ve never done anything at all with it and wouldn’t even know where to begin.
and I’ll be honest I’ve only been following this in case I can pick up any new knowledge so am not entirely familiar with the details. However if it helps I have timers setup that survive restarts by knowing the start time and duration. It’s not exactly rocket science, I know but I’m happy to share if it might be useful.
OK. But first, my use of time in Linux (python, jinja… whatever) is not exactly up to scratch so @petro or anyone else please don’t laugh, but by all means feel free to offer improvements.
This is a part of a ‘Guest Mode’ package but this is the relevant bit. The whole package is in my GitHub (This is most definitely NOT a plug, my GitHub is not a true repository, it is just a place I put stuff that others have asked for and is too big to paste here).
#====================================================
#=== Check Guest mode schedule timers at HA start up
#===
#=== If the house is not in holiday mode and
#=== if they are turned on, it is the right day and
#=== the time now is during the on time, then set
#=== the timer to run until the scheduled end time.
#====================================================
#=== Guest Schedule 1
- alias: Guest mode schedule 1 check at startup
trigger:
- platform: homeassistant
event: start
condition:
- condition: state
entity_id: binary_sensor.holiday_mode
state: 'off'
- condition: state
entity_id: input_boolean.guest_mode_schedule_1
state: 'on'
- condition: or
conditions:
- condition: template
value_template: "{{ states('input_select.guest_mode_schedule_1_day') == now().strftime('%A') }}"
- condition: state
entity_id: input_select.guest_mode_schedule_1_day
state: 'Every Day'
- condition: template
value_template: >
{% set time_now = as_timestamp(now()) %}
{% set start_time = states('input_datetime.guest_mode_schedule_1_start_time') %}
{% set start_time = as_timestamp(now()) | timestamp_custom('%Y-%m-%d') + ' ' + start_time %}
{% set duration = states('input_number.guest_mode_schedule_1_duration') | int * 60 %}
{% set end_time = as_timestamp(start_time) + duration %}
{{ time_now > as_timestamp(start_time) and time_now < end_time }}
action:
- service: timer.start
data_template:
entity_id: timer.guest_mode_schedule_1_duration
duration: >
{% set time_now = as_timestamp(now()) %}
{% set start_time = states('input_datetime.guest_mode_schedule_1_start_time') %}
{% set start_time = as_timestamp(now()) | timestamp_custom('%Y-%m-%d') + ' ' + start_time %}
{% set duration = states('input_number.guest_mode_schedule_1_duration') | int * 60 %}
{% set end_time = as_timestamp(start_time) + duration %}
{% set seconds_left = end_time - time_now %}
{% set hours_left = seconds_left // 3600 %}
{% set minutes_left = (seconds_left - (hours_left * 3600)) // 60 %}
{{ '%02i' | format(hours_left) }}:{{ '%02i' | format(minutes_left) }}:01
That looks interesting. I might be able to co-opt some of that and use the input boolean that starts my CPAP indicator system in motion to set the “start_time” and since the duration is always going to be the same that simplifies that a bit.