Trigger for Automation when nobody is home

Hi folks,

I am trying to wrap my head around this and I am stuck :slight_smile:

What I want to achieve:
When nobody is home for 10 mins several actions should fire. I know how to to that for individuals but I don’t know how to program a smart trigger that only fires when all members of the family have left home.

The logic I have in mind is this:

If all members of group “Family” have left the house for 10 minutes and have left the “nearby zone” then:

  • activate this
  • do that

How do I do that?

trigger:
  - platform: state
    entity_id: zone.nearby_zone
    to: 0
    for:
      minutes: 10
1 Like

Thanks @finity,

I used your input to write following:

I am checking after couple of minutes if we left the nearby and home zone before the activation of camera surveillance is triggered.

alias: Camera - activate when nobody is home
description: Arm the camera
trigger:
  - platform: state
    entity_id: zone.home
    to: "0"
condition: []
action:
  - delay:
      hours: 0
      minutes: 12
      seconds: 0
      milliseconds: 0
  - condition: state
    entity_id: zone.home
    state: "0"
    for:
      hours: 0
      minutes: 10
      seconds: 0
  - condition: state
    entity_id: zone.in_der_nahe
    state: "0"
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - service: alarm_control_panel.alarm_arm_away
    data: {}
    target:
      entity_id: alarm_control_panel.blink_home
  - service: notify.mobile_app_iphone
    data:
      message: "Main entrance camera active since nobody is at home"
      title: "Camera active"
mode: single
1 Like

Depending on what you consider nearby this could mean the alarm does not go on when you leave.
If nearby zone could be a stop at the local store then you could be nearby and thus the alarm does not activate as you leave.
I understand that you don’t want the alarm to turn on when it’s unnecessary but these conditions you add here could mean the alarm does not turn on at all when it should.

yes, that is my situation here (shop nearby), however I wanted to prevent the alarm to go on/off to often since its battery driven.
Do you have any other suggestion to narrow the risk (like you also described it well) down?

You need to think about this if this fits your setup.
I assumed your home zone is inside the nearby zone and when you leave nearby zone going home you enter home zone without coming to an area not defined in home or nearby zone.

This will look at the for just as finity suggested in the first place and then if you are in nearby zone wait up to 20 minutes to see if you leave nearby zone (this could be increased if you need).
If you leave nearby zone it makes sure you did not go back home.
If you left home zone and nearby zone in less than 10 minutes you get to the else part, that is activate alarm.

alias: Camera - activate when nobody is home
description: Arm the camera
trigger:
  - platform: state
    entity_id: zone.home
    to: "0"
    for:
      minutes: 10
condition: []
action:
  - if:
      - condition: numeric_state
        entity_id: zone.in_der_nahe
        above: 0
    then:
      - wait_for_trigger:
          - platform: state
            entity_id:
              - zone.in_der_nahe
            to: "0"
        continue_on_timeout: false
        timeout:
          minutes: 20
      - condition: state
        entity_id: zone.home
        state: "0"
      - service: alarm_control_panel.alarm_arm_away
        data: {}
        target:
          entity_id: alarm_control_panel.blink_home
      - service: notify.mobile_app_iphone
        data:
          message: Main entrance camera active since nobody is at home
          title: Camera active
    else:
      - service: alarm_control_panel.alarm_arm_away
        data: {}
        target:
          entity_id: alarm_control_panel.blink_home
      - service: notify.mobile_app_iphone
        data:
          message: Main entrance camera active since nobody is at home
          title: Camera active
mode: single
1 Like

Holy s* :slight_smile:

Thanks much. Will test this and provide feedback. I’ll get the logic and this should work. My home zone is also inside nearby zone.

I don’t have a nearby zone. But one thing I didn’t think about, when you are home, are you also nearby?
What does the nearby also 1 since you are within it or are you only considered home?

When I am home I am inside the nearby zone (physically). However, if I check the state in HA, then I am only home (state = 1), but not nearby (state = 0). Does this require any code/logic change?

No. This is what I thought

1 Like

Thanks for the automation! When I drive straight out, the script does time out however without triggering anything. I am trying to debug, if I find the solution will post it here

That is probably because you want the home zone to be 0 for 10 minutes.

But show us the trace and we can find what the issue is

I don’t know, I just copy/paste the automation.

I called ChatGPT to the resue, it didn’t fix the script but I did manage to get it to write a new automation that after small modifications seems to work (but I haven’t tried all possible options yet and I think that using “choose between actions” would make the automation cleaner but I still lack some knowledge to get all the options to work). For anyone interested this was my question to ChatGPT :
‘Write home assistant automation to switch on switch outletcameralivingroom and notify mobile mobile_app_moto_g5 when zone_home changes to “0” for 5 minutes then wait for 20 minutes, if zone_home is occupied abort script and switch off switch outletcameralivingroom again and notify mobile mobile_app_moto_g5 that someone is home otherwise continue, test if both zone_nearby is “0” and zone_home is “0” notify mobile mobile_app_moto_g5 and run script home_away’

alias: Home away - Activate camera and notify nobody is home
description: Home away - Activate camera and notify nobody is home
triggers:

  • entity_id: zone.home
    to: “0”
    for:
    minutes: 5
    trigger: state
    conditions:
    actions:
  • target:
    entity_id:
    - switch.outletcamerakitchen
    - switch.outletcameraliving
    - switch.outletcameraoffice
    action: switch.turn_on
    data: {}
  • action: notify.mobile_app_moto_g82_5g
    data:
    message: No one is at home for 5 minutes, activating the camera.
    title: Camera Active
  • wait_for_trigger:
    • entity_id: zone.home
      to: “1”
      trigger: state
      timeout:
      minutes: 20
      continue_on_timeout: true
  • choose:
    • conditions:
      • condition: state
        entity_id: zone.home
        state: “1”
        sequence:
      • target:
        entity_id:
        - switch.outletcamerakitchen
        - switch.outletcameraliving
        - switch.outletcameraoffice
        action: switch.turn_off
        data: {}
      • data:
        message: Someone is home, camera deactivated.
        title: Camera Deactivated
        action: notify.mobile_app_moto_g82_5g
      • action: script.home_occupied
        data: {}
        enabled: false
  • condition: and
    conditions:
    • condition: state
      entity_id: zone.home
      state: “0”
    • condition: state
      entity_id: zone.nearby
      state: “0”
  • data:
    message: >-
    No one is at home, home and nearby are ‘0’. Running home_away script.
    title: All zones unoccupied
    action: notify.mobile_app_moto_g82_5g
  • action: script.home_away
    data: {}
    mode: single

I don’t understand why you want to trigger on home zone when it’s the nearby zone that actually matters.

In my opinion you should just trigger on nearby = 0 then do all the stuff.

When I leave home I want my cameras to switch on like immediately. When I also leave nearby, automations switch off lights, televisions, amplifiers, etc etc (it’s a big house) which I do not want switched off when still in nearby and perhaps returning to home soon

Then that is two automations, or two triggers and two separate branches in the automation