Templates: Volume up only if unmute, otherwise unmute

Hi all,
This is my first post here and I’m excited to be part of this community.

I’ve done a bunch of simple automation but hit the wall when trying to achieve volume up and unmute in one automation.

action:
  - service: media_player.volume_up
    entity_id: media_player.snapcast_client_bedroom
    data: {}
  - service: media_player.volume_mute
    data:
      is_volume_muted: false
    entity_id: media_player.snapcast_client_bedroom

I trigger this when pressing a button and it works fine except for the fact that it’ll always increase the volume. I’m trying to make it to only unmute in case it’s muted and increase the volume otherwise.

I’ve tried so many configurations but none got me there.

This is an example of one of those failed attempts:

action:
  service_template: >
    {% if (state_attr('media_player.snapcast_client_bedroom', 'is_volume_muted')) -%}
      media_player.mute
    {%- else -%}
      media_player.volume_up
    {%- endif -%}
  entity_id: media_player.snapcast_client_bedroom
  data:
    is_volume_muted: false

I think the service part is working, and it’s indeed choosing media_player.mute if it’s muted and volume_up otherwise, but it seems like sending is_volume_muted: false as data makes the volume_up fail. Maybe I need a conditional dictionary? Couldn’t find a way of doing that and any help would really be appreciated!

Thanks

You need to have a choose in the action, then check if unmuted, in which case you would mute etc.

Do you mean like that?

action: >
  {% if (state_attr('media_player.snapcast_client_bedroom', 'is_volume_muted')) -%}  
  - service: media_player.volume_mute
    data:
      is_volume_muted: false
    entity_id: media_player.snapcast_client_bedroom
  {%- else -%}
  - service: media_player.volume_up
    entity_id: media_player.snapcast_client_bedroom
    data: {}
  {%- endif -%}

Unfortunately this doesn’t work and yields this error message:
Message malformed: expected dictionary @ data['action'][0]

No, much simpler than that.

action:
- choose:
  - conditions: "{{ state_attr('media_player.snapcast_client_bedroom', 'is_volume_muted') }}"
    sequence:
    - service: media_player.mute
      data:
        entity_id: media_player.snapcast_client_bedroom
        is_volume_muted: false
  default:
  - service: media_player.volume_up
    data:
      entity_id: media_player.snapcast_client_bedroom

Your first attempt failed for the reason you stated; you can’t supply an unsupported option to a service call.

Your second attempt failed because you can’t include options within a template. A template is used to produce a value for an option.

What I posted is a effectively an if-else statement. If the media_player is muted then do this otherwise do that.

Thanks Taras, that did the trick. And thank you for taking the time to explain what I was doing wrong.

1 Like

Thank you!
By the way i looked for the opposite condition, there it is:

- conditions: {{ not state_attr('media_player.your_player', 'is_volume_muted') }}