Automation Help, Smart Night Light (ignore off command when it was switched on manually)

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 :slight_smile:
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 :slight_smile: )

Is such a solution possible in Home Assistant? or am i looking at this completely wrong?

Yes, that works fine. I did smth similar.

Just have a input_boolean that you can trigger only by the ON automation.
Make a condition for your other rules to check for the boolean.

I did something like this for the light on my aquarium. I did the following.

Script 1: Set input_boolean to no

Script 2: If light is on, set script 1 input_boolean to yes

Script 3: turn light off if script 1 input_boolean is no

Script 4: Call script1, run script 2, turn on light, then wait for say 5 minutes and run script 3.

So if the light was on before script 4 runs, it will not turn the light off when running the off light script (script 3).

Not sure if there is an easier way to do this, this worked for me.

Could you please paste your script codes so i can have a look?
Although i understand what you are saying, i have no idea how to recreate the steps :slight_smile:

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.

feed_mode:
  sequence:
    - service: script.turn_on
      entity_id: script.set_light_static
    - service: script.turn_on
      entity_id: script.check_light_static
    - service: switch.turn_on
      entity_id: switch.tank_light
    - service: switch.turn_off
      entity_id: switch.tank_pump
    - delay: '00:{{ states.input_slider.feed_mode_time.state | int }}:00'
    - service: script.turn_on
      entity_id: script.turn_off_tank_light
    - service: switch.turn_on
      entity_id: switch.tank_pump
    - service: input_boolean.turn_off
      entity_id: input_boolean.feed_mode
     
set_light_static:
  sequence:
    - service: input_boolean.turn_off
      entity_id: input_boolean.tanklightindictor       
     
check_light_static:
  sequence:
    - condition: state
      entity_id: switch.tank_light 
      state: 'on' 
    - service: input_boolean.turn_on
      entity_id: input_boolean.tanklightindictor    
     
turn_off_tank_light:
  sequence:
    - condition: state
      entity_id: input_boolean.tanklightindictor
      state: 'off'
    - service: switch.turn_off
      entity_id: switch.tank_light

hey @Dinoaus, I have spent some time studying your code (keep in mind its the first HASS script i am working on so im not familiar at all)

I have figured out most of it, what i dont understand is:
Why do you call this at the end?

- service: input_boolean.turn_off
  entity_id: input_boolean.feed_mode

and, to fire this whole thing in need to enter this in my original automation code right?

action:
 - service: script.turn_on
   entity_id: feed_mode

Just to give you a second option. I did it all in the automation. Let me know if there are any questions :slight_smile:

######################################################################################################## DRESSER DAY  ###
- alias: 'Dresser Light Day'
  trigger:
    platform: state
    entity_id: binary_sensor.ankleide_motion
    to: 'on'
####################################################  TURN ON GROUP DRESSER  @ DAY IF MOTION  ###########################
  action:
  - service: light.turn_on
    data:
      entity_id: light.bedroom_dresser
      brightness_pct: 100
      kelvin: 2700
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.dresser
####################################################  CONDITIONS  #######################################################
  condition:
    condition: and
    conditions:
####################################################  Lights are off  ###################################################
    - condition: state
      entity_id: light.bedroom_dresser
      state: 'off'
####################################################  ILLUMINANCE IS BELOW XX  ##########################################
    - condition: numeric_state
      entity_id: sensor.flur_helligkeit
      below: '30'
####################################################  MODE IS DAY  ######################################################     
    - condition: state
      entity_id: input_select.mode
      state: 'Tag'

#####################################################################################################  DRESSER NIGHT  ###

- alias: 'Dresser Light Night'
  trigger:
    platform: state
    entity_id: binary_sensor.ankleide_motion
    to: 'on'
####################################################  TURN ON DRESSER  TOP @ NIGHT IF MOTION  ############################
  action:
  - service: light.turn_on
    data:
      entity_id: 'light.bedroom_dresser'
      brightness_pct: 25
      color_name: 'red'
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.dresser
####################################################  CONDITIONS  #######################################################
  condition:
    condition: and
    conditions:
####################################################  Lights are off  ###################################################
    - condition: state
      entity_id: 'light.bedroom_dresser'
      state: 'off'
####################################################  ILLUMINANCE IS BELOW XX  ##########################################
    - condition: numeric_state
      entity_id: sensor.flur_helligkeit
      below: '30'
####################################################  MODE IS NIGHT  ######################################################     
    - condition: state
      entity_id: input_select.mode
      state: 'Nacht'

      
#############################################################################################  TURN OFF IF NO MOTION  #######
- alias: 'Dresser Light Off'
  trigger:
    platform: state
    entity_id: binary_sensor.ankleide_motion
    to: 'off'
    for:
      seconds: 30
####################################################  TURN ON DRESSER  TOP @ NIGHT IF MOTION  ############################
  action:
  - service: light.turn_off
    data:
      entity_id: 'light.bedroom_dresser'
  - service: input_boolean.turn_off
    data:
      entity_id: input_boolean.dresser
####################################################  CONDITIONS  #######################################################
  condition:
 ################################################  Lights have been off before  ######################################################
    condition: state
    entity_id: 'input_boolean.dresser'
    state: 'on'

Thanks for all your help, i used your advice and ended up with a solution of my own.
Here are the parts of my code:

Configuration.yaml

input_boolean: !include input_boolean.yaml
automation: !include automations.yaml
script: !include scripts.yaml

input_boolean.yaml:

  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:

Configuration.yaml

switch: !include switches.yaml
input_boolean: !include input_boolean.yaml
automation: !include automations.yaml
script: !include scripts.yaml

switches.yaml

  - platform: template
    switches:
      saloni_4_auto:
        value_template: "{{ is_state('switch.saloni_4' , 'on') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.saloni_4_auto_on
        turn_off:
          service: script.turn_on
          entity_id: script.saloni_4_auto_off

input_boolean.yaml

  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

and lastly scripts.yaml:

######## MOTION ON
night_light_on:
  sequence:
    - condition: state
      entity_id: input_boolean.light_manually_on
      state: 'off'
    - service: switch.turn_on
      entity_id: switch.Saloni_4
    - service: input_boolean.turn_on
      entity_id: input_boolean.light_on_by_automation

######## MOTION OFF
night_light_off:
  sequence:
    - condition: state
      entity_id: input_boolean.light_on_by_automation
      state: 'on'
    - service: switch.turn_off
      entity_id: switch.Saloni_4
    - service: input_boolean.turn_off
      entity_id: input_boolean.light_on_by_automation
 
######## SIMPLE ON
saloni_4_auto_on:
  sequence:
    - service: input_boolean.turn_on
      entity_id: input_boolean.light_manually_on
    - service: switch.turn_on
      entity_id: switch.Saloni_4 

######## SIMPLE OFF  
saloni_4_auto_off:
  sequence:
    - service: input_boolean.turn_off
      entity_id: input_boolean.light_manually_on
    - service: switch.turn_off
      entity_id: switch.Saloni_4      
    - service: input_boolean.turn_off
      entity_id: input_boolean.light_on_by_automation

Hope this helps :slight_smile:

Just used your examples, Will test them out after dark.

Unfortunately my automation still turns the lights off when manually activated

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)

part of my groups.yaml is like this:

  saloni_panw:
    entities:
      - switch.saloni_1
      - switch.saloni_2
      - switch.saloni_3
      - switch.saloni_4
      - switch.saloni_4_auto
      - switch.saloni_5
      - switch.saloni_6
      - switch.saloni_7
      - switch.saloni_8
      - input_slider.slider1
      - input_select.timer_living_room

excellent, ! thanks !!