Media_player.volume_mute Toggle

I’m trying to make a Mute Toggle (in a glance card). This works to unmute, but how do I make this a toggle?

                  - entity: media_player.media_room
                    icon: 'mdi:volume-off'
                    name: Mute
                    tap_action:
                      action: call-service
                      service: media_player.volume_mute
                      service_data:
                        is_volume_muted: false
                      target:
                        entity_id: media_player.media_room
2 Likes

Don’t think there is a toggle call right away. But you could make a script for it. You van even make it universal by passing the entity_id as parameter (field). You can read the current mute status from the attribute.

this should point you down the track

A mute script:

mute_toggle:
  alias: Toggle media_player mute
  fields:
    entity_id:
      description: Entity id of the media_player
      example: media_player.media_room
  sequence:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ state_attr(entity_id, ''is_volume_muted'')
          == false }}'
      sequence:
      - service: media_player.volume_mute
        data:
          is_volume_muted: true
        target:
          entity_id: '{{ entity_id }}'
    default:
    - service: media_player.volume_mute
      target:
        entity_id: '{{ entity_id }}'
      data:
        is_volume_muted: false
  mode: single
1 Like

That works to unmute, but I can’t get it to mute. I’ve also tried simplifying:

media_mute_toggle:
  mode: single
  sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: media_player.media_room
            attribute: is_volume_muted
            state: 'false'
        sequence:
          - service: media_player.volume_mute
            target:
              entity_id: media_player.media_room
            data:
              is_volume_muted: true
    default:
    - service: media_player.volume_mute
      target:
        entity_id: media_player.media_room
      data:
        is_volume_muted: false

The script works flawless here…Is the the is_volume_muted actually set with your entity?

Thanks for your response. is_volume_muted is an attribute, only visible when the media player is on

This is my configuration.yaml

media_player:
  - platform: yamaha
    host: 192.168.2.83
    name: Media Room
    source_names:
      HDMI1: "HDMI 1"
      HDMI2: "HDMI 2"
      HDMI3: "HDMI 3"
      HDMI4: "HDMI 4"
      AV1: "AV 1"
      AV2: "AV 2"
      AV3: "AV 3"
      AV4: "AV 4"

What’s weird is that if I mute it (with a mini media player card) clicking on my button unmutes. But clicking again does not mute.

1 Like

When you call the script, do you see the attribute change?

The for the Automation Trace to also work on scripts…

chromecast will mute and unmute as required.

My TCL TV will toggle the mute, reguardless if you tell it to mute or unmute. If you tell it to mute when it is muted, it will unmute (and visa-versa).

I know that’s how it works with IR, but if you goto Developer Tools / Services and enter Media player: Mute volume, followed by the media_player entity, there is a toggle called: True/false for mute/unmute. When on, it will mute and not unmute, when off, the opposite.

Is there some way to call that service that doesn’t force you to choose between mute and unmute?

No… But if the attribute changes my script should work :wink:

make a script that toggles itself.

yamaha_toggle:
  sequence:
  - service: media_player.volume_mute
    target:
      entity_id: media_player.media_room
    data:
      is_volume_muted: "{{ not state_attr('media_player.media_room', 'is_volume_muted') }}"

then call it on your card:

                  - entity: media_player.media_room
                    icon: 'mdi:volume-off'
                    name: Mute
                    tap_action:
                      action: call-service
                      service: script.yamaha_toggle

EDIT: FYI, the reason your script with the choose isn’t working is because you have ‘false’ in quotes. Remove the quotes and it would work. Or you can use the simplified script that uses templates that I posted above. Both will get the job done.

EDIT2: If you want to get really fancy, you can use variables with your script:

mute:
  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') }}"

then your tap actions will be:

                    tap_action:
                      action: call-service
                      service: script.mute
                      service_data:
                        media_player: media_player.media_room
11 Likes

Oh, I get it, you want to togle.

Should be able to determine if it is muted or not, and do the opposite with a little logic.

****Edit: after I wrote a whole other post, I found @petro did it a lot more eloquently than I would have proposed.

Thanks to all. Very elegant solution. I’ll study it to learn as much as I can from that.

Cheers, Richard

I missed that he changed that in his edit :confused:

@RichardU If you would just have copied my script you would have had a working version a whole week ago. Version of @petro is 99% the same, only he used a template value where I used a chooser… So the error must have been in the coping.

2 Likes

I’ve found this works also toggles the mute

tap_action:
  action: call-service
  service: media_player.volume_mute
  service_data:
    is_volume_muted: false
    entity_id: media_player.roku_50_tv

That just turns off mute, that does not toggle it on/off.

On my roku tv by hisense it toggles. The toggles sevice just mutes only??? I don’t know why same as power toggles, it only turns it off? Test and see on your tv, it’s better than another script.

1 Like

Providing “false” to that attribute will only unmute it. Not sure what else to tell you. This may be specific to your device only and not all devices. It does not work with any media player I own.

This also works for toggling mute with my Samsung TV.