Media_player.volume_mute Toggle

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

- service: script.toggle_mute
  data:
    media_player: media_player.onkyoreceiver

Use in the UI as a button’s tap action

tap_action:
  action: call-service
  service: script.mute
  service_data:
    media_player: media_player.onkyoreceiver
3 Likes

this is my simple automation:

  - alias: Mute volume ampli  
    description: with lg remote control
    mode: parallel
    trigger:
      platform: state
      entity_id: media_player.lg_webos_smart_tv
      attribute: is_volume_muted
      from:
        - true
        - false
      to:
        - false
        - true
    action:
      service: remote.send_command
      data:
        device: NAD 350
        command: nad_mute
      target:
        device_id: 446xxxx6fb35xxxx536e50281xxxx04

If you want to use change “entity_id” (media player) and “action” section.
gb!

Hello people I just noticed that webostv.button has a mute toogle

I put this code direct on the Android TV card

volume_mute:
    service: webostv.button
    target:
     entity_id: media_player.lg_recamara
    data:  MUTE

Works just fine!!!

fyi :

sequence:
  - if:
      - condition: template
        value_template: >-
          {{ is_state_attr('media_player.lg_webos_tv_um7400plb',
          'is_volume_muted', true) }}
    then:
      - action: media_player.volume_mute
        metadata: {}
        data:
          is_volume_muted: false
        target:
          entity_id: media_player.lg_webos_tv_um7400plb
    else:
      - action: media_player.volume_mute
        metadata: {}
        data:
          is_volume_muted: true
        target:
          entity_id: media_player.lg_webos_tv_um7400plb
alias: tvlivingmute
description: ""