I have two automations to provide motion based light control in my kitchen. What I would like is to be able to manually override the automatic control if the light switch is operated, but only on a per instance basis. ie: if on any particular occasion I walk into the kitchen and the light turns ON automatically, I would like to be able to press the light switch button to have the light stay ON indefinitely rather than still turn OFF as per the kitchen_light_auto_off.yaml automation (when motion stops for 2 minutes) below. Currently if I press the light switch, the light will turn OFF, and I can then turn it back ON by pressing it again, however the auto_off will still occur. Is there a way to change this code so that a manual switch press disables the auto_off for that instance only? My thoughts being that if I force the light to stay ON, I can then manually turn it OFF again later and subsequent motion in the kitchen will turn it ON again. Obvisouly I can turn OFF the kitchen_light_auto_off automation in this case to achieve this but that means using a phone / computer / tablet when all I want is the ease of doing so via the light switch for a simple and quick manual mode.
kitchen_light_auto.yaml
alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor
to: 'on'
condition:
condition: or
conditions:
- condition: sun
after: sunset
# after_offset: '00:00:00'
- condition: sun
before: sunrise
before_offset: '00:30:00'
action:
- service: homeassistant.turn_on
entity_id: light.kitchen_light_level
data:
brightness: 100
kitchen_light_auto_off.yaml
alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor
to: 'off'
for:
minutes: 2
action:
- service: homeassistant.turn_off
entity_id: light.kitchen_light_level
I use input booleans for these things, as in this post. Turn the input_boolean on when the automation turns the light on, but not if you turn it on manually. Then you only turn the light off automatically if the input boolean is off.
Whether you can make the switch not turn the light off, that depends on what your switch is - you forgot to tell us.
Thanks for the hint and link. That’s exactly what I needed.
The light switch is actually a momentary push-button hard-wired into the switch input of a z-wave dimmer so it doesn’t have a HA entity at all. The button will operate the light independent of HA and the dimmer simply relays the light’s status to HA. Since the button isn’t a HA entity I don’t see how I could stop a button press while already ON from turning it OFF rather than just forcing ‘manual mode’, but even if I have to turn it OFF and then back ON it will work with your linked example. Thanks!
Hi @Tinkerer, I was just making the code changes for this an had a thought but am yet to check it in action. Surely this code doesn’t prevent the automation from setting the input_boolean to ‘on’ despite the light already being on (in the case that the light was turned on manually) so it would still allow the ‘off’ automation to function…?
this is what I have at the moment:
kitchen_light_auto.yaml
alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor
to: 'on'
condition:
condition: or
conditions:
- condition: sun
after: sunset
# after_offset: '00:00:00'
- condition: sun
before: sunrise
before_offset: '00:30:00'
action:
- service: homeassistant.turn_on
entity_id: light.kitchen_light_level
data:
brightness: 100
- service: input_boolean.turn_on
data:
entity_id: input_boolean.kitchen_light_motion_activated
kitchen_light_auto_off.yaml
alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor
to: 'off'
for:
minutes: 2
condition:
condition: state
entity_id: input_boolean.kitchen_light_motion_activated
state: 'on'
action:
- service: homeassistant.turn_off
entity_id: light.kitchen_light_level
- service: input_boolean.turn_off
entity_id: input_boolean.kitchen_light_motion_activated
So I’m thinking that I will need to have a condition in the ‘auto_on’ to prevent it running if the light is already ‘on’…?
It seems the above code is wrong in that it fails the config check… I #'ed out the condition: section and it passes but its the first time I’ve tried nested conditions so I’m not sure what I’ve done wrong.
Can someone please show me where I’ve made the mistake?
Well after a bit of playing around trying to dodge the motion sensor while hitting the light switch, I have had mixed results with the below code. At first I thought it was working how I wanted, but then the light would sometimes turn off, other times not turn off when it was supposedly in ‘auto’ mode… can someone please point me in the right direction here?
input_booleans.yaml
kitchen_light_motion_activated:
name: Kitchen light motion activated
initial: off
icon: mdi:run-fast
kitchen_light_auto.yaml
alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor
to: 'on'
condition:
condition: and
conditions:
- condition: state
entity_id: light.kitchen_light_level
state: 'off'
- condition: or
conditions:
- condition: sun
after: sunset
after_offset: '00:00:00'
- condition: sun
before: sunrise
before_offset: '00:30:00'
action:
- service: homeassistant.turn_on
entity_id: light.kitchen_light_level
data:
brightness: 100
- service: input_boolean.turn_on
data:
entity_id: input_boolean.kitchen_light_motion_activated
kitchen_light_auto_off.yaml
alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor
to: 'off'
for:
minutes: 2
condition:
condition: state
entity_id: input_boolean.kitchen_light_motion_activated
state: 'on'
action:
- service: homeassistant.turn_off
entity_id: light.kitchen_light_level
- service: input_boolean.turn_off
entity_id: input_boolean.kitchen_light_motion_activated
My intention is to have the light turn on and off automatically based on motion, however if I want to force the light to stay on I press the light switch off and then back on to put it in ‘manual mode’. My suspicion is that the motion sensor is still putting the light back in ‘auto’ mode when it sees me walking around, even after I have tried to manually override it. As I said, it seems to be hit and miss as to it working or not. Prior to the manual override code, the light worked perfectly in ‘auto’ mode, its just the manual override that I cant seem to get quite right.
The trouble is, you need to turn off the input boolean simply when the light turns off, and you’re only turning it off when they’re automatically turned off.
Thanks for the write-up. I have made the changes but wont be able to test until I get home. From what I can tell from your code though, the motion sensor is still able to turn the input_boolean ON when someone walks past it, despite the light being ON manually. Therefore the OFF automation will still be valid. Surely you need to prevent the input_boolean from switching ON if the light was manually turned ON beforehand, otherwise nothing stops the OFF automation from occurring anyway. I get that you have the condition there in the OFF automation, but if the motion sensor can still cause the input_boolean to switch, then this condition is met.
I think that the ON automation needs a condition to ensure that the light is OFF before it can run, that way eliminating the motion sensor from switching the input_boolean if the light was already ON having been manually switched… but then the re-trigger for motion in that room wont work. This is where I was stumped before.
cool. thats what I had in my original code and have now put it back in. I’ll give it another test. It seems like your example is very much like mine but uses an additional automation. Hopefully this now works. Cheers for the help.
Did the solution posted by @Tinkerer end up working smoothly for you. I’ve tricked my garage light tonight that has a motion sensor but wondering how this works if the motion sensor trips before you get to the physical light switch?
It didn’t work reliably so I ended up changing my control philosophy for motion controlled lights. I now have Xiaomi Wireless Buttons which (via automations) control the respective light to be in a particular mode: ON, OFF, AUTO (motion controlled). When in ON mode, the light is set to full brightness and the motion sensor has no effect, likewise with OFF mode being light off. AUTO mode allows the light to be motion controlled (for both turning on and off) and sets Circadian Lighting for that light to ON so that the brightness and colour temp are set correctly for the time of day