Can't figure out template to toggle volume_mute

oops sorry you need to make a template switch and then use that

switch:
- platform: template
  switches:
    denon_mute:
      value_template: "{{ state_attr('media_player.denon', 'is_volume_muted') }}"
      turn_on:
        service: media_player.volume_mute
        data_template:
          entity_id: media_player.denon
          is_volume_muted: "{{ not state_attr('media_player.denon', 'is_volume_muted') }}"
      turn_off:
        service: media_player.volume_mute
        data_template:
          entity_id: media_player.denon
          is_volume_muted: "{{ not state_attr('media_player.denon', 'is_volume_muted') }}"

then expose that in you button

type: button
name: Mute
show_name: true
icon: mdi:volume-off
entity: switch.denon_mute
3 Likes

OMG ! This is it !
You shoud make it “sticky” or something! Thank You!
This is full solution.

One more, there is entity_id type “all” in decsription of media.player config.
Is this applicable to this script?

PS. Can I buy You a “coffe” :slight_smile: ?

1 Like

I have a few posts covering that but it’s buried with harmony hub stuff and my config is here:

Sure, thanks! Here’s the link
Buy me a coffeeBuy me a coffee

Hi, can you please explain to me the syntax here?
What is ‘not state_attr’?
How do I know when to use it?
For example, if I want to toggle power, is it with the same string?

state_attr gets an attribute from an entity. The attribute mentioned in the other posts is_volume_muted is a boolean. This means the attribute is either true or false. When using the method state_attr to get is_volume_muted, it will return the value of the attribute; i.e. true or false. When adding the not in front of it, it inverts the true/false value. So true becomes false and false becomes true.

This depends on a lot. What are entity are you working with and what are you trying to create?

Thanks for the fast reply.
I don’t get something…
If I try the following:

- type: "custom:button-card"
  color_type: label-card
  color: rgb(66, 134, 244)
  name: Mute
  show_name: true
  icon: mdi:volume-off
  tap_action:
    action: call-service
    service: media_player.volume_mute
    data_template:
      entity_id: media_player.denon_2
      is_volume_muted: "{{ not state_attr('media_player.denon_2', 'is_volume_muted') }}"

it’s failing with:
Failed to call service media_player/volume_mute. required key not provided @ data['is_volume_muted']

but if I try:

- type: "custom:button-card"
  color_type: label-card
  color: rgb(66, 134, 244)
  name: Mute
  show_name: true
  icon: mdi:volume-off
  tap_action:
    action: call-service
    service: script.denon_toggle_mute

where script holds:

denon_toggle_mute:
  alias: "toggle mute denon"
  sequence:
    - service: media_player.volume_mute
      data_template:
        entity_id: media_player.denon_2
        is_volume_muted: "{{ not state_attr('media_player.denon_2', 'is_volume_muted') }}"

It works. What am I missing here?

1 Like

Yes because Lovelace does not accept templates. You have to use a script to do that.

Got you!
Thank you very much!

Regarding the power.
I have a denon receiver.
I want to create a script that toggle the power the same it toggles mute function.
Any idea what should the template compare?

just use media_player.toggle service

1 Like

Perfect! Thanks!

I have run into this issue as well but im getting a different error message. Im using the following:

        - type: custom:button-card
          entity: media_player.living_room_speaker
          icon: mdi:volume-mute
          tap_action:
            action: call-service
            service: media_player.volume_mute
            service_data:
              entity_id: media_player.living_room_speaker
              is_volume_muted: "{{ not state_attr('media_player.living_room_speaker', 'is_volume_muted') }}"
          template: icon_only_tom

But the error i get is the following when i push the button:

“Failed to call service media_player/volume_mute. invalid boolean value {{ not state_attr(‘media_player.living_room_speaker’, ‘is_volume_muted’) }} for dictionary value @ data[‘is_volume_muted’]”

So this to me suggests lovelace is accepting templates but the output of the template is not in the right format. Now when i go to the developer tools → templates and put this in, it is giving me the proper ‘True’ and ‘False’ but it also says that the result is a string as opposed to boolean. It almost suggests that the function “state_attr” is pumping out a string result and the button card is not accepting that string result. I also cannot see any way to convert it to a true boolean. I may try to convert it to 1 and 0 and see what happens there. Anyone run into this?

Lovelace does not accept templates.

That’s the custom button card, which accepts js.

ofcourse… what was i thinking? Thanks for pointing that out. Ill see what i can do with javascript. I appreciate the response.

I’m not 100% sure that you can template inside tap action with js either.

the code should be similar, try this

        - type: custom:button-card
          entity: media_player.living_room_speaker
          icon: mdi:volume-mute
          tap_action:
            action: call-service
            service: media_player.volume_mute
            service_data:
              entity_id: media_player.living_room_speaker
              is_volume_muted: |
                [[[
                  return (states['media_player.living_room_speaker'].attributes.is_volume_muted) ? false : true;
                ]]]
          template: icon_only_tom

Hello @petro i have read some of your other posts in similar topics… Super helpful.

Ya i had may have tried what you provided there because i think i saw something you posted in another thread. I will try it again tho.

Its very strange. I have this working now but im using a script to do it (again this may have come from you):

mute_toggle:
  fields:
    media_player:
      description: Media Player that will be mute or unmuted
  sequence:
  - service: media_player.volume_mute
    target:
      entity_id: "{{ media_player }}"
    data:
      is_volume_muted: "{{ not state_attr(media_player , 'is_volume_muted') }}"

THis works if i call the script with:

          - type: custom:button-card
            entity: '[[entity]]'
            icon: mdi:volume-mute
            tap_action:
              action: call-service
              service: script.mute_toggle
              service_data:
                media_player: '[[entity]]'
            template: icon_only_tom

So then i tried to do the same thing with a media_player toggle which should turn the media player (this case a google home) on and off. So i used a modification of the script like this (almost identical):

power_toggle:
  fields:
    media_player:
      description: Media Player that will be toggled
  sequence:
  - service: media_player.toggle
    target:
      entity_id: "{{ media_player }}"

And i call it with:

          - type: custom:button-card
            entity: '[[entity]]'
            icon: mdi:power
            tap_action:
              action: call-service
              service: script.power_toggle
              service_data:
                media_player: '[[entity]]'
            template: icon_only_tom

The weird thing is that it will only turn on the media_player but not turn it off. However, if i use the services developer tools and call the same media_player.toggle call with this same entity, it too will only turn it on but not off… HOWEVER if i create a basic entities card with teh media player (google home) and hit the power button on that (default media player i suppose), it will turn on and off all day long. There is something weird going on. I was thinking maybe it was a button card issue now but with the developer tools call not working now im not sure.

well, the good news is, you don’t need to get that complicated for toggle

          - type: custom:button-card
            entity: '[[entity]]'
            icon: mdi:power
            template: icon_only_tom

tap action defaults to toggle, and if it doesn’t

          - type: custom:button-card
            entity: '[[entity]]'
            icon: mdi:power
            tap_action:
              action: toggle
            template: icon_only_tom

As for

No clue, it should work

By they way, the code you provided does work! So it saves me from having to use a script… very nice! I even was able to template it in side a declutter card like this:

          - type: custom:button-card
            entity: '[[entity]]'
            icon: mdi:volume-mute
            tap_action:
              action: call-service
              service: media_player.volume_mute
              service_data:
                entity_id: '[[entity]]'
                is_volume_muted: |
                  [[[
                    return (states['[[entity]]'].attributes.is_volume_muted) ? false : true;
                  ]]]
            template: icon_only_tom

Seems to be working with the javascript variables in there. Super.

Now if i could figure out the toggle (power) issue.

Thank yoU!

2 Likes

Just to add to the story. I was trying to figure out why the button will only turn on the media player but not turn it off. I did use your suggested ‘toggle’ service and that works, but again, still only turns the media player on but not off. I ended up taking out the button-card template called ‘icon_only_tom’ and now the button does both. But the weird thing is that this is that button-card template:

  icon_only_tom:
    show_name: false
    aspect_ratio: 5/4
    color: white
    styles:
      icon:
        - mix-blend-mode: exclusion
      card:
        - background-color: rgba(100,100,100,0.3)
        - border-radius: 0.3em
        - box-shadow: rgb(0 0 0 / 30%) 0px 5px 1px -2px, rgb(0 0 0 / 14%) 0px 3px 2px 0px, rgb(0 0 0 / 12%) 0px 2px 5px 0px

i dont see anything in there that would have any sort of effect on the toggle action… weird eh? (canadian)

Ill keep playing with this… @petro i want to buy you a coffee for your great help… really appreciate it…