Detect zone absence at two adjacent zones

My home area is somewhat lengthy and pretty small. Therefore I have two zones. The smaller part containing the house, resembles the „home“ area, the larger part next to it is the „garden“ area.
I want to check if there is an open window, in case, I am leaving. I established an automation, which works so far, as it tells me, if the window is open. The presence detection is done via companion app, as it is described in the HA documents.
However, it should not alert, when changing from „home“ to „garden“ and back, what it currently does. If I include both zones into one giant „home“, I will be several hundred meters away, until the the alert occurs. IF the GPS signal is not good, it takes more than 1km until the alert uccurs. I tried to add a ping detection. But the Wifi is not far enough to reach all of the „Garden“ zone. And, in addition, Wifi is closed at night, so it would not be continously reliable. So, this is no improvement.
Is there a more sophisticated way to combine the two zones into one small and lengthy area and not one huge circular zone?

Just for those, that need YAML code to judge, what I am struggling with

alias: Verandatür ist auf
description: ""
mode: single
triggers:
  - device_id: 521453e9bcb1d037f9c56fd3d0f9fbec
    domain: device_tracker
    entity_id: 9b60aa4e0ed761a7d77be297ae4fc277
    type: leaves
    zone: zone.home
    trigger: device
  - device_id: 521453e9bcb1d037f9c56fd3d0f9fbec
    domain: device_tracker
    entity_id: 9b60aa4e0ed761a7d77be297ae4fc277
    type: leaves
    zone: zone.garden
    trigger: device
conditions:
  - type: is_open
    condition: device
    device_id: 293f1bc7f630438d59833c67080458f0
    entity_id: 73c05cdd5c416385a0b2bd0c889de7d5
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
actions:
  - action: notify.mobile_app_phone
    metadata: {}
    data:
      message: Verandatür ist auf!

If you use Node red then you can draw a polygon around for instance the grass area of Tower of London.

This could then set a sensor or set a device tracker.

You should only need to add a condition that checks the data available from the underlying Zone trigger.

alias: Verandatür ist auf
description: ""
mode: single
triggers:
  - device_id: 521453e9bcb1d037f9c56fd3d0f9fbec
    domain: device_tracker
    entity_id: 9b60aa4e0ed761a7d77be297ae4fc277
    type: leaves
    zone: zone.home
    trigger: device
  - device_id: 521453e9bcb1d037f9c56fd3d0f9fbec
    domain: device_tracker
    entity_id: 9b60aa4e0ed761a7d77be297ae4fc277
    type: leaves
    zone: zone.garden
    trigger: device
conditions:
  - condition: template
    value_template: |
      {% set garden = state_attr('zone.grundstuck', 'friendly_name') %}
      {{ trigger.to_state.state not in ['home', garden] }}
    condition: device
    device_id: 293f1bc7f630438d59833c67080458f0
    entity_id: 73c05cdd5c416385a0b2bd0c889de7d5
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
actions:
  - action: notify.mobile_app_phone
    metadata: {}
    data:
      message: Verandatür ist auf!

Unfortunately, this does not work. I get the error message:
jinja2.exceptions.UndefinedError: 'trigger' is undefined

You can’t run the automation using the run actions button

Ok, it‘s raining. So, better keep the door closed and stay at home. Wait until tomorrow. Thanks!

Well, today it is sunny anf fine for testing. However, no success.
Step one, leaving garden, succeeded as always and as expected. But the template did not:

result: false
entities:
  - zone.garden

stopped the automation.
Maybe, I missed something, but I used the template exactly as proposed.

Download and post the debug Trace from the run that didn’t work as expected… there’s not much we can diagnose without data.

Well, here we are. There are some slight changes in the name of the phone and the name of the zone.garten, which is the friendly name in HA, but the entity says zone.grundstuck. I replaced these names, but no luck anyway.
Because of security, I truncated the GPS location data, ok?

{
  "trace": {
    "last_step": "condition/0",
    "run_id": "2d9bb4deca3a1ab3ac55b1bce4043bf8",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2026-03-29T15:12:22.349170+00:00",
      "finish": "2026-03-29T15:12:22.350084+00:00"
    },
    "domain": "automation",
    "item_id": "1729874920431",
    "trigger": "device_tracker.sm_s908b leaving Garten",
    "trace": {
      "trigger/1": [
        {
          "path": "trigger/1",
          "timestamp": "2026-03-29T15:12:22.349347+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.verandatur_ist_auf",
              "state": "on",
              "attributes": {
                "id": "1729874920431",
                "last_triggered": "2026-03-29T09:55:07.495214+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Verandatür ist auf"
              },
              "last_changed": "2026-03-29T04:31:54.913495+00:00",
              "last_reported": "2026-03-29T09:55:07.735571+00:00",
              "last_updated": "2026-03-29T09:55:07.735571+00:00",
              "context": {
                "id": "01KMWG9HK6TFX1EEEF29KXG1AT",
                "parent_id": "01KMWG9HK581GWZ07371DGCE3C",
                "user_id": null
              }
            },
            "trigger": {
              "id": "1",
              "idx": "1",
              "alias": null,
              "platform": "device",
              "entity_id": "device_tracker.sm_s908b",
              "from_state": {
                "entity_id": "device_tracker.sm_s908b",
                "state": "home",
                "attributes": {
                  "source_type": "gps",
                  "latitude": 54.000,
                  "longitude": 10.000,
                  "gps_accuracy": 32,
                  "altitude": 79,
                  "course": 0,
                  "speed": 0,
                  "vertical_accuracy": 0,
                  "icon": "mdi:cellphone",
                  "friendly_name": "Peters S22 Ultra"
                },
                "last_changed": "2026-03-29T15:09:19.727034+00:00",
                "last_reported": "2026-03-29T15:09:19.727034+00:00",
                "last_updated": "2026-03-29T15:09:19.727034+00:00",
                "context": {
                  "id": "01KMX28VZFVZS2XMB3YXTBS0K8",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "device_tracker.sm_s908b",
                "state": "home",
                "attributes": {
                  "source_type": "gps",
                  "latitude": 54.000,
                  "longitude": 10.000,
                  "gps_accuracy": 10,
                  "altitude": 80,
                  "course": 0,
                  "speed": 0,
                  "vertical_accuracy": 0,
                  "icon": "mdi:cellphone",
                  "friendly_name": "Peters S22 Ultra"
                },
                "last_changed": "2026-03-29T15:12:22.347964+00:00",
                "last_reported": "2026-03-29T15:12:22.347964+00:00",
                "last_updated": "2026-03-29T15:12:22.347964+00:00",
                "context": {
                  "id": "01KMX2EEAB8MBNBGBSHZK2ERG2",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "zone": {
                "entity_id": "zone.grundstuck",
                "state": "0",
                "attributes": {
                  "latitude": 54.000,
                  "longitude": 10.000,
                  "radius": 71.36104172896837,
                  "passive": false,
                  "persons": [],
                  "editable": true,
                  "icon": "mdi:forest",
                  "friendly_name": "Garten"
                },
                "last_changed": "2026-03-29T14:38:05.540075+00:00",
                "last_reported": "2026-03-29T14:38:05.540075+00:00",
                "last_updated": "2026-03-29T14:38:05.540075+00:00",
                "context": {
                  "id": "01KMX0FNQ4BE3Q763VCKTS4YNJ",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "event": "leave",
              "description": "device_tracker.sm_s908b leaving Garten"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2026-03-29T15:12:22.349460+00:00",
          "result": {
            "result": false,
            "entities": [
              "zone.grundstuck"
            ]
          }
        }
      ]
    },
    "config": {
      "id": "1729874920431",
      "alias": "Verandatür ist auf",
      "description": "Warnung Verandatür auf",
      "triggers": [
        {
          "device_id": "521453e9bcb1d037f9c56fd3d0f9fbec",
          "domain": "device_tracker",
          "entity_id": "9b60aa4e0ed761a7d77be297ae4fc277",
          "type": "leaves",
          "zone": "zone.home",
          "trigger": "device"
        },
        {
          "device_id": "521453e9bcb1d037f9c56fd3d0f9fbec",
          "domain": "device_tracker",
          "entity_id": "9b60aa4e0ed761a7d77be297ae4fc277",
          "type": "leaves",
          "zone": "zone.grundstuck",
          "trigger": "device"
        }
      ],
      "conditions": [
        {
          "condition": "template",
          "value_template": "{% set garden = state_attr('zone.grundstuck','friendly_name') %}\n{{ trigger.to_state.state not in ['home', garden] }}\"\n"
        },
        {
          "type": "is_open",
          "condition": "device",
          "device_id": "293f1bc7f630438d59833c67080458f0",
          "entity_id": "73c05cdd5c416385a0b2bd0c889de7d5",
          "domain": "binary_sensor",
          "for": {
            "hours": 0,
            "minutes": 2,
            "seconds": 0
          }
        }
      ],
      "actions": [
        {
          "action": "notify.mobile_app_s22_galaxy",
          "metadata": {},
          "data": {
            "message": "Verandatür ist auf!"
          }
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01KMX2EEACCVDHFYAZZ8NHEDRN",
      "parent_id": "01KMX2EEAB8MBNBGBSHZK2ERG2",
      "user_id": null
    }
  },
  "logbookEntries": []
}

HTH

In the meantime I tried to understand the template. My actual intention is: it does not work as it should, because I deleted the ‚not‘ in ‚trigger.to_state.state not in‘ just to inverse the condition. But the result still remains ‚false‘.
Maybe templates do not work like expected in dockerized HA?

The trigger triggers when you leave home or garden, so the condition can’t be in home/garden.
That will obviously not work

Well, my idea was simple: When the result is ‚false‘, then try the opposite. It should be ‚true‘ then. But, obviously, any case is ‚false‘. So, I doubt, that this template construct works properly.

Try it in developer tools → template and use the developer tools → states page to set peoples state to something.
Like home and away

There is a typo in the template, an extra " at the end.

However, the whole thing is confusing because it looks like the garden zone is set to be passive, because it’s not registering as the state of the device tracker, but the Trace states that it is not a passive zone… so I don’t know what to tell you.

Do the zones overlap? Maybe that’s what’s causing the discrepancy between the tracker state and the zone crossing?

Well, I can see in the activities list, that, when walking to the zone garden, it changes from 0 to 1. Going back to zone home, it changes back to 0. The zones do overlap on a short seqment. But usually I change the zones outside of this overlapping part.

There is no double quote “ anywhere in the template. Maybe, it was introduced while copying the code here? Or should it be somewhere?

Although the developer tools, up to now, are kind of a black box for me, I managed to change the state of person to „away“. This, indeed, immediately triggered the automation to set the result to ‚false‘ again and stopped then. So far, I do not need to go out and leave the zone, but can do the tests at home now. Thanks for this hint! But actually, I do not proceed.

Maybe you already fixed it, but it’s definitely in the Trace… see the \" after the }} near the end of the template:

Ah, I see. But actually, it is gone. Just checked the traces. Don‘t know, where it came from. Maybe, I messed it up. Anyway, there is no success even without it.

What if you just set the condition to require the to state value be “away”?

  - condition: template
    value_template: "{{ trigger.to_state.state == 'not_home' }}"

Still results in ‚false‘

I think, that this kind of template construct does not work in docker HA.
Bad news.