Zone automation trigger based on past location

Hi
I am trying to build an automation/trigger with GPS.mobile.device that triggers only if all of the following coditions apply:

  1. GPS.mobile.device is moving above certain speed (> 3m/s)
  2. GPS.mobile.device is entering one of 3 zones (zone1, zone2 or zone3)
  3. GPS.mobile.device was not in home.zone or any of the other 3 zones before entering one of the 3 zones above

I figured the first 2 conditions (see code below), but how can I test the previous location (for condition 3) i.e. where the device was 30secs ago?

The device sends its location every 30 secs.

trigger:
  - platform: device
    device_id: e7206e11
    domain: device_tracker
    entity_id: device_tracker.gpsmobiledevice
    type: enters
    zone: zone.1
  - platform: device
    device_id: e7206e11
    domain: device_tracker
    entity_id: device_tracker.gpsmobiledevice
    type: enters
    zone: zone.2
  - platform: device
    device_id: e7206e11
    domain: device_tracker
    entity_id: device_tracker.gpsmobiledevice
    type: enters
    zone: zone.3

condition:
      - condition: and
        conditions:
          - condition: numeric_state
            entity_id: device_tracker.gpsmobiledevice
            attribute: speed
            above: 3
          - condition: or
            conditions:
              - condition: zone
                entity_id: device_tracker.gpsmobiledevice
                zone: zone.1
              - condition: zone
                entity_id: device_tracker.gpsmobiledevice
                zone: zone.2
              - condition: zone
                entity_id: device_tracker.gpsmobiledevice
                zone: zone.3

          - condition: not
            conditions:
              - condition: zone
                entity_id: device_tracker.gpsmobiledevice
                zone: zone.home

The very last condition should be looking back at the last known position (i.e. 30secs ago) and test for condition 3)
How can this be achieved?

Trigger.from_state

Use a template condition

{{ trigger.from_state != "zone something" }}

Would this be the correct condition formatting when using a list of zones?

  - condition: template
    value_template: >-
      {{ trigger.from_state.state != "zone.home, zone.1, zone.2, zone.3" }}

to add a condition to test the previous location before the trigger occured was not “zone.home, zone1, 2 or 3”?

This would be read as a string.

Either you need to do or

  - condition: template
    value_template: >-
      {{ trigger.from_state.state != "zone.home" or trigger.from_state.state != "zone.1"..... }}

Or you add them as separate template conditions.

1 Like

Sorry, probably need to be and not or.

Good catch about the AND for the NOT expression to work.

Final question, is it “trigger.from_state.state != …” or simply “trigger.from_state != …” to provide the previous zone of the trigger device_tracker?

FWIW, I don’t use zones much but I think you can condense that down by using a Zone trigger and using an in test in the template condition.

Your original And condition wasn’t actually doing anything, because conditions act as And by design.

For both Device and Zone triggers, trigger.from_state.state will return the previous state of the entity specified in the trigger. In both these cases if the state was a defined zone other than home, the state will be the name/friendly_name of the zone. So if the zone’s name is “Work”, trigger.from_state.state will return “Work” not “zone.work”.

trigger:
  - platform: zone
    entity_id: device_tracker.gpsmobiledevice
    zone: 
      - zone.1
      - zone.2
      - zone.3
    event: enter
condition:
  - condition: numeric_state
    entity_id: device_tracker.gpsmobiledevice
    attribute: speed
    above: 3
  - condition: template
    value_template: >-
      {{ trigger.from_state.state not in ["home", "1", "2", "3"] }}
action:
1 Like

I can’t get this work HA Home Assistant 2022.9.1. For trigger - zone, the list of zones does not seem to work. However I can create a list of entity_id, i.e.

entity_id: 
  - device_tracker.gpsmobiledevice1
  - device_tracker.gpsmobiledevice2

For some reason I thougt that had been fixed… I guess you’ll have to list out the triggers.

trigger:
  - platform: zone
    entity_id: device_tracker.gpsmobiledevice
    zone: zone.1
    event: enter
  - platform: zone
    entity_id: device_tracker.gpsmobiledevice
    zone: zone.2
    event: enter
  - platform: zone
    entity_id: device_tracker.gpsmobiledevice
    zone: zone.3
    event: enter
condition:
  - condition: numeric_state
    entity_id: device_tracker.gpsmobiledevice
    attribute: speed
    above: 3
  - condition: template
    value_template: >-
      {{ trigger.from_state.state not in ["home", "1", "2", "3"] }}

Or, what about:

trigger:
  - platform: state
    entity_id: device_tracker.gpsmobiledevice
    to: 
      - "1" 
      - "2" 
      - "3"
    not_from:
      - "1" 
      - "2" 
      - "3"
      - home
condition:
  - condition: numeric_state
    entity_id: device_tracker.gpsmobiledevice
    attribute: speed
    above: 3

One thing to keep in mind for all of these is that these will probably only work with active zones because they rely on the state of the trigger entity. Passive zones don’t give their name to the state of the device tracker, so there’s no way to check if the device was just in a passive zone.

1 Like