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
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:
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.
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.