Working with zones, away and not_home - I simply don´t get it

Hi there, I have created the below automations, which are supposed to turn off the lights when either my wife or me is leaving the house and nobody is home.

Sometimes it´s working, sometimes not. I believe it´s because I don´t understand whats the difference between a zone, away and not_home.

An example when it´s working:
We´re leaving both almost at the same time

An example when it´s NOT working:
I´m leaving earlier, and as I only need 5 minutes with my bike to get to work, I´m entering the zone “work” before my wife is leaving the home zone. When she´s leaving, none of the 2 automation will be triggered.

Now, my understanding was when I´m in another zone, which is not the home zone, I´m “not_home”, and if I´m in none oi my defined zones, I am “away”.
But obviously not, can someone fix my wrong thinking?

Thanks for any help…

Here is the code:

- id: '42'
  alias: Leaving-home-weekday-01
  trigger:
  - entity_id: person.rebecca
    event: leave
    platform: zone
    zone: zone.home
  condition:
    condition: and
    conditions:
    - condition: state
      state: not_home
      entity_id: person.philipp
    - condition: time
      after: 06:00:00
      before: 08:00:00
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
  - service: light.turn_off
    entity_id: group.all_lights
  - service: switch.turn_on
    entity_id: switch.foscam_wohnen
##################################################################################################################################
- id: '43'
  alias: Leaving-home-weekday-02
  trigger:
  - entity_id: person.philipp
    event: leave
    platform: zone
    zone: zone.home
  condition:
    condition: and
    conditions:
    - condition: state
      state: not_home
      entity_id: person.rebecca
    - condition: time
      after: 06:00:00
      before: 08:00:00
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
  - service: light.turn_off
    entity_id: group.all_lights
  - service: switch.turn_on
    entity_id: switch.foscam_wohnen

Try something like this…

  - entity_id: person.liz
    from: 'home'
    platform: state
    to: 'not_home'

Thanks, I will try.

But what exactly is “not_home”? When I´m at work in my work zone, I´m physically not at home, but I can´t find this state anywhere, as my person.philipp entity is at “work”.

So you have a work zone then? But when you leave home it must go from home to not_home to work? Maybe check the zone docs… I’m only interested in home or not_home here.

‘away’ is just a frontend representation of the actual ‘not_home’ state. In conditions or triggers, you always use home/not_home (unless of course you have made your own device_trackers that actually have a state ‘away’, but I would suggest you’d not do that…)

further more, if you have zones defined, a device_tracker can either be in that/those zones, or be home/not_home. Of course a zone would also be not_home (except for the zone.home…) but you’d need to check for these states in a condition or trigger.

to give you an idea:

      - condition: template
        value_template: >
          {% set zones = states.zone | map(attribute='name')|list %}
          {{trigger.to_state.state in ['home','not_home'] or
            trigger.from_state.state in ['home','not_home'] or
            trigger.to_state.state in zones or 
            trigger.from_state.state in zones}}

this checks on both home/not_home and the zones. Iow, if a device is in a zone, it is not home/not_home…

1 Like

But the “not_home” state gets overwritten as soon as I enter any of my defined zones, right? This is confusing me, because for me “not_home” is NOT home, even if I´m in a zone called work, sports or something else.

@David:
Yes, when I´m leaving home the state goes from “home” to “not_home”. But after 5 Minutes it changes from “not_home” to “work”
And when second person leaves home, the automation checks the state from my person for “not_home”, and because this is not true (as the state is “work”) the automation won´t trigger.
I guess maybe a combinded or condition would help to check if I´m at work

1 Like

in the first automation you could add an or for person.philipp to be in a zone, hence my code snippet.

well, you should get to get to grips with HA Logic, don’t fight it… :wink:

of course it depends on the type of device_tracker. A simple network tracker is always home/not_home, Life360 or other gps trackers can follow the zones, and a person can be a composite tracker, all depending on the trackers you define the person to be based on.

Agree with Marius, but you could use is not_state ‘home’ or do a or condition. I’d try the not_state for simplicity myself.

btw you can write this as follows, since conditions are by default ‘and’-ed:

condition:
  - condition: state
    state: not_home
    entity_id: person.philipp
  - condition: time
    after: 06:00:00
    before: 08:00:00
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri

having said that, I would suggest the use of condition: template, which has the benefit of being able to test the conditions in the developer/template page.

 condition: template
 value_template: >
   {{is_state('person.philipp','not_home')}}

and also makes it rather easy to add the ‘or’ bit in the same template, as I suggested above.

{{state('person.philipp') != 'home'}}
1 Like

yes, that’s a very fine suggestion!
only downside is it doesnt positively check on being in another zone. But very useful to start with indeed.

small typo:

{{states('person.philipp') != 'home'}}

Ha! Thanks for finding my typo. Benefit is it’s just the one condition and he’s only interested in home…

yes and you’ve perfectly templated what OP stated here:

cool. so simple…
for completeness’ sake, note it can also be:

{{ not states('person.philipp') == 'home'}}
3 Likes

thanks for the suggestions guys, I will work through it in a quiet hour and some coffee this weekend. :+1: :slightly_smiling_face:

And also:

{{ not is_state('person.philipp', 'home') }}

Variety is the spice of life, as they say.

1 Like

Hi guys,

I am trying to use a condition like these but it doesn’t work and I can’t fugure out why. What am I missing ?

- id: progr_chauff_bureau_start
  alias: progr_workday_office_start
  trigger:
    - platform: time
      at: input_datetime.workday_office_start
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sensor.workday
        state: "True"
      - condition: state
        entity_id: sensor.homeworking
        state: "True"
      - condition: state
        entity_id: input_boolean.chauffage
        state: "on"
      - condition: template
        value_template: >
          "{{ states('person.raph_k', 'home') }}"
  action:
    #   - service: climate.turn_on
    #     entity_id: climate.chauffage_bureau
    - service: climate.set_temperature
      data_template:
        temperature: "{{states.input_number.target_temp_bureau.state}}"
      entity_id: climate.chauffage_bureau

Without the template condition, everything works just fine.

Thank you,

R.

If it can help someone, I couldn’t figure out why my previous condition did not work, however after a check in the conditions documentation https://www.home-assistant.io/docs/scripts/conditions/#template-condition, I changed the format to this:

  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sensor.workday
        state: "True"
      - condition: state
        entity_id: sensor.homeworking
        state: "True"
      - condition: state
        entity_id: input_boolean.chauffage
        state: "on"
      - "{{ is_state('person.raph_k', 'home') }}"

Now it works juste fine

Bit disturbed that you seem to have text-based sensors for workday and homeworking. It would be more elegant to have them as binary_sensors that are 'on' or 'off'.

You can simplify further, as conditions are AND-ed by default. Without changing your sensors:

 condition:
   - condition: state
     entity_id: sensor.workday
     state: "True"
   - condition: state
     entity_id: sensor.homeworking
     state: "True"
   - condition: state
     entity_id: input_boolean.chauffage
     state: "on"
   - "{{ is_state('person.raph_k', 'home') }}"

…and there’s no real need to use a template for your home detection. You could just as easily do this, like all your other conditions:

   - condition: state
     entity_id: person.raph_k
     state: "home"

For these sensors, on my lovelace I have them used as switches in order to preset the days of the week as work or not; homeworking or not (for the heater).

(my sensors to determine it (same for homeworking))

  - platform: template # determine if today is selected as working day or day off
    sensors:
      workday:
        entity_id: sensor.date
        value_template: >
          {% set sensor_names = [ 'monday', 'tuesday', 'wednesday','thursday','friday','saturday','sunday'] %}
          {% set today_name = sensor_names[now().weekday()] %}
          {% set entity_id = 'input_boolean.offwork_'+today_name %}
          {{ is_state(entity_id, 'on') }}

I didn’t know the conditions are AND-ed by default, but indeed, it make sense, tanks for the tip :slight_smile:

Initially my goal were to use the ‘condition: zone’ with a group of person:

        - condition: zone
          entity_id: group.family
          zone: zone.zone.home

but homeassistant doesn’t like it, that’s why I used a template.