How to automate: detects motion when I'm not home?

Hi, I’m trying to make a simple automation that detects motion when I’m not home.
I want to take this:

description: notify when detects motion when not home
trigger:
  - type: motion
    platform: device
    device_id: 858d37003d922a280f438557d94a8f55
    entity_id: binary_sensor.pir1_occupancy
    domain: binary_sensor
condition:
  - condition: zone
    entity_id: person.gal
    zone: zone.home
action:
  - service: notify.persistent_notification
    data:
      message: Motion detected
      title: Motion detected
mode: single

and change the condition from being home to being not at home.
I’ve seen condition like
{{states('person.gal') != 'home'}}
but I’ve no idea where to plant it in the yaml.

Thanks

description: notify when detects motion when not home
trigger:
  - type: motion
    platform: device
    device_id: 858d37003d922a280f438557d94a8f55
    entity_id: binary_sensor.pir1_occupancy
    domain: binary_sensor
condition:
  - condition: state
    entity_id: person.gal
    state: 'not_home' 
action:
  - service: notify.persistent_notification
    data:
      message: Motion detected
      title: Motion detected
mode: single

This assumes that OP didn’t configure any zones, otherwise the state of the person entity in his absence can be the name of a zone and not necessarily not_home.

This solves it:

condition:
  - condition: template
    value_template: "{{ states('person.gal') != 'home' }}"

Or shorthand

condition:
  - "{{ states('person.gal') != 'home' }}"
1 Like

Here’s what I use:

- alias: motion-Living room
  trigger:
    platform: state
    entity_id: binary_sensor.living_room_motion_motion
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: "{{ states('group.family') != 'home' }}"
      - condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.day_night
            state: 'off'
          - condition: state
            entity_id: switch.living_room_fan_light
            state: 'off'
  action:
    - service: notify.mobile_app_my_phone
      data:
        message: 'motion: Living Room'

It only responds to motion if no one in my family is home
or if it’s night-time and the room light is not on.
(I sometimes get up in the middle of the night to work on something and I don’t want HA telling me that I’m moving around all the time.)
The day_night input boolean is set and cleared in another automation. Right now it’s entirely time based, but one could just as easily make it based on sun position.
I use sun position to trigger porch and patio lights.

Thanks, exactly what I was looking for.

Nice, I would want to do something similar in the future. Thanks. Is there a guide to learn the syntax for HA automations?

To some extent, there is the documentation. Follow the links on the right after you visit this link for more detail. As you’ve done, feel free to also ask questions. That’s what the community is here for. I did both to arrive at what you saw.

And for completeness there is also a “NOT” condition built-in if you don’t want to use a template.

docs are here: https://www.home-assistant.io/docs/scripts/conditions/#not-condition

condition:
  condition: not
  conditions:
    - condition: zone
      entity_id: person.gal
      zone: zone.home
2 Likes

Thanks for all your comments. I found that reading on templating and jinja helped me understand snippets I read, but it feels like there are multiple layers (yaml, jinja, python?, HA specifics) and it’ll probably take some time to get how it all fit together.