Trouble with timers surviving a restart

hey all,

i have set up an extremely over complicated alarm system as i am terrible at getting up :stuck_out_tongue:

i have a repeat to send Alexa notifications and then a delay to wait before resending them, of course this means if i restart home assistant it gets cancelled in its tracks - so i have managed to set up a timer system for it to start a timer wait for it to complete and then continue the repeat

i thought since timers survive restarts if i reset home assistant midway through the delay timer it would have no issue pausing the timer and then when its all restarted continuing the timer and continuing the alarm (or at least being re-triggered by a state change from active to idle) but everything i do just does not survive a reboot

its only a 10 second delay and i had thought having a restore timer would pause it and then resume it on reboot but after watching a test 5 minute timer it just keeps ticking so i think that could be an issue?

any help would be appreciated - i will post the yaml if anyone needs it but lie i said its extremely over complicated so it may just be a big mess

(i do have a very simple workaround of just retriggering when home assistant boots but i would like to know if i can do it this way or at least understand why it isnt working)

We can better help if we see the yaml code instead of a screenshot of the UI.

here

alias: wake up reminder when at home
description: ""
triggers:
  - at: "08:30:00"
    trigger: time
    id: Time 8:30
  - at: "08:00:00"
    trigger: time
    id: Time 8
  - at: "06:00:00"
    trigger: time
    id: Time 6
  - at: "00:00:00"
    enabled: true
    trigger: time
    id: midnight
  - trigger: state
    entity_id:
      - binary_sensor.bed_leak_sensor_water_leak
    from: "off"
    to: "on"
    id: Get in bed
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - trigger: homeassistant
    event: start
    id: ha starts
    enabled: false
  - trigger: time
    at: "15:16:00"
    id: test time
  - trigger: event
    event_type: "timer.restarted\t"
    event_data:
      entity_id: timer.wake_up_test_timer
    context: {}
    id: timer restart
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.wake_up_test_timer
    context: {}
    id: timer finish
conditions:
  - condition: state
    entity_id: input_boolean.at_home
    state: "on"
actions:
  - choose:
      - conditions:
          - condition: time
            before: "09:00:00"
            after: "07:59:00"
            weekday:
              - mon
              - tue
              - sat
              - sun
            enabled: false
          - condition: state
            entity_id: binary_sensor.bed_leak_sensor_water_leak
            state: "on"
            for:
              hours: 0
              minutes: 0
              seconds: 30
            enabled: false
          - condition: trigger
            id:
              - Time 8:30
              - Time 8
              - test time
              - ha starts
              - timer
              - timer finish
              - timer restart
        sequence:
          - alias: alarm
            repeat:
              sequence:
                - action: media_player.volume_set
                  metadata: {}
                  data:
                    volume_level: 0.7
                  target:
                    entity_id: media_player.craigs_dot_bedside
                - parallel:
                    - data:
                        message: wake up, pokemon tag, brush teeth
                      action: notify.alexa_media_craigs_dot_bedside
                    - data:
                        message: wake up, pokemon tag, brush teeth
                      action: notify.alexa_media_craigs_echo_show
                - action: timer.start
                  metadata: {}
                  data: {}
                  target:
                    entity_id: timer.wake_up_test_timer
                  enabled: true
                - wait_for_trigger:
                    - trigger: state
                      entity_id:
                        - timer.wake_up_test_timer
                      from: active
                      to: idle
              until:
                - condition: and
                  conditions:
                    - condition: template
                      value_template: >+
                        {{ today_at() <= states('sensor.toothpaste2') |
                        as_datetime | as_local }}

                    - condition: numeric_state
                      entity_id: sensor.craigs_toothbrush_duration
                      above: 30
          - action: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.brushed_teeth
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.55
            target:
              entity_id: media_player.craigs_dot_bedside
        alias: non college days
      - conditions:
          - condition: and
            conditions:
              - condition: time
                before: "09:00:00"
                after: "07:59:00"
                weekday:
                  - fri
                  - thu
                  - wed
              - condition: state
                entity_id: binary_sensor.bed_leak_sensor_water_leak
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 30
              - condition: state
                entity_id: input_boolean.is_collage_running
                state: "off"
              - condition: trigger
                id:
                  - Time 8:30
                  - Time 8
        sequence:
          - alias: alarm
            repeat:
              sequence:
                - action: media_player.volume_set
                  metadata: {}
                  data:
                    volume_level: 0.7
                  target:
                    entity_id: media_player.craigs_dot_bedside
                - parallel:
                    - data:
                        message: wake up, pokemon tag, brush teeth
                      action: notify.alexa_media_craigs_dot_bedside
                    - data:
                        message: wake up, pokemon tag, brush teeth
                      action: notify.alexa_media_craigs_echo_show
                - action: timer.start
                  metadata: {}
                  data: {}
                  target:
                    entity_id: timer.wake_up_test_timer
                  enabled: true
                - wait_for_trigger:
                    - trigger: state
                      entity_id:
                        - timer.wake_up_test_timer
                      from: active
                      to: idle
              until:
                - condition: and
                  conditions:
                    - condition: numeric_state
                      entity_id: sensor.craigs_toothbrush_duration
                      above: 30
                    - condition: template
                      value_template: >+
                        {{ today_at() <= states('sensor.toothpaste2') |
                        as_datetime | as_local }}

          - action: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.brushed_teeth
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.55
            target:
              entity_id: media_player.craigs_dot_bedside
        alias: college days without college
      - conditions:
          - condition: and
            conditions:
              - condition: time
                before: "07:00:00"
                after: "05:59:00"
                weekday:
                  - fri
                  - thu
                  - wed
              - condition: state
                entity_id: input_boolean.is_collage_running
                state: "on"
              - condition: trigger
                id:
                  - Time 6
        sequence:
          - alias: alarm
            repeat:
              sequence:
                - action: media_player.volume_set
                  metadata: {}
                  data:
                    volume_level: 0.7
                  target:
                    entity_id: media_player.craigs_dot_bedside
                - parallel:
                    - data:
                        message: its a college day ,wake up, pokemon tag, brush teeth
                      action: notify.alexa_media_craigs_dot_bedside
                    - data:
                        message: its a college day ,wake up, pokemon tag, brush teeth
                      action: notify.alexa_media_craigs_echo_show
                - action: timer.start
                  metadata: {}
                  data: {}
                  target:
                    entity_id: timer.wake_up_test_timer
                  enabled: true
                - wait_for_trigger:
                    - trigger: state
                      entity_id:
                        - timer.wake_up_test_timer
                      from: active
                      to: idle
              until:
                - condition: and
                  conditions:
                    - condition: numeric_state
                      entity_id: sensor.craigs_toothbrush_duration
                      above: 30
                    - condition: template
                      value_template: >+
                        {{ today_at() <= states('sensor.toothpaste2') |
                        as_datetime | as_local }}

          - action: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.brushed_teeth
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.55
            target:
              entity_id: media_player.craigs_dot_bedside
        alias: college days early morning
      - conditions:
          - condition: trigger
            id:
              - Get in bed
          - condition: state
            entity_id: input_boolean.brushed_teeth
            state: "on"
          - condition: time
            before: "09:00:00"
        sequence:
          - alias: bed alarm
            repeat:
              sequence:
                - action: media_player.volume_set
                  metadata: {}
                  data:
                    volume_level: 0.7
                  target:
                    entity_id: media_player.craigs_dot_bedside
                - parallel:
                    - data:
                        message: get out of bed
                      action: notify.alexa_media_craigs_dot_bedside
                    - data:
                        message: get out of bed
                      action: notify.alexa_media_craigs_echo_show
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 3
                    milliseconds: 0
              until:
                - condition: state
                  entity_id: binary_sensor.bed_leak_sensor_water_leak
                  for:
                    hours: 0
                    minutes: 0
                    seconds: 0
                  state: "off"
          - action: media_player.volume_set
            metadata: {}
            data:
              volume_level: 0.55
            target:
              entity_id: media_player.craigs_dot_bedside
        alias: in bed alarm
      - conditions:
          - condition: trigger
            id:
              - midnight
        sequence:
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.brushed_teeth
        alias: bool off at midnight
mode: single

Does that look right to you?

i noticed that but i had copy pasted it from another user so i knew it looked wrtong but wasn,t sure


it looks fine in the visual editor which is what i mainly use

doesnā€™t seem to have changed it still doesnā€™t to anything on boot

As posted above, with a literal backslash character and letter tee, it corrupts the eventā€™s name.

My understanding is that you probably had a literal tab character and, during the copy-paste operation, it was represented as \t. It exists within the double-quotes so itā€™s interpreted as part of the entire string (thereby defining a non-existent event name).

Ensure the timerā€™s restore option is enabled and you understand how it causes an active timer to behave after being interrupted (notably its finished_at property).

If an interrupted active timer fails to automatically resume its remaining countdown after a restart, then you may need to report it as an Issue in Home Assistantā€™s Github Core repository. In fact, there appears to be an existing open Issue about a failure to resume.

1 Like

i removed the backslash and cleaned it up to be identical to the .finished trigger (which does work fine) and it still does nothing, and the timer is selected to restore, i dont know much about timers and at a glance through the wiki and such i cant find anything helping me out so thatā€™s why Iā€™m asking here,

i may be missing something with how the timer works or how the automation is set up all i know is that a 5 minute timer does continue after restart but the 10 second one wont so it wont retrigger the alarm when home assistant starts up and Iā€™m unsure why or any ways to work around this to get the automation to restart using a timer after i reboot home assistant

i would report an issue but with my limited knowledge i dont think i would be able to help with finding what the issue is nor how it could be solved

I have alot of complicated scenarios that are in any one of various states when home assistant is restarted. For each different thing that might be going on when HA is restarted, I have automations that run on start. For example, for a light that goes off when a timer is finished, I have this sort of thing in an automation that runs at sartup: If automation ā€œxā€ has enabled drop-down = ā€œEnabledā€ and the light is on but timer is inactive then restart the timer with the duration specified by the sliderā€¦ If the timer is active but the light is off and automation enabled then just cancel the timer, or if the timer is active but the automation is marked as disabled just cancel the timer, etc., etcā€¦

This way I can be sure that when HA is restarted, everything is back up and running in the proper state.

Hope that helps

BTW the trigger for when HA is shut down - I have never been able to use, seems like it is a weird unuseable one-off (as you might be trying to trigger an automation with devices that are unavailable - because HA is shutting down!).

1 Like

Probably because your system takes more than 10 seconds to restart. The timer is considered to have finished during the startup phase. Review the docs about finished_at.

From the documentation:

If an active timer was supposed to end while Home Assistant is stopped, the timer.finished event will fire on startup for that timer. The finished_at property in the event data will provide you with the time that the timer was actually supposed to fire which you can use in automation conditions to decide whether or not to act on it.

If the 10 second timer was supposed to finish while Home Assistant was in the process of restarting, on startup the timer.finished event will fire. If it doesnā€™t then itā€™s a bug (all of this assumes the timerā€™s restore option is enabled).

2 Likes

i had a feeling it had to do with the short time and how long ha took to restart as far as i am aware timer.finished does not trigger after a restart (but does when run normally) iā€™ll give this a quick recheck and verify

(also yes it is triggered to restore like i said)

Then itā€™s not behaving according to its documentation and is a bug; report it as a new Issue or, if thereā€™s an existing open Issue that describes the exact same faulty behavior, add a comment to it describing your observed failure scenario.

1 Like

just rechecked and it seemingly definitely isnā€™t triggering so i will do that! thanks for the help

1 Like

Pure speculation:

The bug might be something simple like the timer.finished event fires before Home Assistant finishes loading all automations. In other words, the Timer integration is loaded before the Automation integration. So by the time your automation is loaded, itā€™s too late to detect the timerā€™s event.

1 Like

i have also been wondering this exact thing as i was testing, wondering if it triggers before the automation actually has time to start working