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