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.