Motion activated lights automation

I think I have this figured out. I found enkama/hass-variables and use it to set a binary sensor that I can test to determine if the light had been turned on by automation. I now have two automations and initial tests look promising. Any feedback appreciated.

alias: Front Porch Sensor Light - on
description: Turn on front porch light if motion detected
triggers:
  - entity_id: binary_sensor.node_2_motion_detection
    to: "on"
    trigger: state
conditions:
  - condition: sun
    before: sunrise
    after: sunset
  - condition: state
    entity_id: light.front_porch_light_switch
    state: "off"
actions:
  - target:
      entity_id: light.front_porch_light_switch
    action: light.turn_on
    data: {}
  - target:
      entity_id: binary_sensor.bsw01
    data:
      value: "true"
    action: variable.update_binary_sensor
mode: single

alias: Front Porch Sensor Light - off
description: >-
  Turn off front porch light if no motion detected and it was turned on by
  automation
triggers:
  - entity_id: binary_sensor.node_2_motion_detection
    to: "off"
    for:
      minutes: 2
    trigger: state
conditions:
  - condition: sun
    before: sunrise
    after: sunset
  - condition: state
    entity_id: light.front_porch_light_switch
    state: "on"
  - condition: state
    entity_id: binary_sensor.bsw01
    state: "on"
actions:
  - target:
      entity_id: light.front_porch_light_switch
    action: light.turn_off
    data: {}
  - target:
      entity_id: binary_sensor.bsw01
    data:
      value: "false"
    action: variable.update_binary_sensor
mode: single

Edit: while re-reading my post to check for typos I found my first update - I’m going to remove the “sun” condition from the “off” automation. It’s not needed and there’s a very small chance the “on” automation will be triggered 1 minute before sunrise and the light would then stay on all day.

This automation is awesome. It works even if you have a smart switch in the room.

Simple and efficient!

You don’t even need the helper used in the condition.

Since you use 2 automations, why not programmatically en-/dis-able the motion automation, when using the switch,

Probably because you will have to make template sensor that will determine did you turn light switch manually on/off. It is probably possible but not that easy to do.

It’s taken me a while to get my head around what you might be suggesting here and once I figured it out I realised that @ddaniel is replying to your suggestion and he’s right.

My whole setup, for this scenario, is one simple smart switch (Zooz ZEN71), one motion sensor (Zooz ZSE70) and one dumb exterior flood light. If I en/disable the automation when the switch is used I can’t, easily, determine if the switch was used physically, manually through the Home Assistant app or automatically through this automation. As far as I can see the same on/off state change happens in each of these situations.

I didn’t take that into consideration…it was not part of the original request.
But ofc, you are right :wink:

Hi All,

New to home assistant.

I can’t get this automation working. I have a shelly switch and the aqara motion sensor. I think its something to do with the YAML as the switch and sensor work independently. Any ideas. Photos attached.

I wrote the script written but when I saved it changed automatically.

Script in at the moment is

triggers:
  - entity_id: binary_sensor.0x54ef441000b6309c_occupancy
    to: "on"
    trigger: state
  - entity_id: binary_sensor.0x54ef441000b6309c_occupancy
    to: "off"
    for:
      minutes: 2
    trigger: state
actions:
  - target:
      entity_id: switch.en_suite_light_switch_0
    action: light.turn_{{ trigger.to_state.state }}
mode: single

Please read this from How to help us help you - or How to ask a good question11 Format it properly

Sorry about that Nick,

Just updated now

Thanks in advance

Did you check the traces of the automation?

Also, I would advice you to give your devices/entities a much more meaningful name then
0x54ef441000b6309c to make things easier to recognize/read/troubleshoot.

In the automation I can see when I move infront of the sensor it triggers.

Its just the action (Then do) that isn’t working

Is the entity_id correct?
Also: put double quotes around it.

entity_id is correct and double quotes didn’t help.

The error i’m getting when trying to run the action is this

“Error rendering service name template: UndefinedError: ‘trigger’ is undefined”

Sorry, the action, not the target!

Sadly not.

This is the code

triggers:
  - entity_id: binary_sensor.0x54ef441000b6309c_occupancy
    to: "on"
    trigger: state
  - entity_id:
      - binary_sensor.0x54ef441000b6309c_occupancy
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 1
    trigger: state
actions:
  - target:
      entity_id: switch.en_suite_light_switch_0
    action: light.turn_{{ "trigger.to_state.state" }}
mode: single

and get the error “Template rendered invalid service: light.turn_trigger.to_state.state”

Sorry if this an easy fix but this is all new to me haha

Changed to one second at the moment for troubleshooting

Shouldn’t that be

switch.turn_{{ "trigger.to_state.state" }}

No joy with that.

with this

target:
  entity_id: switch.en_suite_light_switch_0
action: turn.switch_{{ trigger.to_state.state }}

getting this error when trying to run action “Error rendering service name template: UndefinedError: ‘trigger’ is undefined”

Of course: you have to give both triggers an ID: click on the dots and name them on & off

Yeah this was it.

Thanks for your help