AC sleep cycle automation? Persistent timers on HA restart

When I sleep, turn off AC in 90 minutes.
Then start again after about 2 hours for 60 minutes.

Sounds simple, but have 2 problems:

  1. Sleep at different times so just timed automation won’t work.
  2. Now the thing is the place where I am there are a lot of power cuts and it’s possible that HA can get restarted multiple times during night, so need a solution where timer persists on restart.

Would really appreciate any help.
Thanks.

Depending on your platform, I’m on ubuntu and has been testing alternate timer automation using crontab with HA api.
The basic idea is when a switch is turn off with automation, a job to turn it back on is added to the crontab with appropriate time to execute so doesn’t matter HA restart or system reboot, the job will run when it’s time.
Something to consider.

Not sure what you’re using, or plan to use, for HA to know when you’ve gone to sleep, but when that happens, you can grab the current timestamp into an input_text. Then you can trigger the next event (e.g., turning the AC off 90 minutes later) by comparing sensor.date__time (converted to a timestamp) to the grabbed timestamp. This will work, as long as HA has a chance to gracefully shutdown when you have a power outage, because input_text entities are saved & restored at shutdown and startup. (If HA doesn’t have a chance to gracefully shutdown, I’m not sure what would work.) E.g.:

input_text:
  sleep_time:
    name: Sleep timestamp
sensor:
  - platform: time_date
    display_options:
      - 'date_time'
automation:
  - alias: Gone to sleep
    trigger:
      # Something appropriate goes here...
    action:
      # Grab current timestamp
      service: input_text.set_value
      entity_id: input_text.sleep_time
      data_template:
        value: "{{ as_timestamp(now()) }}"
  - alias: Turn AC off after 90 minutes
    trigger:
      # Trigger 90 minutes after going to sleep
      platform: template
      value_template: >
        {{ as_timestamp(strptime(states('sensor.date__time'), '%Y-%m-%d, %H:%M'))
           - states('input_text.sleep_time')|float > 90*60 }}
    action:
      # Turn AC off
      service: climate.set_operation_mode
      entity_id: climate.XXX
      data:
        operation_mode: 'off'

Etc.