Automation templating: dynamic variable/entity_id

Hi Community,

I’ve been hard at work refactoring my python script for GPS but I’ve run into a problem. Not sure what I’m trying to do is even possible (custom component) or if I’m just going about it the wrong way so I’m asking for help.

Previously, I could dynamically alter an entity_id from within a python script; this no longer works (per se) but does work with the custom component variable. My only stumbling block is the actual data portion of the automation in trying to “generate” a custom entity to pass to HA:

- alias: GPS Filter
  trigger:
    - platform: state
      entity_id: device_tracker.homeassistant_chris, device_tracker.vals
  action:
    - service: python_script.calc_gps_coords
      data_template:
        entity_id: '{{ trigger.entity_id }}'
        hist_entity: '{{ trigger.to_state.attributes.friendly_name }}'
        zone_entity: 'None'
        zone_data: 'None'
    - service: variable.set_variable
      data:
        variable: 'gps_{{ trigger.to_state.attributes.fiendly_name }}'
        attributes_template: >
            {
              "latitude": "{{ states.trigger.entity_id.attributes.latitude }}",
              "longitude": "{{ states.trigger.entity_id.attributes.longitude }}"
            }

Of course this doesn’t work (2nd or 3rd iteration trying different methods.) I’ve seen some example of this in other threads but it’s not exactly 1:1

- service: notify.ios_johnnys_iphone
    data_template: 
      message: "{{ states('sensor.local_time') }} Garage {{ trigger.to_state.state }}"
      data:
        push:
          category: camera
        entity_id: camera.{{ trigger.entity_id.split('.')[1] }}

Is there a way for me to dynamically pass gps_chris (or gps_val) here? Or am I stuck making two automations? Once variable.gps_chris (for example) is populated, I can easily (now) modify it in my script but I’d like to keep it to one automation if it’s even possible.

I spread my hands before you all in hopes someone can help - I’d really like to pass the entity_id dynamically as it appears can be done (see example.)

Try changing the variable.set_variable to a data_template.

- service: variable.set_variable
  # data:
  data_template:
    variable: 'gps_{{ trigger.to_state.attributes.fiendly_name }}'
    # attributes_template: >
    attributes:
        #{
          "latitude": "{{ states.trigger.entity_id.attributes.latitude }}",
          "longitude": "{{ states.trigger.entity_id.attributes.longitude }}"
        #}
1 Like

Thanks, unfortunately, it’s still not working:

[homeassistant.helpers.service] Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'

I also had to keep the {}'s as it failed configuration check otherwise. Even tried ‘entity_id’ instead of ‘variable’ but no luck.

Did you fix the errors under attributes in the code posted? I just noticed latitude and longitude are spaced in 6. should only be 2 and I left the quotes round them and the comma at the end of latitude.

- service: variable.set_variable
  data_template:
    variable: 'gps_{{ trigger.to_state.attributes.fiendly_name }}'
    attributes:
      latitude: "{{ states.trigger.entity_id.attributes.latitude }}"
      longitude: "{{ states.trigger.entity_id.attributes.longitude }}"

Appreciate the help. This time, I removed everything I had and pasted exactly as you had (passes config check) but still the same error:

[homeassistant.helpers.service] Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'

so it seems variable: 'gps_{{ trigger.to_state.attributes.fiendly_name }}' isnt working to me.

I would check its not a case issue with the friendly name like a capital first letter in Chris making gps_Chris when it should be gps_chris

Case shouldn’t make a difference; I’ve used it interchangeably before unless this is something new.

I’m not sure with that custom component but some stuff it is.

I’m not in a position to test stuff at the moment but the test I would do from here;

  • Hardcode the variable - change variable: gps_chris to confirm the rest is working.
  • If it is try it with variable: gps_Chris to confirm there’s no case issue (you could be right but just entertain me for a second)

if that all works then we know its an issue with variable: 'gps_{{ trigger.to_state.attributes.fiendly_name }}'

Looks like an issue with the custom component itself; I reset it back to my hardcoded values and it works, as seen here:

I’m going to have to find another method to do this. Perhaps it’s now time to move to Node Red.

1 Like

@PeteCondliffe @chrisw. Not sure if this is the cause of your problem, but friendly_name is spelled wrong. Missing the r.

Also, the friendly name can contain spaces. Not sure if that’s what you want either.

2 Likes

Yup, I fixed that in the original paste :slight_smile: Thanks tho!