It’s silly, but I have an automation to reset my ice maker every night at 22:00 - this automation has to turn the socket off, wait, and then turn it back on. The ice maker is not ‘smart’ and if it loses power, it can leave water in the freezing part, which isn’t good for the unit - but as soon as it regains power, it ‘empties’ that bin but keeps the unit ‘powered off’ (meaning it won’t start making more ice)
I ran into a situation though that if - for example - someone turns it on after 22:00 to make a batch of ice and doesn’t turn it back off, the automation I created that runs at 22:00 has no way of knowing it needs to do it again.
SO… I wrote this:
alias: Ice Maker - back off
description: ""
trigger:
- platform: device
type: turned_on
device_id: b5f9ef5619b40116296a60a7f0ff1564
entity_id: b6303fdf24907c7705b2b7f7af8b3520
domain: switch
condition:
- condition: time
after: "22:01:00"
before: "06:00:00"
action:
- delay:
hours: 0
minutes: 30
seconds: 0
milliseconds: 0
- type: turn_off
device_id: b5f9ef5619b40116296a60a7f0ff1564
entity_id: b6303fdf24907c7705b2b7f7af8b3520
domain: switch
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- type: turn_on
device_id: b5f9ef5619b40116296a60a7f0ff1564
entity_id: b6303fdf24907c7705b2b7f7af8b3520
domain: switch
mode: single
I was super proud of myself, until I realized that since I’m turning the outlet back on, it will - in theory - re-trigger and loop the entire automation.
After digging around, this is what I came up with - will it work?
alias: Ice Maker - back off
description: ""
trigger:
- platform: device
type: turned_on
device_id: b5f9ef5619b40116296a60a7f0ff1564
entity_id: b6303fdf24907c7705b2b7f7af8b3520
domain: switch
condition:
- condition: time
after: "22:01:00"
before: "06:00:00"
- condition: template
value_template: >-
{{ (as_timestamp(now()) - as_timestamp(states.automation.ice_maker_back_off.attributes.last_triggered | default(0)) | int > 2100) or states.automation.ice_maker_back_off.attributes.last_triggered == None }}
action:
- delay:
hours: 0
minutes: 30
seconds: 0
milliseconds: 0
- type: turn_off
device_id: b5f9ef5619b40116296a60a7f0ff1564
entity_id: b6303fdf24907c7705b2b7f7af8b3520
domain: switch
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- type: turn_on
device_id: b5f9ef5619b40116296a60a7f0ff1564
entity_id: b6303fdf24907c7705b2b7f7af8b3520
domain: switch
mode: single
Basically the important part here is:
- condition: template
value_template: >-
{{ (as_timestamp(now()) - as_timestamp(states.automation.ice_maker_back_off.attributes.last_triggered | default(0)) | int > 2100) or states.automation.ice_maker_back_off.attributes.last_triggered == None }}
I THINK that will properly say that if it’s been < 35 minutes (the automation waits 30 minutes before performing another power off / power on, so I built in 5 minutes of slush time) it will not run, and so should not re-trigger.
I’m no code monkey, so this is pretty complex to my small brain.