Help with automation script

Hello,

I have a simple automation that turns on my lights when my phone connects to the network. The automation works when I remove the condition, but once I have add the condition the automation never triggers. I assume I am making a simple mistake but I can’t determine what it is. Any help is appreciated.

- id: home-greeting
  alias: Empty home arrival
  trigger:
  - entity_id: device_tracker.phone
    platform: state
    to: home
  condition:
  - condition: state
    entity_id: device_tracker.phone
    state: not_home
    for:
      hours: 0
      minutes: 20
      seconds: 0
  action:
  - entity_id: group.all_lights
    service: light.turn_on

Thanks,
Drew

This is the correct format:

  - condition: state
    entity_id: device_tracker.phone
    state: not_home
    for:
      minutes: 20

However, as your trigger occurs when the device is home this condition will never be true.

Thanks Tom! That makes a lot of sense. The purpose of the condition was to avoid having the lights turn on when my phone goes asleep or otherwise drops off the network for small periods of time (this had been causing the lights to come on late at night for instance). How I wanted the automation to work was something like:

If phone is home and phone has been away from home for 20 minutes, turn on the lights.

I have added a condition to avoid triggering the automation late at night, but if I wanted to implement what I was talking about above, do you have any thoughts on how to do it?

Thanks,
Drew

Easy-peasy.

One automation is triggered by phone not_home for 20 minutes. When triggered it turns on input_boolean.indicator (serves to indicate the phone has been away from home or more than 20 minutes).

Another automation is triggered when phone is home. Its condition checks if the state of input_boolean.indicator is on. If it is then its action turns on the lights and sets input_boolean.indicator to off.

Done and done.

Thanks 123! I appreciate the patience, I haven’t used Home Assistant very much so far.

Drew

No problem, let me know if you need help writing the two automations. If you follow my description closely, they almost write themselves. :slight_smile:

Another thing to check out is the consider_home parameter.

consider_home (default = 180) Seconds to wait till marking someone as not home after not being seen. This parameter is most useful for households with Apple iOS devices that go into sleep mode while still at home to conserve battery life. iPhones will occasionally drop off the network and then re-appear. consider_home helps prevent false alarms in presence detection when using IP scanners such as Nmap. consider_home accepts various time representations, (e.g., the following all represents 3 minutes: 180 , 0:03 , 0:03:00 )

Thanks for your help guys! This community is awesome.

I think I’ll try out the consider_home method, but if it doesn’t work then I’ll try changing my automation script to the following based on 123’s advice:

- id: trigger_phone_not_home
  alias: Used for tracking how long phone is gone
  trigger:
  - entity_id: device_tracker.iphone
    platform: state
    to: not_home
  action:
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.phone_away
- id: home-greeting
  alias: Empty home arrival
  trigger:
  - entity_id: device_tracker.iphone
    platform: state
    to: home
  condition:
    condition: state
    entity_id: input_boolean.phone_away
    for:
      minutes: 20
  action:
  - entity_id: group.all_lights
    service: light.turn_on
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.phone_away