Motion Automation

Hi all, quite new to HA as I’ve formally been doing most things with Google Home Automation. HA is a breath of fresh air!

I have a lot of automations now working very will in HA. However, I have a very simple automation that turns a light on if a camera detects motion and the sun has not risen.

It should check to see if the light is on first and if so, not run the automation.
However, for reasons I cannot fathom this doesn’t seem to work, even though when I test the condition it detects the light being on fine. I’m sure this must be something daft I’m doing in the code but any pointers would be much appreciated! A similar script in Google with the same devices worked a treat!

YAML code below:

alias: Motion - Turn on Hall Lights
description: ""
triggers:
  - trigger: state
    entity_id: binary_sensor.hall_motion_alarm
    from: "off"
    to: "on"
conditions:
  - condition: sun
    before: sunrise
    after: sunset
    enabled: true
  - condition: light.is_off
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
    options:
      behavior: any
actions:
  - alias: Turn on the light
    action: light.turn_on
    data: {}
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
  - alias: Wait until there is no motion from device
    wait_for_trigger:
      trigger: state
      entity_id: binary_sensor.hall_motion_alarm
      from: "on"
      to: "off"
  - alias: Wait the number of seconds that has been set
    delay: 120
  - alias: Turn off the light
    action: light.turn_off
    data: {}
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
mode: restart
max_exceeded: silent

Thanks in advance.

Have you looked at the ‘traces’ on the automation (edit automation > top right > traces) ?

this should show you the flow of the automation events and where/what each decision that was made.

you might also try disabling the conditions, make sure the basic on/off works, then enable 1 condition at a time to see narrow down what is not working.

Obviously though, if you are testing during the day, then the light won’t come on.

I would add a block of “AND” to the second condition, then both have to be true for it to work.

description: ""
triggers:
  - trigger: state
    entity_id: binary_sensor.hall_motion_alarm
    from: "off"
    to: "on"
conditions:
  - condition: sun
    before: sunrise
    after: sunset
    enabled: true
  - condition: and
    conditions:
      - condition: light.is_off
        target:
          device_id: 2d3a0ada37368de5b883ee2613cd54f8
        options:
          behavior: any
actions:
  - alias: Turn on the light
    action: light.turn_on
    data: {}
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
  - alias: Wait until there is no motion from device
    wait_for_trigger:
      trigger: state
      entity_id: binary_sensor.hall_motion_alarm
      from: "on"
      to: "off"
  - alias: Wait the number of seconds that has been set
    delay: 120
  - alias: Turn off the light
    action: light.turn_off
    data: {}
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
mode: restart
max_exceeded: silent

Worth a try?

conditions are AND by default

Cool, still learning… Thank you

I really, really wonder why the developers seem to be pushing to use device IDs in the UI, even in the brand new purpose-specific triggers and conditions feature.

conditions:
  - condition: sun
    before: sunrise
    after: sunset
    enabled: true
  - condition: light.is_off
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
    options:
      behavior: any
actions:
  - alias: Turn on the light
    action: light.turn_on
    data: {}
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8

Just be aware that using device IDs can cause problems later as they can change and then your automations won’t work any more. If you swap out a device or even, say, re-include the same device, anything that references that will break.

I feel the abstraction you want is to turn on a specific light, not turn on whatever device is controlling the light.

    - action: light.turn_on
      target:
        entity_id:
          - light.my_bedside_light

Everyone has slightly different approaches, but some general ideas to consider:

- alias: Wait until there is no motion from device
    wait_for_trigger:
      trigger: state
      entity_id: binary_sensor.hall_motion_alarm
      from: "on"
      to: "off"
  - alias: Wait the number of seconds that has been set
    delay: 120
  - alias: Turn off the light
    action: light.turn_off
    data: {}
    target:
      device_id: 2d3a0ada37368de5b883ee2613cd54f8
mode: restart

That’s probably ok, but I tend to think about events – meaning consider using triggers instead of waiting for things to be a specific state. That is, trigger on motion and also trigger on lack of motion for 2 minutes.

And to avoid having a zillion automations take a look at trigger IDs where you can essentially combine multiple automations into a single one.

triggers:
  - trigger: state
    entity_id: binary_sensor.hall_motion
    to: "on"
    id: motion_detected
  - trigger: state
    entity_id: binary_sensor.hall_motion
    to: "off"
    for: "00:02:00"
conditions: []
actions:
  - if:
      - condition: trigger
        id: motion_detected
    then:
      - action: light.turn_on
        metadata: {}
        target:
          entity_id: light.hallway_lights
    else:
      - action: light.turn_off
        metadata: {}
        target:
          entity_id: light.hallway_lights

Or another more concise way:

triggers:
  - trigger: state
    entity_id: binary_sensor.hall_motion
    to: "on"
  - trigger: state
    entity_id: binary_sensor.hall_motion
    to: "off"
    for: "00:02:00"
conditions: []
actions:
  - action: light.turn_{{ trigger.to_state.state }}
    metadata: {}
    target:
      entity_id: light.hallway_lights

That doesn’t handle the “light is already on”, though.

I used that approach of testing if the light is already on in a room, and people would walk in and the light would turn on. Then they would, out of habit, turn the light switch off on the way out, and then the motion would immediately turn it back on. Or also they would turn the light switch on before the motion triggered and then it would stay on. What’s the right behavior?

(For one room I use an input_boolean to allow overriding the motion detection.)

1 Like

Why?
turning it on again shouldn’t bother, should it? Just drop that part.

@busman

Because they are in love with it, even though it is crap.
There is Sooo much effort on something someone thinks is ‘so much better’ and it just messes people up constantly.

1 Like

Mainly because I have other automation that turns the lights on there at specific times, ie, during the evening etc. If the automation doesn’t check, then it’ll detect any motion and then turn off the lights after the defined time. This automation is for the middle of the night etc when the lights are typically off.

Thanks for this. The multiple triggers is good. I wasn’t aware of that option so I’ll make good use of that!