List of Zones in Automation

I’m trying to create a single automation for whenever a list of devices enters or leaves a list of zones. I think I have it set up except for the list of zones part.

- id: 'zone-notify'
  alias: Zone Notifications
  trigger:
  - entity_id: 
    - person.me
    - person.wife
    platform: zone
    - zone.home
    - zone.daycare
    - zone.supermarket
  condition: []
  action:
  - data:
      data:
        push:
          thread-id: zone-notify
      message: '{{ trigger.to_state.friendly_name }}''s location has changed from
        {{ trigger.from_state.state }} to {{ trigger.to_state.state }}'
      title: '{{ trigger.to_state.friendly_name }}''s Location Has Changed'
    service: notify.ios_myiPhone

However HA doesn’t like me listing out my zones like this. I don’t want to use state as a trigger because I only want to be notified of a couple of zones, not each one. Does anyone know how I would set this up without creating a ton of different automations.

Do I just need to create a trigger for each zone? That seems more doable, but I was hoping there was a cleaner way to do this.

Thoughts?

Thanks in advance!

  action:
  - data_template:    #### <-------
      data:
        push:
etc...

Also you might have a problem with this bit:

friendly_name }}''s

The double single quote will escape the second single quote.

Try using double quotes:

      message: "{{ trigger.to_state.friendly_name }}'s location has changed from
        {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"
      title: "{{ trigger.to_state.friendly_name }}'s Location Has Changed"

I’m not sure I understand, the issue seems to be with the trigger, not the action unless I’m misunderstanding what you’re saying.

I’m saying your template wont work without calling it a template. Also you may have a problem with quotes.

As to the trigger, I’ll let someone who uses zone triggers answer.

Thank you Tom! This would probably be needed once I get the zone issue sorted out. For now I have a number of triggers in the same automation, but I would still like to narrow it down to just a list of zones if possible instead of creating a trigger for each one.

Now to see if I can update my location without actually going somewhere, if not I gotta wait until tomorrow morning to test this.

Try a trigger & condition, something like this:

  trigger:
    platform: state
    entity_id:
    - person.me
    - person.wife
  condition:
    condition: template
    value_template: >
      {% set zones = 'home',
                     state_attr('zone.daycare', 'friendly_name'),
                     state_attr('zone.supermarket', 'friendly_name') %}
      {{ trigger.from_state and trigger.to_state and
         trigger.from_state.state != trigger.to_state.state and
         (trigger.from_state.state in zones or trigger.to_state.state in zones) }}

Thank you Phil! I would love to use something like that, but I won’t unless I can actually understand it and right now that is a little over my head haha. I don’t want to just copy/paste it and move on, is there a resource that could help me translate that into something I could comprehend? I understand the conditions are a list of zones, and it looks like you’re making sure the from.state isn’t the current state, but I don’t get all the rest or how it’s formatted fully.

Start with the standard HA documentation, especially Templating and State Objects.

When you use a state trigger, and do not specify to: or from: options, then it will trigger whenever any part of the entity’s state changes, including attributes. In the condition and action parts of the automation there will be a variable defined named trigger which contains details of the state of the entity that changed before (trigger.from_state) and after (trigger.to_state) the change.

Basically the condition is making sure that both the before & after states are not None (which can happen when the entity is first created, or removed, respectively.) If they are both not None, then it checks to make sure the State Objects’ states are not the same (i.e., the “state” has actually changed, and not just an attribute.) Lastly it checks that either the before or after “state” is one of the zones you care about (i.e., you have either left one of these zones, or entered one of these zones.)

The first statement of the condition is just setting a “local variable” in the template to be a list of the zones you care about. Note that when you are “Home”, the state is actually home, whereas when you’re in any other zone the state is the “friendly name” of the zone.

Thank you for the explanation, I’ve been messing around a little bit with templates since yesterday, so between your explanation and what I’ve learned this actually does make sense now.

Just to clarify, this is the check to make sure that they have a value and aren’t blank?

{{ trigger.from_state and trigger.to_state }}

If that’s the case then I think I understand this just about perfectly where I would feel comfortable using it in my config. I’m loving HA so far, and learning more and more about it every day. It’s a learning curve for sure but the things I can do with it (compared to SmartThings) is absolutely insane.

Yes. If there is no previous state, then trigger.from_state will be None, which evaluates to False. And the same thing for trigger.to_state.

I also moved from SmartThings (about a year or so ago) and have never looked back! :slight_smile:

Doesn’t it need an event like “enter” or “leave”

All the way at the bottom is zone trigger