HOWTO: Turn ON lights with motion/turn OFF lights with no motion, but DO NOT turn OFF lights when manually enabled

Hello,

since I came across the problem, that with my basic automation the lights were turned off when I manually turned them on and the motion sensor registered motion, I would like to share my solution to prevent this. Sure this solution is not 100% from me, but in fact I had issues to understand at that time how to implement the hints from the people of the forum. I crossread so many threads and different solutions, I got a bunch of yaml snippets but I was not that sure how to use them. Especially since many people just paste “one code example” but in fact when realizing the structure, they are for example two automations in one code example. So it must be splitted into two pieces.

This morning I just woke up and the first thing which came up to my mind was how I could fix that problem. I will try to explain this step by step, for ppl who are like me without a deep or no knowledge of yaml and HA. So for the more experienced users, you can skip this thread. But you are pretty much welcome to improve my solution if I forgot usecases. But please then step by step, that also newcomers can understand this.

Requirements:
working Sensor (e.g Fibaro FGMS-001. Or any other sensor you would like to use)
working Lights (I used two Philips Hue for testing purposes)

  1. Create a Helper Object (Settings - Helper Element)
  • add helper element and choose switch
  • name it: e.g. Bedroom_Ambilight_boolean_ON_OFF
  1. Create Automation for turn light ON after sunset and before sunrise (Settings - Automation)
  • give it a good name (I prefer to start with LIGHT_ROOM_FUNCTION-ON)
  • set a trigger: (this will be your sensor) to state “ON”
  • set condition (must be with OR) after sunset and a new one with before sunrise
  • set condition: the given light must be OFF to trigger this automation
  • set action: turn on light
  • set action: input_boolean.turn_on for the helper you created before

YAML CODE:

alias: LICHT_Schlafzimmer_AMBIENT-AN
description: after sunset (-30m)&& before sunrise
trigger:
  - platform: state
    entity_id: binary_sensor.fgms001_zw5_motion_sensor_home_security_motion_detected_2
    to: 'on'
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:30:00'
      - condition: sun
        before: sunrise
  - condition: state
    entity_id: light.schlafzimmer_ambient
    state: 'off'
action:
  - service: light.turn_on
    data: {}
    entity_id: light.schlafzimmer_ambient
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.licht_schlafzimmer_boolean_on
mode: single
  1. Create Automation for turn the light OFF
  • again give it a good name
  • set trigger: use the sensor again but use as state “off” this time (I used here a delay of 30 seconds)
  • set condition: same as for the ON automation (after sunset, before sunrise)
  • set condition: state of the boolean you defined before must be on
  • set action: turn off the light
  • set action: turn off the boolean

YAML CODE:

alias: LICHT_Schlafzimmer_AMBIENT-AUS
description: Nach Sonnenuntergang
trigger:
  - platform: state
    entity_id: binary_sensor.fgms001_zw5_motion_sensor_home_security_motion_detected_2
    to: 'off'
    for: '00:00:30'
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:30:00'
      - condition: sun
        before: sunrise
  - condition: state
    entity_id: input_boolean.licht_schlafzimmer_boolean_on
    state: 'on'
action:
  - service: light.turn_off
    data: {}
    entity_id: light.schlafzimmer_ambient
  - service: input_boolean.turn_off
    data: {}
    entity_id: input_boolean.licht_schlafzimmer_boolean_on
mode: single

I’m sure the code could be improved. But I’m happy now that it works :slight_smile:
Let’s see what comes up to my mind after I wake up the next time :smiley:

KR

fastboot