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.
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 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
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.