Creating dynamic entities names, getting string instead of object.state

Tried for hours now, and started to feel really stupid, so I hope someone can shed up some light on my problem…

Big Picture:
I’m trying to mute my sonos when snips detects the hotword. As my snips siteId is the same as my sonos media_players name I want to have one automation for all my rooms.
The siteId is delivered in the payload and can be etxracted (trigger.payload_json.siteId). Then I want to dynamicaly create the object.

Problem
That works in the condition, it works in the service to lower the volume, but it is just not working where I want to save it to a variable via the custom component hass_variables. https://github.com/rogro82/hass-variables Instead of the current volume, the string is beeing saved.

The automations work great when using static values e.g.

states.media_player.arbeitszimmer.attributes.volume_level

This is what i got so far:

Automation to save the actual volume level, and lower the volume

- alias: Mute Sonos when SNIPS hotword detected
  id: mute_sonos_on_snips
  trigger:
  - platform: mqtt
    topic: hermes/hotword/default/detected
  condition:
    condition: template
    value_template: "{{ is_state('media_player.' ~trigger.payload_json.siteId, 'playing') }}"
  action:

#Problem start
  - service: variable.set_variable
    data_template:
      variable: tmp_sonos_volume
      value_template: "{{'states.media_player.' ~ trigger.payload_json.siteId ~ '.attributes.volume_level' }}"
#Problem end

  - service: media_player.volume_set
    data_template:
      entity_id: "media_player.{{trigger.payload_json.siteId}}"
      volume_level: '0.05'

a second automation is setting the volume back when snips goes inactive again…

I’ve also tried among others:


{% set where = my_test_json.siteId %}
{% set front = 'states.media_player' %}
{% set back = 'attributes.volume_level' %}
{% set value = states.media_player.arbeitszimmer.attributes.volume_level %}
{% set final = [front,where,back]|join(".") %}

value is working, but hardcoded result: 0.05
final is a string and not the object and its volume setting result : states.media_player.arbeitszimmer.attributes.volume_level


value_template: "{{ states('media_player.' ~ trigger.payload_json.siteId ~ '.attributes.volume_level') }}"

gives me this error:

ERROR (MainThread) [homeassistant.core] Invalid service data for media_player.volume_set: expected float for dictionary value @ data['volume_level']. Got 'unknown'

so I added the conversion to float

value_template: "{{ states('media_player.' ~ trigger.payload_json.siteId ~ '.attributes.volume_level') |float}}"
or
value_template: "{{ states('media_player.' + trigger.payload_json.siteId + '.attributes.volume_level') | float }}"

which results in 0.0 instead of the current volume of the object…

Thanks!

So after palying arround with it for days, and finally posting it to the community I’ve found the solution a few minutes later…

value_template: "{{ state_attr('media_player.' + trigger.payload_json.siteId, 'volume_level') }}"

the state_attr() is converting a string to an object it seems, and therfore I can concatenate the string.

Feeling less stupid now, just a little slow :wink:

will leave it here as a ressource for others…

4 Likes