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