A script seems way too complicated to solve a benign binary control like mute volume.
Make a int number or global. Assign it 1 meaning the sound is on,
and assume mute is off ( sound is on ) at first, because it ~always is. If you call for mute, test if the number is 1, then mute, decrement number else unmute, increment number
Worst case, you have to hit it ONE more time in the beginning, to get in sync.
There, solved locally. This can be done with a boolean, template number, anything really.
cheers
Example using a global and simple assignment:
globals:
- id: g_mute_state # USED TO MAKE MUTE TOGGLE MEDIA PLAYER CONTROL OVER NETWORK
type: int
initial_value: '1' # 1 means volume ON 0 means mute is ON
switch:
- platform: template
id: e32a_momentary
name: "e32a_momentary button"
optimistic: true
on_turn_on: # WORKS BUT SHOULD BE turn_on_action PER INFO
- if:
condition:
lambda: 'return (id(g_mute_state) == 1);'
then: # CALL FOR MUTE
- logger.log: __________ MUTE __________
- homeassistant.service: ### MUTE
service: media_player.volume_mute
data:
is_volume_muted: 'true'
entity_id: media_player.onkyoreceiver
- lambda: '(id(g_mute_state) = 0);'
else: # CALL FOR UNMUTE
- logger.log: __________ UNMUTE __________
- homeassistant.service: ### UNMUTE
service: media_player.volume_mute
data:
is_volume_muted: 'false'
entity_id: media_player.onkyoreceiver
- lambda: '(id(g_mute_state) = 1);'
- delay: 100ms
- switch.turn_off: e32a_momentary
Your post will not work in home assistant. That’s clearly a ESPHome setup. I think you walked into the wrong thread. This will just confuse people. Not to mention, its way more complicated than a simple script.
Script
mute:
mode: parallel
fields:
media_player:
description: Media Player that will be mute or unmuted
example: media_player.media_room
sequence:
- service: media_player.volume_mute
target:
entity_id: "{{ media_player }}"
data:
is_volume_muted: "{{ not state_attr(media_player , 'is_volume_muted') }}"
Use in automation action section or a script sequence
Can’t remember now to be honest, I think it was a yaml script but I probably did something else wrong. Instead I just used condition A/B to write one, will just duplicate it for any devices needing mute. Thank you though