Can't figure out template to toggle volume_mute

The last example requires quotes around the single line template. You can either do that or split it into two lines like so

is_volume_muted: >
  {{ your expression }}

Also, are you familiar with hass --script check_config If you use that it’ll report errors in syntax.

This thread helped a bit. Solution below.

  receiver_mute_toggle:
    sequence:
      - service: media_player.volume_mute
        data_template:
          entity_id: media_player.denon
          is_volume_muted: >-
            {%- if states.media_player.denon.attributes.is_volume_muted == false -%}
            true
            {%- else -%}
            false
            {%- endif -%}
4 Likes

Hello all,

I’m trying this but getting

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

Running 0.93.1, did we have a breaking change in this syntax from 2017 to now? Probably…

Can someone please update this? Thanks a lot!

Tales

your automation doesn’t have the is_volume_muted attribute. If you read the error it says "required key not provided ‘is_volume_muted’. Take a look at your automation/script and verify that it still exists.

Also, this is a streamlined version of @bbrendon’s method.

  receiver_mute_toggle:
    sequence:
      - service: media_player.volume_mute
        data_template:
          entity_id: media_player.denon
          is_volume_muted: "{{ not state_attr('media_player.denon', 'is_volume_muted') }}"
2 Likes

Hi Petro,

Thanks for replying.

I’m using the button-card and denonavr components to control a Denon AVR-X3000 receiver.

Here’s the code for the mute button, using your suggestion for the more streamlined template (indeed much better):

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

I really wasn’t able to understand the error message. So in my case is the denonavr component that is not supplying the is_volume_muted attribute? It should, as it is listed and has a value in its states list:

Any ideas?

Thank you!

Tales Maschio

Yeah, you need to update the entity_id inside the is_volume_muted template

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

Sorry for that, my mistake copying/pasting here at the forum. In the code that’s actually running it was correct, but still doesn’t work.

Or I’m doing something really wrong, or denonavr component is unable to read the parameter from the device, no matter the flavour of the code in the template - the original if/then/else/endif statement or your more elegant way negating the attribute value.

The states dev-tools page can see the correct parameter value, so it’s there!!

Thanks for your help, I’ll continue looking into this.

I don’t see anything wrong with the template or the code. It all looks correct.

It must be a syntax error. If I put this in the template editor:

{{states.media_player.denon_sala.attributes.is_volume_muted}}

It will correctly show me the value for attribute is_volume_muted

Also, if I use this code:

- type: entity-button
  name: Mute
  icon: mdi:volume-off
  tap_action:
    action: call-service
    service: media_player.volume_mute
    service_data:
      entity_id: media_player.denon_sala
      is_volume_muted: false
  entity: media_player.denon_sala

The service will be called correctly, obviously muting or unmuting according to what is passed on the is_volume_mute parameter.

When replacing service_data for data_template and writing an expression that must be evaluated on is_volume_mute then it stops working. For some reason the code is not being interpreted.

This has to be ovbious, but I can’t see it.

Duh, I don’t know why I didn’t catch this before. You can’t template service data sections in lovelace.

Turn it into a script and call the script for the button

1 Like

It had to have an explanation. It occurred to me that maybe what I was trying was not allowed, but I didn’t want to believe that :)))

Ok, done through script. Thanks a lot!!! This was educational :slight_smile:

Tales

So I’m trying to do this via an automation but it doesn’t seem to work - I’m trying to toggle mute when an ad plays when I’m listening to music. However, when I use the above on a call_service call in the automation, I get the following error:

Error while executing automation automation.mute_music_when_ad_plays. Invalid data for call_service at pos 2: invalid boolean value {{ not state_attr('media_player.front_rooms', 'is_volume_muted') }} for dictionary value @ data['is_volume_muted']

but as this would toggle mute, I don’t want to hard code a value for is_volume_muted

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