Can't figure out template to toggle volume_mute

are you using data_template or data?

looks like data:

  - data:
      is_volume_muted: "{{ not state_attr('media_player.front_rooms', 'is_volume_muted') }}"
    entity_id: media_player.front_rooms
    service: media_player.volume_mute

Should it be data_template?

Yep, then it’ll work. Also put your entity_id inside data_template too.

EDIT: To clarify…

Whenever you use a template {{ }} or {% %} etc, you need to use it in a template field. data is not a template field, data_template is. It essentially tells home assitant to treat all fields inside data_template as templates. It doesn’t matter if the field is a template or not.

1 Like

That worked! Thanks!

For reference, in automations.yaml:

  - data_template:
      is_volume_muted: "{{ not state_attr('media_player.front_rooms', 'is_volume_muted') }}"
    entity_id: media_player.front_rooms
    service: media_player.volume_mute
1 Like

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.