Reset state timer

I have an automation that turns on an amplifier whenever something is played using an action:

2 hours after the last time something was played I want it to turn off.

I set up an automation that is triggered by a time pattern every minute and turns off the amplifier if it has been on longer than 2 hours:

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.

I guess he has as

But yeah, the everlasting “I will only post the part of the automation I think is relevant”

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

Here’s the on-automation:

alias: Amplifier on
description: ''
trigger:
  - platform: mqtt
    topic: hermes/hotword/+/detected
condition: []
action:
  - type: turn_on
    device_id: 851f2c8dd097ac9f9233fbd338d74c9c
    entity_id: switch.amplifier
    domain: switch
mode: parallel
max: 10

And here is the off-automation:

alias: Amplifier off
description: ''
trigger:
  - platform: time_pattern
    minutes: '*'
    hours: '*'
    seconds: '0'
condition:
  - condition: state
    entity_id: switch.amplifier
    state: 'on'
    for: '02:00:00'
action:
  - service: switch.turn_off
    target:
      entity_id: switch.amplifier
mode: parallel
max: 10

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.

What’s the media_player?

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?

Then the following statement was misleading:

‘After something was played’ implies after the content finished playing (not 2 hours after the content started playing).

Anyway, if all you want to do is turn off the amp after 2 hours of operation, the example I posted above will do that.

Yeah, sorry, maybe I shouldn’t have used the word “play”.

Well, yes, but the 2 hours need to start over when it is “switched on” again (despite already being on), that’s my point.

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:

  1. Turn on the amplifier.
  2. 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?

Yes, that sounds exactly like what I 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.

1 Like

Whoa, didn’t know that. Somehow I have never clicked Helpers. Thanks a lot.

1 Like