Automation when leaving home and coming back

Hey guys and gals,

I want to run some automation when all persons left our home. At the moment I am a little confused because I found conflicting information (old vs. new guides) online. Maybe someone may enlighten me.
I’ve used the GUI for this and the going away part (“Niemand zu hause = nobody at home”) works fine…I think. At least it triggers.

alias: Niemand zu Hause
description: ""
trigger:
  - platform: state
    entity_id:
      - person.xx
      - person.yy
    to: not_home
condition:
  - condition: state
    entity_id: person.xx
    state: not_home
  - condition: state
    entity_id: person.yy
    state: not_home
action:
...
mode: single

But the code for coming home (“Jemand kommt nach Hause”) does not trigger / start. Any ideas for that?

alias: Jemand kommt nach Hause
description: ""
trigger:
  - platform: state
    entity_id:
      - person.xx
      - person.yy
    to: home
    for:
      hours: 0
      minutes: 0
      seconds: 0
    from: not_home
condition:
  - condition: state
    entity_id: person.xx
    state: not_home
  - condition: state
    entity_id: person.yy
    state: not_home
action:
...
mode: single

Additionally I want to ad conditions (i.e. sunrise/sunset) for single actions (turning lights on should only be done when the sun is down) for single actions.
I assume I have to use this code, but do not know how or were to paste it or if it is even possible to add it to actions.

service: light.turn_on
condition: sun
before: sunrise
after: sunset
data: {}
target:
  entity_id:
    - light.lampe

Using the code above (for conditional action light on) I get this error-message:

Message malformed: extra keys not allowed @ data[‘action’][0][‘service’]

I hope someone will be able to guide me.

Thanks,
Max

You can use the state of zone.home these days. All away:

trigger:
- platform: numeric_state
  entity_id: zone.home
  below: 1

Somebody home:

trigger:
- platform: numeric_state
  entity_id: zone.home
  above: 0

No need for the condition checks on the person entities if you do that.

This looks like you tried to smash together a sun condition and turning on a light:

service: light.turn_on
condition: sun
before: sunrise
after: sunset
data: {}
target:
  entity_id:
    - light.lampe

They’re two separate things and have to stay that way, for example:

- if:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  then:
  - service: light.turn_on
    target:
      entity_id: light.lampe

Thank you for the quick response.
The zone.home was exactly what I was looking for…I just did not understand that it is a numerical value and not a state: they do not work (I’ve just copy-pasted your code).

In regard to the if/then conditions:

- if:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  then:
  - service: light.turn_on
    target:
      entity_id: light.lampe

I still get this message when trying to save the automation:

Message malformed: expected dictionary @ data[‘action’][0]

Maybe I got some formatting or something else wrong?

Go to Developer tools > States and search for zone.home. What do you see in the column State?

Try to share the full automation code here (please remove any private info) so it will be easier to help. Not rarely this is just some blank space missing somewhere…

Thanks for the Input. I really do not know what a column State is :smiley:

Thats the attributes I find within Developer tools. But the triggering will work properly I assume.

latitude: xxxxxxx
longitude: yyyyyyy
radius: 100
passive: false
persons: []
editable: true
icon: mdi:home
friendly_name: zu Hause

Current State is “0” because no one is home. This is the automation code I want to test right now.

alias: Jemand kommt nach Hause
description: ""
trigger:
  - platform: numeric_state
    entity_id: zone.home
    above: 0
condition: []
action:
  - if:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
  then:
    - service: light.turn_on
      target:
        entity_id: light.lampe
mode: single

I get the following error message when trying to save:

Message malformed: required key not provided @ data[‘action’][1][‘then’]

I assume there is some syntax error.

I feel not good enough to debug.
But maybe my automation helps?
It switches on a light when I’m at home or come home and the sun is below horizon for 30 minutes.

alias: Außenlicht bei Sonnenuntergang
description: >-
  Lichterkette auf der Terrasse 30 min nach Sonnenuntergang einschalten und um
  23:30 Uhr ausschalten
trigger:
  - platform: sun
    event: sunset
    offset: "00:30:00"
    id: sun
  - platform: state
    entity_id: device_tracker.iphone
    from: not_home
    to: home
    id: home
  - platform: time
    at: "23:30"
    id: time
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sun
        sequence:
          - condition: state
            entity_id: device_tracker.iphone
            state: home
          - service: switch.turn_on
            target:
              device_id: <id>
            data: {}
      - conditions:
          - condition: trigger
            id: home
        sequence:
          - condition: sun
            after: sunset
            after_offset: "00:30:00"
          - condition: time
            before: "23:30"
          - service: switch.turn_on
            target:
              device_id: <id>
            data: {}
      - conditions:
          - condition: trigger
            id: time
        sequence:
          - service: switch.turn_off
            target:
              device_id: <id>
            data: {}
    default: []
mode: single

Your indenting is wrong. This is what I wrote:

- if:
...
  then:

This is what you wrote:

  - if:
...
  then:

Make sure that then is in line with if

That did the trick! Ad least it removed the error message. Will test it tonight! Thanks for the great help!

What you posted there still isn’t indented correctly

  - if:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
  then:
  ^^ should be another two spaces before this

I think that was some copy-paste-error on my side. In HomeAssistant it looks fine.

alias: Jemand kommt nach Hause_Test
description: ""
trigger:
  - platform: numeric_state
    entity_id: zone.home
    above: 0
condition: []
action:
  - if:
      - condition: state
        entity_id: sun.sun
        state: below_horizon
    then:
      - service: light.turn_on
        target:
          entity_id: light.lampe
        data: {}
mode: single

1 Like

Is that working as expected?

In theory it works quite nice (at least when setting the values in Dev Tools).

Now we have to see how it works in reality :smiley:

1 Like

I wonder, if it is possible to trigger an interaction on the (iPhone) app?
(Asked that here, but till now no response…)

Is there a way to do this ONLY when coming home from out of the house? Say if my phone never left for three days, I don’t want it to activate, but if I leave for 15 minutes and come back, it should activate?

I did a blueprint that does these things, perhaps you can get some inspiration from that.