Create Sonos Mute Button- with script using fields (having trouble with template)

Ok, the goal I am trying to achieve is to create a Mute button for a Sonos device, where I can define which sonos device when creating the button. It seems that sonos devices to not respond well to the standard media_player.volume_mute as this funtion will only change the device to unmuted but when run again will not unmute, so…

I started by creating a script which when called would inquire as to whether the state attribute of a sonos device is curently muted or not and then change that attribute appropriately:

alias: Toggle Sonos Mute
sequence:
  - alias: Toggle Sonos Mute
    sequence:
      - data_template:
          entity_id: media_player.living
          is_volume_muted: >
            {% if is_state_attr('media_player.living', 'is_volume_muted', true)
            %}
              false
            {% else %}
              true
            {% endif %}
        action: media_player.volume_mute
description: ""

(sorry if poorly formatted - first post :slight_smile: )

Once I had that up and running - I added form variables to the code and got this:

sequence:
  - alias: Toggle Sonos Mute
    sequence:
      - data_template:
          entity_id: "{{sonosdevice}}"
          is_volume_muted: |
            {% if is_state_attr('"{{sonosdevice}}"', 'is_volume_muted', true) %}
              false
            {% else %}
              true
            {% endif %}
        action: media_player.volume_mute
alias: Toggle Sonos Mute
description: ""
icon: mdi:volume-mute
fields:
  sonosdevice:
    selector:
      entity:
        multiple: true
    name: sonosdevice
    description: Select Sonos device.
    required: true

And it seems to work in as much as it will mute the device intended in the form when I create a button which when clicked will “Perform Action” of this script on the selected device, but it does not seem to be evaluating the embedded template?

{% if is_state_attr(‘“{{sonosdevice}}”’, ‘is_volume_muted’, true) %}

Does this have to do with the fact that this is an embedded template? What am I doing wrong? It seems like all the script does now is target the device listed in the entity_id: but no longer evaluates the true/false logic?

Thank you so much in advance, fairly new to HA and looking to expand my understanding. Any tips or better ways to attack this problem would be great.

alias: Toggle Sonos Mute
description: ""
icon: mdi:volume-mute
fields:
  sonosdevice:
    selector:
      entity:
        multiple: true
    name: sonosdevice
    description: Select Sonos device.
    required: true
sequence:
  - alias: Toggle Sonos Mute
    sequence:
      - action: media_player.volume_mute
        target:
          entity_id: '{{ sonosdevice }}'
        data:
          is_volume_muted: |
            {{ not is_state_attr(sonosdevice, 'is_volume_muted', true) }}

NOTE

Your original template was incorrect because you attempted to nest one template inside another one (it’s invalid).

{% if is_state_attr('"{{sonosdevice}}"', 'is_volume_muted', true) %}
                    ^^^^^^^^^^^^^^^^^^^

This is correct:

{% if is_state_attr(sonosdevice, 'is_volume_muted', true) %}

I tried thisin the code… but now when I click the button that was created I get error message:

Failed to perform the action script/toggle_sonos_mute. Error rendering data template: TypeError: unhashable type: 'list'

Could it have something to do with the way I created the button that is executing this script?

show_name: false
show_icon: true
type: button
tap_action:
  action: perform-action
  perform_action: script.toggle_sonos_mute
  target: {}
  data:
    sonosdevice:
      - media_player.living
icon: mdi:volume-mute
icon_height: 50px
hold_action:
  action: none

Thanks

Edit: yes - it is - this works for a single device… when I change:

    sonosdevice:
      - media_player.living 

to:

    sonosdevice: media_player.living

the code works… I guess the next thing I will need to figure out is how to get this to work for multiple devices. I was hoping to have the ability to mute multiple devices at once and allowed for multiple devices to be selected, I will have to figure out how to get the script to run for each device in the selected list. I will read up on the documentation to see how to do this, unless it is an really easy fix I am just missing. Thank you

Yes, the hypen means that the value of sonosdevice contains a list (therefore what you created is a list containing one string item).

sonosdevice:
  - media_player.living

I suggest you change it to mean a string.

sonosdevice: 'media_player.living'

I made an edit to the last post, Thank you for the tip! Now I just need to read up on how to make this execute for a list of devices… There are areas where I want to deploy this script that it would be advantageous to have the mute affect multiple areas - ie I get a phone call in the master - but the master and master bath are both playing and i would want both to mute to take the call…

I haven’t read up on the documentation and don’t want to waste your time if it is a complicated fix, but if there is a simple solution I would be all ears!

Thanks again!! Cheers

1 Like