[SOLVED] prevent run if already run in last 10 mins

I have a couple of presence automations that work well though sometimes my HA has a fit and starts triggering the ‘welcome home person’ notifications every few minutes.

Now as I have Alexa announcing this it’s becoming proper annoying, so annoying in fact that the wife has banished me to the couch because my ‘sh*t never works right’ lol :joy:

So while I have this recently found time to reflect on a resolution I was wondering if there is a way of preventing an automation from running if it has been run in the last ten minutes … maybe utilising the last triggered event ?

Any pointers would be greatly received as I would like my bed back :joy::joy::joy:

2 Likes

In this case it’s not the automation running too often but that presence detection is flapping. You could try to make it more stable, for example by using a combination of device trackers for each person.

To build anti flapping you could read the concept in this openHAB forum thread.

Here is the setup I use to keep my security cameras from turning the lights on when it notices them being turned off. When my camera detects motion, it turns on the light for 5 minutes. When the 5 minutes expires, it set a 1 minute de-bounce timer, then turns off the light. If the de-bounce timer is running when motion is detected, the light won’t turn on.

configuration.yaml:

timer:
  motion_debounce:
    duration: '00:01:00'

automations.yaml:

- id: '1555681619396'
  alias: Driveway Motion
  trigger:
  - entity_id: binary_sensor.ring_driveway_cam_motion
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: timer.motion_debounce
    state: idle
  - condition: state
    entity_id: switch.driveway_light
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      entity_id: switch.driveway_light
    service: switch.turn_on
  - delay: 00:05:00
  - data:
      entity_id: timer.motion_debounce
    service: timer.start
  - data:
      entity_id: switch.driveway_light
    service: switch.turn_off
3 Likes

Have a look at the first condition in this automation;

- id: doorbell_alert
  alias: 'Doorbell Alert'
  trigger:
    entity_id: binary_sensor.doorbell
    platform: state
    to: 'on'
  condition:
  - condition: template # only notify once every 6 seconds at most
    value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.doorbell_alert', 'last_triggered')) |int(0) ) > 5 }}"
  - condition: state
    entity_id: input_boolean.dnd
    state: "off"
  action:
  - service: script.doorbell
  - service: script.flash_lights_purple
11 Likes

Added the following to the automations, and i must say its been much better today.

condition:
- condition: template # only notify once every 10 minutes at most
value_template: “{{ ( as_timestamp(now()) - as_timestamp(state_attr(‘automation.andrews_home_notification’, ‘last_triggered’)) |int(0) ) > 3600 }}”

Still getting it happening but its been 3 times through course of today rather than constant yesterday so progress.

Just need to dig into the presence trackers see what is going on.

2 Likes

Move all your actions into a script and call the script as the automation action. Note that is how my actions are done in the example above. The problem seems to be that the last triggered state does not update until all the actions have finished. With that delay in your actions this takes some time and the automation can be retriggered before this happens. It only takes a fraction of a second to call a script. Then the automation is done and the condition should prevent it firing right away again.

Here is the automation as it stands for now

# Andrew is home notification
- alias: 'Andrews Home notification'
  initial_state: true
  trigger:
    platform: state
    entity_id: person.andrew_vint
    from: 'not_home'
    to: 'home'
    for:
      seconds: 60
  condition:
    - condition: template # only notify once every 10 minutes at most
      value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.andrews_home_notification', 'last_triggered')) |int(0) ) > 3600 }}"      
  action:
    - service: notify.all
      data:
        message: 'Daddy is home YES!' 
    - service: script.audio_notify
      data_template:
        tts_msg: "Welcome Home Andrew"
2 Likes

If I understand well, we just need to add a delay in trigger at the end and using “single” mode automation.

1 Like

Delay in the action, not the trigger

1 Like

Yes indeed, I was reading something else while writing -_-

If set to Single, don’t you get a log warning if it triggers while there is a delay in the action.

Hi
I’ve got this automation

id: turn_on_garden_lights_on_movement_in_the_morning
  alias: "Lichten: Tuinverlichting 's morgens aan op beweging"
  trigger:
    platform: state
    entity_id: binary_sensor.det_living
    to: "on"
  condition:
    - condition: and
      conditions:
        - condition: time
          after: "06:00:00"
          before: "08:00:00"
        - condition: numeric_state
          entity_id: sun.sun
          attribute: elevation
          below: 1
        - condition: state
          entity_id: light.tuinverlichting
          state: "off"
  action:
    - service: homeassistant.turn_on
      entity_id: light.tuinverlichting   

My automation is turning on my lights too often, say it starts at 7, and when i want to turn off the lights, cause its clear enough at 7:40, then it autoturns on my light again

How can i make sure this automation only runs once ?
I need to add delay of 2 hours after the action or?

Can someone help with this one ? I have a relay which shouldn’t be switched on more often than 2 times on minute. And only 2 seconds at time.
This is not working… :frowning:

- alias: 'pakastin_ovi'
    initial_state: true
    trigger:
      entity_id: switch.keittio_apteekkari
      platform: state
      to: 'on'
    condition:
    - condition: template
      value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.pakastin_ovi', 'last_triggered')) |int(0) ) > 30 }}"
    action:
      - service: switch.turn_on
        entity_id: switch.keittio_apteekkari
      - delay: '00:00:02'
      - service: switch.turn_off
        entity_id: switch.keittio_apteekkari

Not sure if you ever solved this (I found it while searching for something else). It’s a lot easier now:

- alias: 'pakastin_ovi'
    initial_state: true
    trigger:
      entity_id: switch.keittio_apteekkari
      platform: state
      to: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.keittio_apteekkari
      - delay: '00:00:02'
      - service: switch.turn_off
        entity_id: switch.keittio_apteekkari
      - delay: "00:02:00"
1 Like

I found that earlier but thanks anyway! Yes have to admit that way much easier now :slight_smile: Hopefully this helps someone else too!