What am I doing wrong here.... person detection home and away

For now, I’m stumped. HA will change the state of the wife and I, when we are home or away -
HA-away1
I’m trying to get a couple of automations run upon home or away detection. -

trigger:
  - platform: state
    entity_id: person.chris
    id: chris
    from: home
    to: away
  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
  action:
  - service: switch.turn_off
    target:
      device_id: 645a96c59cd945bd9d8f20df1be6f8df
  mode: single

However, it did not run.
The code for the person, as defined in HA -


Can anyone show me the way?

I’m using an odroid running latest HA os.
Thanks!

You should use the actual state from the Developer Tools which is not_home. away is just a frontend translation.

Additional information can be found in Person integration.

Once again ardy saved my bacon. I was just looking at that, the not_home versus away, come back and you replied! Teach me to look a little harder at dev tools. ^^
This was done thru the automation ui, is it important that the home and not_home be in quotes in the VSC yaml version, as I read somewhere before?

I’ll watch and see if that works tomorrow. Thanks!

Quotes are used to force a string. For example, you have 10 and you want it to be passed as string, then you write it as '10'.

OK, this did not work. Not sure what I’m doing wrong, still. State changed correctly, however the automation did not fire.


alias: Porch light warren gets home
  description: Porch light on us arrival
  trigger:
  - platform: state
    entity_id: person.sandman
    from: not_home
    to: home
  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
  - condition: sun
    before: sunrise
  action:
  - type: turn_on
    device_id: 645a96c59cd945bd9d8f20df1be6f8df
    entity_id: switch.front_porch_light
    domain: switch
  mode: single

The switch is a Zwave switch, fully controllable by HA correctly.

If I have another automation working with the switch, does that interfere?
This is also running, it works correctly on door opening, obviously too late to be useful…

alias: Porch light on at door opening-sun condition
  description: Porch light on at door opening-sun condition
  trigger:
  - type: opened
    platform: device
    device_id: 7b9635a7847f5b99d24b0c381549d2b5
    entity_id: binary_sensor.front_door_sensor_ias_zone
    domain: binary_sensor
  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
  action:
  - type: turn_on
    device_id: 645a96c59cd945bd9d8f20df1be6f8df
    entity_id: switch.front_porch_light
    domain: switch
  mode: single

These two are currently running together, I’m posting this to see if its an interference thing.

On top of that, if I set my state in developers tools to not_home, then refresh, wait maybe 30 seconds, then change state back to home, then refresh again, shouldn’t that fire off the automation?
Thanks!

The first automation’s condition indentation is incorrect.

  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
  - condition: sun
    before: sunrise

…should be-

  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise

OR just-

condition: not
conditions:
  - condition: sun
    after: sunrise
    before: sunset

You also control the sun condition with a template that will operate it with degrees of elevation. Gives you you ability to have the light come on before sunset, when it may be starting to get dark.\

      - condition: template
        value_template: '{{ state_attr("sun.sun", "elevation") < 8 }}'

You end up with something like this.

# ARRIVING HOME - TURN ON ITEMS IF SUNSET #
- alias: Arriving Home - Turn Items On If Sunset
  initial_state: true
  trigger:
    platform: state
    entity_id: group.family
    from: "not_home"
    to: "home"
    for:
      seconds: 10
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ state_attr("sun.sun", "elevation") < 8 }}'
      - condition: time
        before: "23:00:00"
  action:
    - service: homeassistant.turn_on
      entity_id:
        - script.kitchen_normal
        - script.blue_hall

Good catch, Ardy… didn’t even notice that. No errors in the VSC editor either :frowning:
Think I’ll shorten up the other 2 automations to that code as well. Less clutter the better :slight_smile:

Based in that, just quick-checked other conditions in the autom yaml file. The rest are on par.

Kanga, I’m working up to that level, going to take a while until I get a better hang of this. If nothing else, I’m determined to learn a lot more and branch out.
Thanks for both of your inputs!

Here is a working config we’ve used, I’m sure it isn’t 100% perfect because it doesn’t cover all states, triggers and conditions.

However the light always comes on when one of us gets home in the dark and I’ve never seen it turn on during day. :grinning:

Main difference I can see is I don’t set the from state for person and the sun entity is checking for below horizon instead of sunset / sunrise (the sunset/rise logic is confusing to me and never works as expected) below horizon just worked and I’ve stuck with that.

alias: Arriving Home (Night Time)
description: ''
trigger:
  - platform: state
    entity_id: person.x
    to: home
  - platform: state
    entity_id: person.y
    to: home
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - type: turn_on
    device_id: xyz
    entity_id: light.hallway_ceiling
    domain: light
    brightness_pct: 100
  - wait_for_trigger:
      - type: not_occupied
        platform: device
        device_id: abc
        entity_id: binary_sensor.hallway_sensor_occupancy
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 30
    continue_on_timeout: false
  - type: turn_off
    device_id: xyz
    entity_id: light.hallway_ceiling
    domain: light
mode: single

Extra bit of config is the motion sensor that waits until motion stops in the hallway and then turns the light off after 30 seconds.

Hope this is useful, thanks Stuart.

As noted above, I’d avoid the from part in the trigger. This is only useful, if you really want to check specifically for that state change. Normally it’d be better to just use the state you want to react on, in your case to: home. If the sensor has any kind of problem and reports something else than not_home (eg unknown or unavailable) your trigger will not fire. And for this use case you only want to check, if someone is getting home. :wink:

Next thing, I wouldn’t work with device_ids. They complicate things more than necessary, at least at the moment. If you ever change a device, you’d need to change all device_ids, in all scripts, automations and so on, whereas you’d only need to change the entity_id for a new device. There are ways to do this automatically, but for now they are not really needed and complicate things. :wink:

To debug your current setting, use the automation history (I don’t know the correct name for it, sorry) and see, where things get sideways. You can find it here:

After running the automation it should show exactly what has been executed, and if not, why. :slight_smile:

Btw. I’m not sure that changing the state via developer tools actually triggers an automation. I’d try to really change the state (eg. if you have your presence detection via a phone, turn it off and on).

Good info here. Thanks guys.

Paddy, can I just delete the device id line, and leave entity and everything else alone? or do I need to substitute device id for something?

You can safely delete device_id. :slight_smile:

Tbh I’d write it another way, I find that more readable, but that’s me. :slight_smile: Instead of

action:
  - type: turn_on
    device_id: 645a96c59cd945bd9d8f20df1be6f8df
    entity_id: switch.front_porch_light
    domain: switch

I’d choose this

action:
  - service: switch.turn_on
    entity_id: switch.front_porch_light
1 Like

Will do. I didn’t like that device id thing either, it already happened : switching out a like device for another. Then I had to go in and tweak some automations.
I had a multipurpose sensor going crazy, main body mounted on doorframe, magnet on door, and all day the accelerometer was going off to on to off several times an hour in some cases. Replaced. :slight_smile: Nothing was rattling the doorframe, or door, no obvious movements. Metal door, wood frame, shut tightly, no wiggle in the deadbolt hole (think I checked everything it could be anyway). Oh well.

1 Like

Wanted to follow up, so far this code is working pretty good. Haven’t totally cleaned it up yet, that’s dependent on time :slight_smile:

  alias: Porch light we get home-after sunset
  description: Porch light on us arrival-after sunset
  trigger:
  - platform: state
    entity_id: person.sandman
    to: home
  - platform: state
    entity_id: person.chris
    to: home
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - type: turn_on
    device_id: 645a96c59cd945bd9d8f20df1be6f8df
    entity_id: switch.front_porch_light
    domain: switch
  mode: single

This works when as soon as we pull in the driveway, during sun below horizon time, the porch light will kick on. Done mostly for the boss, sometimes a good source of motivation to get those automations done :slight_smile:

1 Like