How to make this automation only run once unless an hour has passes

Hi,

I have an automation for my washer and dryer where the automation tells me the washer / dryer has started based on current change. The problem is the current changes a few times during the cycle which makes the automation run many times during the cycle. I’d like to have the automation only run once unless an hour has passed. Is this possible?

Here is the code. I created the alert with the UI but thought I’d past the YAML here.

Thanks in advance
Mike


- id: '1724338758735'
  alias: Dryer started new
  description: ''
  trigger:
  - type: power
    platform: device
    device_id: 7474a8427e601667e545c657b1908053
    entity_id: d654124304a068dafba163bdb57ba7e9
    domain: sensor
    above: 6
  condition: []
  action:
  - action: tts.speak
    metadata: {}
    data:
      cache: true
      media_player_entity_id: media_player.family_room_home
      message: Dryer Started
    target:
      entity_id: tts.google_en_com
  - action: notify.mobile_app_mike_s_pixel_6a
    metadata: {}
    data:
      message: Dryer started
      title: Dryer started
  mode: single

add this condition to your code. Remember to change the automation name to match your code!!!

  trigger:
  - type: power
    platform: device
    device_id: 7474a8427e601667e545c657b1908053
    entity_id: d654124304a068dafba163bdb57ba7e9
    domain: sensor
    above: 6
  conditions:
  - condition: template
    value_template: >-
      {{ state_attr('automation.your_automation', 'last_triggered')|as_local > (now() - timedelta(hours=1) ) }}
  action:
  - action: tts.speak
    metadata: {}
    data:

The easy way is to add a 1hour delay to the end of your automation and make sure the mode is single so that it can’t trigger while it’s already running.

However, long delays are undesirable because restarting HA or reloading automations will interrupt it.

The template condition to avoid running if it was already triggered in the past hour would be this, which is similar to what was shared by Art but uses self-referencing to avoid needing the automation name, and it also handles gracefully when the automation has never been triggered:

{{ now() - this.attributes.last_triggered | default(0 | as_datetime) >= timedelta(hours=1) }}
2 Likes

Thank you! I’ll be testing this soon…thx again for the quick response!

Thank you Rick. Sorry, just to be clear you saying add the same bits that Art mentioned but change
{{ state_attr('automation.your_automation', 'last_triggered')|as_local > (now() - timedelta(hours=1) ) }}

to this
{{ now() - this.attributes.last_triggered | default(0 | as_datetime) >= timedelta(hours=1) }}

Pretty sure that’s what you’re saying but I want to make sure as I’m not much of a coder.

Thank You
Mike

Yes that is correct

@mmillner
There is a minor typo in your code.


 | default(0 |

needs a closing parentheses like this


 | default(0)|

To be clear to the original poster, both versions suggested work, but I actually prefer the self referencing version Rick posted.

The code was correct as originally posted; default(0 | as_datetime) means if the last_triggered attribute isn’t defined, then use Jan 1 1970 as the date instead.

Works perfectly…thank you!!!