Any way to consolidate my zones automation?

Right now I have 6 zones, each zone has different triggers for enter and leave. So between the 6 zones, there’s 12 different blocks of code.

    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.one
      event: enter
    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.one
      event: leave
    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.two
      event: enter
    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.two
      event: leave

etc…etc…

I was hoping to something like the templating last option that Pedro lists here, but you can’t have generic zone trigger without event and zone.

It also doesn’t appear you can put multiple zones or events in the trigger either.

    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.one, zone.two
      event: leave, enter

Is there any way to cutdown on this code?

This is where you’d use a template condition using the trigger object. Documented here.

trigger:
    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
condition:
- condition: template
  value_template: >
    {{ trigger.zone in [ 'zone.one', 'zone.two' ] and trigger.event in ['enter','leave'] }}

EDIT: Just tailor your condition to do whatever you want. This block of code is eqivalent to your 4 triggers.

I tried that, but HA doesn’t validate. It doesn’t like not having zone or event in the trigger

Invalid config for [automation]: required key not provided @ data[‘trigger’][0][‘zone’]. Got None.

hmm, it seems that zone is required. I’m not sure how we can get around that.

worse case is that you can simplify it to this:

    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.one
    - platform: zone
      entity_id: device_tracker.life360_1,device_tracker.life360_2
      zone: zone.two

if you don’t care about entering or leaving you can omit that. It doesn’t really spell out that it’s required. If it is, then you’ll have to keep all the separate triggers.

I did notice this way didn’t give me a validation error, then I could do a condition template like above for trigger.event.

Now I’m trying to figure out how I can test this while being isolated at home.

that’s the problem I ran into a minute ago.

Thanks this worked, I used device_tracker.see service for testing.

1 Like

For some reason the leave isn’t working in this, I only get enter messages. I changed the action in the message if trigger.event to enter (from leave) and it triggered properly (gave me the left message). It just doesn’t seem to send a leave trigger.event.

  condition:
    - condition: template
      value_template: >-
        {{ trigger.event in ['enter','leave'] }}

  action:
    data_template:
      message: >-
        {% if trigger.event == 'leave' %}
          {{ trigger.to_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }} at {{now().strftime("%c")}}
        {% else %}
          {{ trigger.to_state.attributes.friendly_name }} arrived at {{ trigger.zone.attributes.friendly_name }} at {{now().strftime("%c")}}
        {% endif %}

Ah, this is why: https://github.com/home-assistant/core/blob/8c6506227150c74083b9827975ec19af20c68ff9/homeassistant/components/automation/zone.py#L19

If no event is listed in the platform: zone, it just defaults to enter so not possible to template.