Automation Action Templating

Greetings all,

I am attempting to make an automation that syncs the volume of all google speakers to the same volume (Adjustment through spotify just adjusts their relative volumes up or down) based on the last changed volume.

I have created a custom sensor for each entity’s volume attribute, because I don’t think “states.entity.last_changed” is supported at child levels (Please let me know if I’m wrong).

My main question is, do automations support templating in the actions portion?

My automation action code is below. Please go easy, it’s probably not the most efficient:

action:
  {%set speakerDevices = namespace(devices = [ 
  "media_player.google_home_bedroom", 
  "media_player.google_nestmini_office", 
  "media_player.google_nesthub_living_room", 
  "media_player.google_nestmini_kitchen"])%}
  {%set speakerON = namespace(speakers = [])%}
  {%for device in speakerDevices.devices%}
    {%if states(device) != 'off'%}
      {%set speakerON.speakers = speakerON.speakers + [device]%}
    {%endif%}
  {%endfor%}
  {%for device in speakerON.speakers%}
    service: media_player.volume_set
    data:
      volume_level: >
        {%set googleSpeakers = namespace(volumeLevel = [
        states.media_player.google_home_bedroom.attributes.volume_level,
        states.media_player.google_nestmini_office.attributes.volume_level,
        states.media_player.google_nesthub_living_room.attributes.volume_level,
        states.media_player.google_nestmini_kitchen.attributes.volume_level])%}
        {%set lastTimeStamp = [
        states.sensor.volume_google_speaker_bedroom.last_changed,
        states.sensor.volume_google_speaker_office.last_changed,
        states.sensor.volume_google_speaker_living_room.last_changed,
        states.sensor.volume_google_speaker_kitchen.last_changed,] %} 
        {%set counter = namespace(counter1 = 0)%}
        {%set mostRecentChange = namespace(speaker = lastTimeStamp[0])%} 
        {% for speakerTimeStamp in lastTimeStamp %}
          {% if speakerTimeStamp >  mostRecentChange.speaker %}
            {% set mostRecentChange.speaker = speakerTimeStamp%}
            {%set counter.counter1 = counter.counter1 + 1 %}
          {% endif %}
        {% endfor %} 
        {{googleSpeakers.volumeLevel[counter.counter1]}} 
        {%set counter.counter1 = 0 %}
    target:
      entity_id: device
  {%endfor%}

Any help would be great, thanks!

  • Jeremy

You can’t use a template to generate YAML.

You can only use a template to generate a value (that’s assigned to a YAML option).

Can you post an example of one of these sensors? It seems like you should be able to make this a lot easier on yourself by using the data from ( and maybe assigning additional attributes to) those sensors.

For example, if your sensors’ states are the volume level of the devices like:

template:
  - sensor:
      - name: "Volume Google Speaker Bedroom"
        state: >
          {{ state_attr('media_player.google_home_bedroom', 'volume_level') }}

You could use something like:

action:
  - variables:
      speakers_on: > 
        {{ expand("media_player.google_home_bedroom",
        "media_player.google_nestmini_office", 
        "media_player.google_nesthub_living_room", 
        "media_player.google_nestmini_kitchen")| rejectattr('state', 'eq', 'off')
        | map(attribute='entity_id') | list }}
      recent_volume: >
        {{ expand("sensor.volume_google_speaker_bedroom",
        "sensor.volume_google_speaker_office",
        "sensor.volume_google_speaker_living_room",
        "sensor.volume_google_speaker_kitchen" ) 
        | sort(attribute='last_changed', reverse=true)
        | map(attribute='state') | first | float(0) }}
  - service: media_player.volume_set
    data:
      volume_level: "{{ recent_volume }}"
    target:
      entity_id: "{{ speakers_on }}"
1 Like

Hmmm this seems a lot more elegant. Filters aren’t my strong point. Is there any good documentation on how to use these?

Below is a look at my sensors.yaml

 - name: "Volume Google Speaker Bedroom"
   state: '{{states.media_player.google_home_bedroom.attributes.volume_level}}'


 - name: "Volume Google Speaker Office"
   state: '{{states.media_player.google_nestmini_office.attributes.volume_level}}'


 - name: "Volume Google Speaker Living Room"
   state: '{{states.media_player.google_nesthub_living_room.attributes.volume_level}}'


 - name: "Volume Google Speaker Kitchen"
   state: '{{states.media_player.google_nestmini_kitchen.attributes.volume_level}}'

I’ll give this a try

  • Jeremy

Your sensors are functionally the same as the example sensor I posted, so I think the automation action should work.

There are a number of sites that cover aspects of using Jinja sensors, but I’m unaware of any site that gives an exhaustive list and explanation of the filters and functions that are available for use in HA and that is geared towards normal users.

HA Templating Docs
PalletsProjects.com Filter list
Skalavala’s Jinja code for Ninjas