Can't figure out template to toggle volume_mute

I’m just starting to work with Home Assistant and I’m trying to work through some basics, but I’m having trouble. I’ve been at this for many hours, and I just can’t figure it out. What I’m doing seems simple, but I just can’t seem to glean the correct information from the documentation…

I want to turn off my TV and mute my receiver.
I have LIRC (working) for turning off my TV (and frankly, I could do the same thing for my receiver, but that’s just working around the problem instead of trying to understand what I’m doing).

#Lirc shell command:
shell_command:
  tv_power: irsend send_once tv key_power

I have a Yamaha receiver that has entity_id: media_player.rxv477_b4f719. This receiver can be muted with media_player.volume_mute.
media_player.volume_mute takes the entity_id and the is_volume_muted value.

I imagine there are a number of ways to do this, but I first tried adding a template after “sequence:”. When that didn’t work, I tried to add a template to the data. When that didn’t work, I tried to simply template the value passed into is_volume_muted.

  test1:
    alias: TV Toggle
    sequence: >
     {% if is_state_attr("media_player.rxv477_b4f719", "is_volume_muted", "true") %}
      - service: shell_command.tv_power
      - service: media_player.volume_mute
        data:
          entity_id: media_player.rxv477_b4f719
          is_volume_muted: "true"
     {% else %}
      - service: shell_command.tv_power
      - service: media_player.volume_mute
        data:
          entity_id: media_player.rxv477_b4f719
          is_volume_muted: "false"
     {% endif %}

Next attempt:

   test2:
    alias: TV Toggle
    sequence:
      - service: shell_command.tv_power
      - service: media_player.volume_mute
        data_template: >
         {% if is_state_attr("media_player.rxv477_b4f719", "is_volume_muted", "true") %}
          entity_id: media_player.rxv477_b4f719
          is_volume_muted: "true"
         {% else %}
          entity_id: media_player.rxv477_b4f719
          is_volume_muted: "false"
         {% endif %}

Next attempt:

  test3:
    alias: TV Toggle
    sequence:
      - service: shell_command.tv_power
      - service: media_player.volume_mute
        data_template:
          entity_id: media_player.rxv477_b4f719
          is_volume_muted: {{ states.media_player.rxv477_b4f719.attributes.is_volume_muted == False }}

In addition to these 3 attempts, I tried countless variations on formatting; most of which seemed congruous with the online documentation. Very frustrating.

I’ve reviewed the templates in the dev tool, and in each case it seems to give me the values I want. I’m thinking there’s something wrong with my syntax, but I just cannot figure it out in the documentation or through many searches through google and this forum. Help would be greatly appreciated.

Errors in the log (with some exceptions) say something to the effect of expected… got “{” (leading me to believe the initial curly bracket of the template is getting parsed in the yaml - meaning I formatted the template wrong somehow) or in the case of the last one: invalid key: “OrderedDict([(‘states.media_player.rxv477_b4f719.attributes.is_volume_muted == False’, None)])”.

Thanks!

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