How turn off amplifier when media player is paused or idle for a while

Hi all,

I am trying to program an action within esphome that allows me to disconnect a switch when the media player is paused or inactive for a certain period of time.

I have tried it this way that I show, but it gives error.

Has anyone achieved this or has any suggestions on how it can be done?

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_dout_pin: GPIO16
    mode: stereo
    on_pause:
    for: 1min           #This line is not correct for esphome
      then:
        - switch.turn_off: relay0
    on_play: 
      then:
        - switch.turn_on: relay0

I’m far from an expert, but what happens if you create a binary_sensor or a switch with a wait_until AND timeout parameter when it’s on?

Theoretically, all you’d need to modify in your above yaml is to remove the for: part and change your entity so that it changes the state of your binary sensor/switch immediately.

Alternatively, just use a Homeassistant automation which definitely allows you to set a for: based on how long an entity has been in the desired state.

Thanks @ShadowFist for your suggestions. I try to follow your advice but I don’t know how I can set a binary sensor to on:

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_dout_pin: GPIO16
    mode: stereo
    on_pause:
      then:
        - press_binary_sensor: mp_paused      #How I can set a binary sensor??
    on_play: 
      then:
        - switch.turn_on: relay0

binary_sensor:
  - platform: template
    id: mp_paused
    filters:
      - delayed_on: 5s
    on_press: 
      then:
        - switch.turn_off: relay0

Any ideas?

Try:

      - lambda: |-
        id(mp_paused).publish_state(true);

I have tried this new code…but no luck either…

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_dout_pin: GPIO16
    mode: stereo
    on_pause:
      if:
        condition:
          for:
            time: 5s
            condition:
              not:
                media_player.is_playing:
        then:
          - switch.turn_off: relay0
    on_play: 
      then:
        - switch.turn_on: relay0

I have to keep trying to find a way to make it work.

I have managed to get the amplifier relay to turn off when it has not played music for about 15 minutes by establishing an interval that monitors that the condition is met every 5 minutes.
Surely it is not the most optimal programming but it works.

I don’t know if anyone can try to do it by another way, but from EspHome not from Home Assistant.

This is how I did it, maybe it can help someone.

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_dout_pin: GPIO16
    mode: stereo
    on_play: 
      then:
        - switch.turn_on: relay0

interval:
  - interval: 5min
    then:
      - if:
          condition:
            for:
              time: 15min   
              condition:
                not:
                  media_player.is_playing:
          then:
            - logger.log: "Turning off amplifier to save energy"
            - switch.turn_off: relay0