Can't figure out template to toggle volume_mute

Hi @DarkWolf

Can You please maka a step by step what did you done to make it work?
I cant make this button to work any way :frowning:

Should I have some additional lines in config.yaml and/or automations.yaml ?
I also have Denon - but want to make a simple separate lovelace button to toggle and display MUTE state of my Denon media player. (or ALL of my media players)

Nick

look at my post here. It has the exact code needed.

hi @petro I’ve tried this before but when putting this code to my button in lovelace i get error:

No card type found or Custom element doesn't exist: button-card.

So i switched to “type:button”:

type: button
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
    is_volume_muted: "{{ not state_attr('media_player.denon', 'is_volume_muted') }}"

And now im gettng this errors in editor:

Expected a value of type `{action,navigation_path,url_path,service,service_data} | undefined` for `tap_action.data_template` but received `{"entity_id":"media_player.denon","is_volume_muted":"{{ not state_attr('media_player.denon', 'is_volume_muted') }}"}`.

And info after pressing button:

Failed to call service media_player/volume_mute. required key not provided @ data['is_volume_muted']

So i’m missing somethig for sure :slight_smile:

SHOW VISUAL EDITOR

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.