Merging trackers statuses with automation templating

In my early spring code cleaning, I tried to combine the device_tracker.see service to do both home/not_home in one automation, but I fail somewhere with yaml.
Is it possible to send the dev_id and location name from inside the statement?

# trackers onoff
- alias: any tracker home/away
  trigger:
    - platform: state
      entity_id: 
        - sensor.rtagme
        - sensor.rtaghoney
        - sensor.rtag3
  action:
    - service: device_tracker.see
      data_template: >
        {% if trigger.entity_id' == 'home') %}
            dev_id: "{{ trigger.from_state.name }}"
            location_name: home
        {% else %}
            dev_id: "{{ trigger.from_state.name }}"
            location_name: not_home
        {% elif %}

You can’t do this. A template will always result in a single field as an output, you can’t template multiple fields.

So it’s what I feared. Ty.
Solved it by simplification :innocent::

- alias: any ble tracker state
  trigger:
    - platform: state
      entity_id: 
        - sensor.rtagme
        - sensor.rtaghoney
        - sensor.rtag3
  action:
    - service: device_tracker.see
      data_template:
        dev_id: "{{ trigger.from_state.name }}"
        location_name: "{{ trigger.to_state.state }}"

2 Likes

The automation ran but no devices were created for me.
Turn out the “{{ trigger.from_state.name }}” is not a good device name.
Piggy back on the entity_id does the trick for me, so:

- alias: any ble tracker state
  trigger:
    - platform: state
      entity_id: 
        - sensor.AddYourOwn
  action:
    - service: device_tracker.see
      data_template:
        dev_id: "{{ trigger.to_state.entity_id | regex_replace(find='sensor\\.', replace='', ignorecase=False) }}"
        location_name: "{{ trigger.to_state.state }}"
1 Like

Jinja profiles might have changed, since i made this. But ty for the update :grin: