Turn on lights only if there is a motion detected AND light intensity is low for given time

Hello, I am trying to “fix” my working automation by adding some more conditions when to really run it.

There is a yaml code from my test. The target is turn on the lights when there is a motion AND level of illumination in lx is less than whatever number FOR some time. I do have a motion sensor in my hall more or less right in front of the door to bathroom. There’s no space to put the sensor somewhere else or face it different way. I tried that. So far the motion detect motion and turn on the lights which is fine. But it detect motion even when I am not leaving, just going to the bathroom. So I am trying to put a condition there about the illumination level. Because when I turn on lights in bathroom, which are dumb, light intensity in hall level up and of course I don’t need to turn on lights in hall. But when I am leaving I want to light them on.

alias: test_chodba
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 3a97b2732d5965cb4bf351df0372eabc
    entity_id: 8847dbccc7403ed3a0c1d4fa2023422a
    domain: binary_sensor
condition: []
action:
# both conditions must be true #
  - condition: and
    conditions:
# if light intensity is < 3lx for 5 seconds AND there is a motion detected turn on lights. #
# Time and light intensity is just for test now. #
# Presumption is when I do not turn on #
# light in bathroom, lx will not go up, therefore #
# I am leaving so after given time it will turn on lights #
# in hall and when there is no motion it will turn them off #
      - condition: template
        value_template: "{{(states('sensor.sensor_botnik_illuminance') | float < 3)}}"
      - condition: state
        entity_id: binary_sensor.pohyb_dvere_motion
        state: "on"
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.svetla_chodba
mode: single

I tried the delay but that was not working well as at night there is low light intensity so it was triggered even without a motion detected. I am struggling with wait_for_template I found here in other topic as there is only state from: ‘off’ to: ‘on’ and timeout: but there is not anything about non boolean value. Any help would be appreciated as I am quite new in HA.

Thank you in advance.

You can’t do that directly with a numeric condition. You will need to create a Template binary sensor that tracks if the lx is below your desired level. Then you will be able to use the State of the binary sensor with a duration as a condition.

Ok, thank you. Still, maybe I am doing something wrong.

Here is my template sensor, showing true or false:

- platform: template
  sensors:
    osvetleni_chodba:
      value_template: >-
        {{states('sensor.sensor_botnik_illuminance') | int > 8}}

and automations itself, when there is a motion wait for 5 second to check binary sensor. If it is true (lights in bathroom are on, therefore do nothing), if it is false (lights in bathroom are off, therefore turn on the lights):

alias: test_chodba
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pohyb_dvere_motion
    to: "on"
    id: pohyb
  - platform: state
    entity_id:
      - sensor.osvetleni_chodba
    from: null
    to: "true"
    id: osvetleni
  - platform: state
    entity_id:
      - binary_sensor.pohyb_dvere_motion
    to: "off"
    id: klid
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.pohyb_dvere_motion
            state: "on"
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - binary_sensor.pohyb_dvere_motion
                to: "on"
              - platform: state
                entity_id:
                  - sensor.osvetleni_chodba
                to: "False"
                from: null
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            timeout:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
            continue_on_timeout: true
          - action: light.turn_on
            target:
              entity_id: light.svetla_chodba
            data: {}
        alias: koupelna_zhasnuto
      - conditions:
          - condition: state
            entity_id: binary_sensor.pohyb_dvere_motion
            state: "on"
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - binary_sensor.pohyb_dvere_motion
                to: "on"
              - platform: state
                entity_id:
                  - sensor.osvetleni_chodba
                to: "True"
                from: "False"
            timeout:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
            continue_on_timeout: true
        alias: koupelna_rozsviceno
      - conditions:
          - condition: trigger
            id:
              - klid
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.svetla_chodba
        alias: chodba_klid
mode: single

It works weirdly. First it does not wait 5 seconds but 2-3 seconds only. With the delay on physical illumination sensor that is too quick. Second it completely ignore the binary sensor state anyway. With light on (state True) it still turn the lights on. Turning lights off works perfectly though :slight_smile:.

Am I missing something?

Is there a reason you are using a sensor instead of a binary sensor?

Your option conditions are not specific… and the waits need to be reconsidered.

Both the first and second options have the same condition. That means it is highly unlikely that the second option will ever be chosen… which is fine since the second option has no real actions. Are actions missing or was the goal for it just to wait? You will need additional conditions to distinguish between when the first and second options should be executed.

Regarding the wait for triggers, the following combination requires that the binary sensor is “on” to pass the condition, then changes to “off” and back to “on” to fire the trigger:

- conditions:
    - condition: state
      entity_id: binary_sensor.pohyb_dvere_motion
      state: "on"
  sequence:
    - wait_for_trigger:
        - platform: state
          entity_id:
            - binary_sensor.pohyb_dvere_motion
          to: "on"

Is that what you were going for?

Well, I am using sensor because it is there, facing the door so it will catch me anyway. This motion sensor have binary output already on/off or detected/clear. Next to it there is the illumination sensor which I have in sensors.yaml with more than something to get binary output true/false.

The idea is when motion sensor detect me it should wait for illumination level to change. If it does not change the state in 5 seconds, turn on lights in hall, if does change the state in 5 seconds, do nothing. And of course when there is no motion, turn the lights off.

Looking at it now I see this sensor I created is probably not binary even though states seem like it is. Hmm.

I think this should work for what you have described…

alias: test_chodba
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pohyb_dvere_motion
    to: "on"
    id: pohyb
  - platform: state
    entity_id:
      - binary_sensor.pohyb_dvere_motion
    to: "off"
    id: klid
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: pohyb
        sequence:
          - alias: Check that hallway has had some light in the last 5s
            condition: not
              - condition: state
                entity_id: sensor.osvetleni_chodba
                state: "False"
                for:
                  seconds: 5
          - action: light.turn_on
            target:
              entity_id: light.svetla_chodba
      - alias: chodba_klid
        conditions:
          - condition: trigger
            id: klid
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.svetla_chodba
mode: single

Tahnk you, I’ll give it a try. So far I put spare motion detector to my bathroom so if it detects me there, it won’t turn on the hall. Sort of.