And condition in automation

Hello there,

i tried to turn of lights automaticly when two named persons aren’t home.
I read the documentations about Conditions form HA, but all of these 3 options aren’t working.

Sorry when this questions is asked often. I found a similar topic with the same problem, but there solution was like option 1.

here ist my code:

condition:
  condition: and
  conditions:
    - condition: state
      entity_id: "device_tracker.xy"
      state: "not_home"
    - condition: state
      entity_id: "device_tracker.xx"
      state: "not_home"

Did I missunderstood something or ist it just on my HA instance?

Thank you.

Conditions are and by default anyway

Your condition checks that both of those are not in any zone. If they’re in a zone then the conditions won’t be true.

If those are the only two people you care about, use the state of zone.home instead - it’s the number of person entities at home.

Otherwise, check that they’re not home:

condition:
  condition: not
  conditions:
    - condition: state
      entity_id: "device_tracker.xy"
      state: "home"
    - condition: state
      entity_id: "device_tracker.xx"
      state: "home"

I will try to use

condition: and

Before seconds condition and test those conditions.

As a condition, this should return true when both persons are not_home. Technically the and isn’t needed here: conditions that are placed below one another in an automation are by default ‘and’ (unless they are in an ‘or’). You only ever need the 'and ’ inside an ‘or’ for complex tests.

But that is not your problem. What you are missing is a trigger. When should HA check if both people are not home? This should be when either device tracker changes from home to something else. So you need two state triggers above the conditions. Conditions are just things that must also be true when the trigger happens.

There is one other potential problem with the automation. When you define more zones, the state can also have other values, such as “Work” or “School”. So the best way is to use a “not” combined with “home”.

That won’t make any difference, and if you do it that way it’s not even going to do what you think it will anyway.

This isn’t the same: this will return true if only one is away and the other at home. Both need a not, or you need an or insode the not.

1 Like

Check my post above - I think you’re missing the difference between conditions and triggers. This is not a complete automation, it is just a test. (Aside from all the other possible and needed improvements).

This does what you probably want to achieve - send a message when the last person leaves home:

alias: example
description: ""
trigger:
  - platform: state
    entity_id:
      - zone.home
    to: "0"
    not_from:
      - unavailable
      - unknown
condition: []
action:
  - service: notify.persistent_notification
    data:
      message: The last person left home
mode: single

If you are only interested in two persons being gone, and not any others, it would be this (so a third person could still be home!):

alias: example
description: ""
trigger:
  - platform: state
    entity_id:
      - device_tracker.jasper_s_telefoon
      - device_tracker.rianne_s_telefoon
    from: home
    not_to:
      - unavailable
      - unknown
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: device_tracker.jasper_s_telefoon
        state: ""
  - condition: not
    conditions:
      - condition: state
        entity_id: device_tracker.rianne_s_telefoon
        state: home
action:
  - service: notify.persistent_notification
    data:
      message: The both Rianne and Jasper are not home
mode: single
1 Like

thank you.

zone.home won’t work in my instance, cause gps tracking is disabled on the devices.
i use nmap to scan if they are connected to the wifi.

but “home” ist either the option, so i gonna test it

I’m sorry, i didn’t post all of my code.
Of course i’ve set a trigger for both persons.

platform: state
entity_id:
  - device_tracker.xy
  - device_tracker.xx
from: home
to: not_home
for:
  hours: 0
  minutes: 0
  seconds: 0

i’m not using the zones from HA.

I just use the nmap tracker addon to check the wifi connections.

this is what the automations webpage shows me, when i want to change the configs in YAML …
These 3 steps (trigger, conditions and action) are seperated for me

The home zone is standard in HA, so you can use it if you want. But what isn’t working? what is in the trace of the automation?

1 Like

Sure i could use the standard zone “home”, but nobody will ever be “home” cause tracking is disabled on the mobile phones.

Now it is working. I was wrong with editing on the webpage.
I tried to use the “condition: and” function in another function in condition. So i mixed up functions in the condition. my fault.

Next time i will instant code in the YAML file of HA.

the option i tried at first is the right one.

this is the working automation:

- id: '1670877527166'
  alias: name
  description: ''
  trigger:
  - platform: state
    entity_id:
    - device_tracker.xy
    - device_tracker.xx
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: device_tracker.xy
      state: not_home
    - condition: state
      entity_id: device_tracker.xx
      state: not_home
  action:
  - type: turn_off
    device_id: ca46e5c9f58311f4371bbc3e15781858
    entity_id: switch.chillroom_lampe_1
    domain: switch
  - type: turn_off
    device_id: ca46e5c9f58311f4371bbc3e15781858
    entity_id: switch.chillroom_lampe_2
    domain: switch
  - type: turn_off
    device_id: c9309b1e2b8e7e2ccb0744aba76a3ffd
    entity_id: switch.tasmota_2
    domain: switch
  - type: turn_off
    device_id: e7a732f94d5a3d8c23b7b1efda0b7680
    entity_id: light.controller_rgb_ir_ced6e8
    domain: light

This automation is used to turn of all lamps of our rooms, when both of us leaves the home

If you link the device trackers you mention to the right person, it will work with the home zone. It doesn’t need to be a phone tracker or even a gps tracker. IP tracking from the router will work just as well, provided your battery does not die or wifi isn’t turned off. Because then the router will think your gone and kill the lights, same as with your automation I’m afraid. Or is it a BLE tracker?

Sorry, I don’t know what a BLE Tracker is^^

I use for this NMAP Tracker. When I’m right, it creates a new Entity named device.tracker.
But ur right, if the battery dies or the wifi is turned of, it will kill the lights.
I think it is a realy rare chance, that both batteries dies at the same time or either the wifi will be turned off.

If you eliminate the redundant/repetitive bits, you can reduce it to this:

- id: '1670877527166'
  alias: name
  description: ''
  trigger:
    - platform: state
      entity_id:
        - device_tracker.xy
        - device_tracker.xx
      from: home
      to: not_home
  condition:
    - condition: state
      entity_id: device_tracker.xy
      state: not_home
    - condition: state
      entity_id: device_tracker.xx
      state: not_home
  action:
    - service: homeassistant.turn_off
      target:
        entity_id:
          - switch.chillroom_lampe_1
          - switch.chillroom_lampe_2
          - switch.tasmota_2
          - light.controller_rgb_ir_ced6e8

BLE = Bluetooth low energy, e.g. an airtag or tile. I hope you don’t get too many firmware updates for your wifi access point :wink:

ahh okay, so it’s not a BLE :smiley:

Noo, It think they are to old for updates now :wink:

Thanks everyone who helped or tried to help me!
I close this topic now.
The “problem” was wrong configuration from me.

Ah nice okay, that looks way better.
But brings it something more than just a better look?

It brings something less. :wink:

Less code to get the same results.

Home always works, home is more than just the area around your house, it’s also what any local presence detection integrations (liike nmap) use.