Automation with timing reset

Hi guys,

I’m new to automations in home assistant.

I’m working on a garage door opener. I want to send out a notification when the garage has been open for 5, 10, 30, and 60 minutes. So far this is working. However, I also want to stop notifications if the garage door closes. This last piece is what I’m struggling with. Is there a way to reset timing in an automation? I tried creating another automation to turn off the first, but that doesn’t work as the timer continues to work in the background.

I feel like there is a better way of doing this. Please let me know your thoughts. Thanks.

Below is my automation config (I’m using seconds instead of minutes to test):

- action:
  - data:
      message: Garage is open
    service: notify.telegram
  - delay: 00:00:05
  - condition: state
    entity_id: sensor.garage_status
    state: Door Open
  - service: notify.telegram
    data:
      message: Garage has been open for 5 minutes now
  - delay: 00:00:05
  - condition: state
    entity_id: sensor.garage_status
    state: Door Open
  - service: notify.telegram
    data:
      message: Garage has been open for 10 minutes now
  - delay: 00:00:20
  - condition: state
    entity_id: sensor.garage_status
    state: Door Open
  - service: notify.telegram
    data:
      message: Garage has been open for 30 minutes now
  - delay: 00:00:30
  - condition: state
    entity_id: sensor.garage_status
    state: Door Open
  - service: notify.telegram
    data:
      message: Garage has been open for 1 hour now
  alias: Garage door is open
  condition: []
  id: '1515877828496'
  trigger:
  - entity_id: sensor.garage_status
    platform: state
    to: Door Open

Rather than set that up as one action with delays, set it up as a single automation with multiple triggers. Use the “for” attribute of the state platform. Have it trigger if door has been opened for 5 min, 10, 20…

As an example, here’s an automation I use to auto-lock my front door, but only if the door is closed. It’ll repeat a few times.

- alias: Auto Lock Front Door
  trigger:
    - platform: state
      entity_id: lock.front_door_deadbolt_134
      to: 'unlocked'
      for:
        minutes: 5
    - platform: state
      entity_id: lock.front_door_deadbolt_134
      to: 'unlocked'
      for:
        minutes: 10
    - platform: state
      entity_id: lock.front_door_deadbolt_134
      to: 'unlocked'
      for:
        minutes: 15
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.front_door
        state: 'off'
      - condition: state
        entity_id: input_boolean.house_guest
        state: 'off'
  action:
    - service: lock.lock
      entity_id: lock.front_door_deadbolt_134
1 Like

thanks a lot @ih8gates that solved my problem. I broke it up into 4 different automations so I can send 4 different notifications. Is there a smarter way of doing this in one automation maybe?

- alias: Notifications Garage Door is Open for 5 mins
  trigger:
    - platform: state
      entity_id: sensor.garage_status
      to: Door Open
      for:
        minutes: 5
  action:
    - service: notify.telegram
      data:
        message: Garage has been open for 5 minutes

- alias: Notifications Garage Door is Open for 10 mins
  trigger:
    - platform: state
      entity_id: sensor.garage_status
      to: Door Open
      for:
        minutes: 10
  action:
    - service: notify.telegram
      data:
        message: Garage has been open for 10 minutes

- alias: Notifications Garage Door is Open for 30 mins
  trigger:
    - platform: state
      entity_id: sensor.garage_status
      to: Door Open
      for:
        minutes: 30
  action:
    - service: notify.telegram
      data:
        message: Garage has been open for 30 minutes

- alias: Notifications Garage Door is Open for 1 hour
  trigger:
    - platform: state
      entity_id: sensor.garage_status
      to: Door Open
      for:
        minutes: 60
  action:
    - service: notify.telegram
      data:
        message: Garage has been open for 1 hour

You could probably combine things and use a template to generate the number for how long the door has been open. But then you’ll need to figure out how to do math with timestamps. Advanced stuff!

yeah, this should be good enough then…thanks again!

You can try this for a repeating 5 minute template trigger. It essentially test if 300 seconds is evenly divisible by the time since triggered / changed in seconds.

{{  (as_timestamp(now()) | int -  (as_timestamp(states.script.chime.attributes.last_triggered ) | int )) / 300 ==  ((as_timestamp(now()) | int -  (as_timestamp(states.script.chime.attributes.last_triggered ) | int )) / 300) | int }}

I know this is a marked as ‘Solved’, but how about simply using the Alert component - isn’t that exactly what it’s for?

2 Likes

Good call. I always forget about the alert component!

I didn’t know about that. I’ll give it a try, thank you!

Just FYI - this is the setup I’m using (it’s in the configuration.yaml, though, not in my automations):

# Alert for Garage Door having been open for 5 minutes
alert:
  garage_door_open_long:
    name: Garage Door is still open!
    entity_id: binary_sensor.garage_door_sensor
    state: 'off'   # Optional, 'on' is the default value
    repeat: 5
#      - 10
#      - 30
#      - 60
    can_acknowledge: true  # Optional, default is true
    skip_first: true  # Optional, false is the default
    notifiers:
      - mypushbullet
      - emailfrommailgun

I’ve not played with the values I commented out - a reminder every 5min is good for me.
What I like particularly is that I can turn off the alerts when I know my garage door is open and don’t need to be reminded every 5min.

1 Like

Thanks a lot, will try it out!