Hey everyone, and thanks for this great community that has helped me a lot so far.
Just to start by saying that I am very new to the whole home assistant and i am using hassio on an RPi.
I have succesfully managed to automate the night light wich will turn on (only at night time) when the PIR sensor is triggered, and will turn off 5 minutes after there is no movement for X minutes. I used this code here and added the time condition on my own.
As the light is positioned in the living room and i usually turn it on from my front end manually, i would like both automations or at least the second one (which turns the light off if no movement) to be disabled whenever the specific switch has been previously switched on by me, pressing the button. (IE. if i am sitting in the living room watching tv, having the light on manually, i dont want it to go off because the PIR sensor caught no movement)
I hope i am making sense
Any help or reading material would be greatly appreciated.
I have also found a relevant question here, but it does not answer my main question.
Here is my existing Automation Code: (ignore the alias naming, its Greek :))
- action:
- alias: Turn On Saloni 4
entity_id: switch.Saloni_4
service: switch.turn_on
alias: Motion - LR upstairs - ON
condition:
condition: time
after: '21:00:00'
before: '5:00:00'
id: '1506607652748'
trigger:
- entity_id: binary_sensor.pir_living_room
from: 'off'
platform: state
to: 'on'
- action:
- alias: Turn OFF Saloni 4
entity_id: switch.Saloni_4
service: switch.turn_off
alias: Motion - LR upstairs - OFF 1m
condition: []
trigger:
- entity_id: binary_sensor.pir_living_room
platform: state
to: 'off'
for:
minutes: 1
With my little coding knowledge, the only thing i can think of is to add an entry to some database whenever the switch was being turned on, like this:
light_on:0 (when the light is off)
light_on:1 (when it was turned on by the automation)
light_on:2 (when it was turned on by the manual frontend switch)
Then ask for the OFF automation to check for the condition light_on:1 before firing and if it fires, reset it to 0
(the “2” option is probably useless and would cause more problems as it would need to be reset back to 0 when turned off by the frontend switch, but ill just leave it there )
Is such a solution possible in Home Assistant? or am i looking at this completely wrong?
An other way could be to replace the original “manual” switch i use to turn the light on and off to a toggle that would also turn the light on and off but would also deactivate the automation when turning the switch to on and reactivating it when turning the switch back off. IDK if this is possible somehow though.
light_manually_on:
name: Check if lights were on manually
initial: off
automations.yaml:
- action:
- alias: Turn On Saloni 4
service: script.turn_on
entity_id: script.night_light_on
alias: Motion - LR upstairs - ON
condition:
condition: time
after: '19:00:00'
before: '5:00:00'
id: '1506607652748'
trigger:
- entity_id: binary_sensor.pir_living_room
from: 'off'
platform: state
to: 'on'
- action:
- alias: Turn OFF Saloni 4
service: script.turn_on
entity_id: script.night_light_off
alias: Motion - LR upstairs - OFF 1m
condition: []
trigger:
- entity_id: binary_sensor.pir_living_room
platform: state
to: 'off'
for:
seconds: 5
scripts.yaml:
################### CHECK IF LIGHTS WERE ON MANUALLY AND DONT TURN THEM OFF AFTER NO MOTION DETECTED
######## ON
night_light_on:
sequence:
- service: script.turn_on
entity_id: script.reset_boolean
- service: script.turn_on
entity_id: script.check_light_status
- service: script.turn_on
entity_id: script.turn_light_on_if_was_manually_off
reset_boolean:
sequence:
- service: input_boolean.turn_off
entity_id: input_boolean.light_manually_on
check_light_status:
sequence:
- condition: state
entity_id: switch.Saloni_4
state: 'on'
- service: input_boolean.turn_on
entity_id: input_boolean.light_manually_on
turn_light_on_if_was_manually_off:
sequence:
- condition: state
entity_id: switch.Saloni_4
state: 'off'
- service: switch.turn_on
entity_id: switch.Saloni_4
############# OFF
night_light_off:
sequence:
- service: script.turn_on
entity_id: script.turn_off_if_it_was_on_by_automation
turn_off_if_it_was_on_by_automation:
sequence:
- condition: state
entity_id: input_boolean.light_manually_on
state: 'off'
- service: switch.turn_off
entity_id: switch.Saloni_4
- service: script.turn_on
entity_id: script.reset_boolean
(wont paste my sensor and switches yaml just to be as to the point as possible)
Thanks for all the input guys!!
BTW, for: seconds: 5 in the automation part, is not templatable, (tried to have it get a value from a slider and that cost me some time)
Lastly, although i used your input to make it i will vote this answer as solution for future refernce, as it shows all parts of the code.
Thanks again!
The above code turned out not to work very well, as it would only check if the light was generally on at the start of the automation. This way, the second time motion was detected and the light was already on by motion, the light would stay on as if it was turned on manually.
I have ended up to this simpler code which works great so far, replacing the manual switch with a template switch which besides controlling the light it also activates a boolean.
Here are is my code:
light_manually_on:
name: Check if lights were on manually
initial: off
icon: mdi:lightbulb-on-outline
light_on_by_automation:
name: Check if lights were on by automation
initial: off
icon: mdi:lightbulb-on-outline
automations.yaml
- action:
- alias: Turn On Saloni 4
service: script.turn_on
entity_id: script.night_light_on
alias: Motion - LR upstairs - ON
condition:
condition: time
after: '18:00:00'
before: '5:00:00'
id: '1506607652748'
trigger:
- entity_id: binary_sensor.pir_living_room
from: 'off'
platform: state
to: 'on'
- action:
- alias: Turn OFF Saloni 4
service: script.turn_on
entity_id: script.night_light_off
alias: Motion - LR upstairs - OFF 1m
condition: []
trigger:
- entity_id: binary_sensor.pir_living_room
platform: state
to: 'off'
for:
seconds: 30
Giving this a another look, trying to get it to work. I’m not seeing where you’re calling the template switch (saloni_4_auto). Unless I’m blind and missing it, lol.
Hey, sorry for not replying earlier.
I am calling the saloni_4_auto in a group i have made, inside the groups.yaml (calling group: !include groups.yaml in configuration.yaml)