How to access entity configuration variables in an automation

I have multiple media players configured like this in the configuration.yaml file:

media_player:

  • platform: mpd
    name: mopidy_liviningroom
    host: 192.168.10.245

  • platform: mpd
    name: mopidy_bedroom
    host: 192.168.10.181

Each media player box is running a copy of mopidy mpd. The same boxes run rhasspy as a voice assistant. Events coming from rhasspy include a _site_id field that can be used in automation to pick the correct entity_id for doing the standard media player commands. I however have a number of shell commands on these boxes that I use voice commands to activate using HA shell_commands. Right now I have a separate copy of the shell command for each media player that has the IP address hard coded. Here is an example:

livingroom_volume_set: 'ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no [email protected] ./set_volume {{ device }} {{ level }}'

The IP address is hard coded. Does anyone know how I can access the “host” configuration parameter from he media_player configuration in an automation? I would then pass the host value to the shell script.

Unless it is listed as an attribute of the media player (see Developer Tools States Menu) you can’t.

No it doesn’t show as an attribute. I could use domain names if there is someway to add domain names in as local resolution values. Any idea if there is a way to resolve domain names locallly?

If you know the entity id you can use a mapper to send the IP as a parameter as well.

Here’s an example of an automation that actually changes the overall volume on the media player device, instead of the media player specific volume.

- alias: set volume level
  trigger:
  - platform: event
    event_type: rhasspy_VolumeSet
  action:
  - service_template: shell_command.{{ trigger.event.data._site_id }}_volume_set
    data_template:
      device: "{{ trigger.event.data.device }}"
      level: "{{ trigger.event.data.level }}"
  - data_template:
      payload: "{{ trigger.event.data.device }} Volumn set to {{ trigger.event.data.level }} percent"
    service_template: >
      rest_command.rhasspy_{{ trigger.event.data._site_id }}
  id: '1620084940985'

The trigger event from rhasspy has a data field “_site_id”, which identifies the media player/rhasspy device that generated the event. I named the services and entities related to the media player to match the contents of the _site_id. This way I can use _site_id to construct the name of the service to execute. The service is a shell command that runs ssh as previously indicated. In a similar fashion I can construct the entity_id as it’s name also based on the _site_id. I think I could create an input_text for each media player an populate it with the associated IP address. I’ve not used mappers and all the examples I found were templates within an automation. Any chance you can point me to an example on how I would set up a mapper outside an automation and then access it inside the automation?

Give me your list of site_id’s and corresponding ip addresses.

bedroom 192.168.10.181
familyroom 192.168.10.245
kitchen 192.168.10.143

Using an if/else template:

  action:
  - service_template: shell_command.volume_set
    data_template:
      device: "{{ trigger.event.data.device }}"
      level: "{{ trigger.event.data.level }}"
      ip_address: >-
        {% if trigger.event.data._site_id = 'bedroom' %}
          192.168.10.181
        {% elif trigger.event.data._site_id = 'familyroom' %}
          192.168.10.245
        {% else %}
          192.168.10.143
        {% endif %}

In this example if an unknown site_id is supplied it will use the kitchen IP.

Or using mapping:

  action:
  - service_template: shell_command.volume_set
    data_template:
      device: "{{ trigger.event.data.device }}"
      level: "{{ trigger.event.data.level }}"
      ip_address: >-
        {% set site_id = trigger.event.data._site_id %}
        {% set mapper = 
          { 'bedroom': '192.168.10.181',
            'familyroom': '192.168.10.245',
            'kitchen': '192.168.10.143' } 
        %}
        {{ mapper[site_id] if site_id in mapper else '192.168.10.143' }}

Again if an unknown site_id is supplied it will use the kitchen IP, you can change this to whatever default IP you prefer.

And the single shell command becomes:

volume_set: 'ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no pi@{{ ip_address }} ./set_volume {{ device }} {{ level }}' 

Thanks, this is very helpful. I have 6 automations that all require this same logic. Is there someway to put the template or mapper logic in a macro or function so replication of the code in each antomation isn’t required?