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