Double trigger? home, not_home

states return states, not entity_id’s. If this is a device tracker that you are refering to, then it will return the friendly name of your zone, not the zone entity_id.

Turn logging on to debug and watch a device tracker when it changes zones. It will have 5 attributes in it, 2 strings and 3 dictionaries. The one string will be the entity_id that sparked the trigger, i.e. your device_tracker. The other string will be the platform, which should be just the word ‘state’. Two of the dictionaries are to_state and from_state. The last should be the ‘for’ dictionary if you have a duration attached to the trigger that fired it.

There should be a ton of info inside the to_state and from_state: entity_id, state, and attributes (all the device_tracker information). It will not contain any zone information accept the zone friendly name as the state.

So, if you want the zone object, what you’ll need to do is search the zones for the zone entity_id via the zone friendly name.

{% set zones = states.zone | selectattr('name','eq', trigger.to_state.state) | list %}
{% if zones | length >= 1 %}
# get the first zone that is equal to the zones in the to_state
{% set triggerzone = zones[0] %}
1 Like

Brilliant (as always)
Thank you.

states.zone are all zones with the prefix zone (check {{states.zone|list}} in the dev-template) and all of their attributes

in my template, I simply take care that the to_state or from_state are in the list of states.zone. If yes, the template checks and the automation continues.

I do not need all the attributes, but simply their state. (entity_id’s) so I filter these out by using

{{states.zone | map(attribute='entity_id')|list}} and compare the trigger.to_state.state and from_state.state to this:

      {% set zones = states.zone | map(attribute='entity_id')|list %}
      {{trigger.to_state.state in ['home','not_home'] or
        trigger.from_state.state in ['home','not_home'] or
        trigger.to_state.state in zones or 
        trigger.from_state.state in zones}}

I cant really understand how what you suggest does the same for me? Should I read it as : if the trigger.to_state.state is in the list, the list length >= 1, so get the first zone equal?

change this line and your stuff will work:

{% set zones = states.zone | map(attribute='name')|list %}

but it already does…trigger.to_state.state must match zone.location, not Location… Or? The state is zone.location, not Location name.

All my device tracker states return the zone friendly name. Maybe your friendly names are the same as your entity id’s

im really confused now, because this:

        {% set name = trigger.to_state.attributes.friendly_name %}
        {% set to_state = trigger.to_state.state %}
        {% set from_state = trigger.from_state.state %}
        {{as_timestamp(now()) | timestamp_custom("%X") }} :
        {% if to_state == 'not_home' %}
          {{-name }} left {{from_state}}
        {% elif from_state == 'not_home' %}
          {{-name }} arrived at {{to_state}}
        {% else %}
          {{-name }} left {{from_state}} and arrived at {{to_state}}
        {% endif %}

returns the to_state as you say, its friendly_name or name.

But how come the value_template passes then?

well, what does it always output? The last line?

It outputs any of the 3 lines;

person1 arrived home, or one of the defined zone friendly_names,
person1 left home,
and also: person1 left home and arrived at zone.friendly_name…