Now the problem is that if something was played 5 minutes ago, but the amplifier has been on for 2 hours, it is turned off.
I think I need an action that would kind of reset the timer that is used by the âForâ field of the condition. I could probably do that by turning the amplifier off and on again, but I donât want it to actually turn off and on.
How do you want to do this? Thatâs not possible unless you have a way for HA to know that something has been played. If you have a way to see that something has been played, then you trigger on when it stops playing for 2 hours instead of the amplifier being on for 2 hours.
You should post all of this applicationâs automations so we can understand how everything interacts. Post them in YAML format, not screenshots of the Automation Editor (for more information, refer to guideline 14 in the FAQ).
The second automation (turn off amp after 2 hours of operation) can be done like this:
alias: Amplifier off
trigger:
- platform: state
entity_id: switch.amplifier
to: 'on'
for: '02:00:00'
action:
- service: switch.turn_off
target:
entity_id: switch.amplifier
However, neither version supports your requirement to turn it off 2 hours âafter the last time something was playedâ.
For it to do that, it has to be based on the activity of the media_player (the entity supplying the amplifier with music). The automation must be aware of when this media_player ceases playing and then it begins its 2-hour countdown.
My question has nothing to do with media player. The âplayingâ I was referring to is all handled by Rhasspy and MQTT. Home Assistant is only involved for turning the amplifier on (when something is published to hermes/hotword/+/detected) and off 2 hours after the last time it was turned on.
So with your proposed config, the 2 hours reset every time the turn_on action is run?
You canât âswitch onâ an entity thatâs already on. Sure, you can send it a command to turn on but itâs pointless because it wonât change the entityâs state (itâs already on). Without a state-change, the automation I posted wonât be reset so it fails to extend its 2-hour countdown.
Perhaps what you want is the first automation to do two things. Whenever it is triggered it should:
Turn on the amplifier.
Start a 2-hour timer (or restart the timer if itâs already running).
So everytime a payload is received, the timer is restarted and the 2-hour countdown starts over. When the timer finishes, it turns off the amplifier. Is that closer to what you want?
(Well, almost. I think to add a custom timer I have to edit a file on the server. If there was a way to restart the âhiddenâ timer that powers the for field, I wouldnât have to add a custom timer.)
The only thing that resets the for option is a state-change (or restarting Home Assistant or executing Reload Automations).
If itâs instructed to detect a state of on for 2 continuous hours, it will be reset if the state changes to off or unavailable. Your first automation can command the switch to turn on when itâs already on but on-to-on is not a state change so it canât serve to reset for.
Does that make more sense now?
You can create a timer via the UI. Go to Configuration > Helpers. Let me know if you need my assistance to create what I described in my previous post.
I thought up another way around this with a Repeat action.
Right now, it sounds like you have 2 triggers:
Reset the timer.
Count down the timer, and wait for it to finish.
Using Repeat, you can repeat until itâs no longer in the Playing state.
Thatâs not enough though.
Sequence
Repeat Until player not playing.
Have a trigger wait for the state to be Stopped.
Add another trigger that waits for 2 min OR if the state changes to Playing.
Turn off.
Any time the state changes to Playing again from any other state, the loop. At that point, it has to waits until it stops again, and then waits 2 min before Playing occurs.
This is a slick way of doing it, and you can even script it (similar to creating a function in code).
Iâm sure others can improve on this design and make it more streamlined.
If this was RxJS, Iâd simply use a debounceTime() operator leading me to believe there should be something similar built into Home Assistant.
. I write lots of reusable scripts, so I might have something like this on a per-room basis. In that case, I canât use a Restart mode.
When I used this code, it was for the garage door closing. If motion, reset timer, so it doesnât close on the trunk when bringing in groceries.
I couldnât use a Restart mode because I combine all my triggers in one automation for the garage door including close and âsomeone scanned their fingerprintâ. Itâs simpler for me that way, but then Iâm limited in the automation mode I can use without building separate scripts for each âchooseâ.