How to add an additional condition to a motion controlled light YAML

I moving all my lights from the Philips hue over to Home Assistant. Now I try to get the hang of using the motion sensor (PIR) to control the lights. With the help of Gemini I got a functioning YAML code, that works well. However, I tried several ways to integrate an additional condition, so that the light is not triggered when there is enough light. Every time the automation just didn't work anymore. I need help to understand how to deal with that.

Here's the functioning automation:

alias: Motion Hallway front (new)
description: >-
  Turns on light based on time of day when motion is detected; turns off after 5
  minutes of no motion.
triggers:
  - type: occupied
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: be0ab254feaa96064d1013129e4e8fef
    domain: binary_sensor
    trigger: device
    id: motion_on
  - type: not_occupied
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: be0ab254feaa96064d1013129e4e8fef
    domain: binary_sensor
    for:
      minutes: 5
    trigger: device
    id: motion_off
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_on
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: "23:00:00"
                    before: "07:00:00"
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 10
              - conditions:
                  - condition: time
                    after: "20:00:00"
                    before: "23:00:00"
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 60
            default:
              - action: light.turn_on
                target:
                  entity_id: light.lamp_hallway_front
                data:
                  brightness_pct: 100
      - conditions:
          - condition: trigger
            id: motion_off
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.lamp_hallway_front
mode: restart

Here's the code snipped considering the brightness, I have based on the GUI:

alias: "Motion Hallway front: day on"
description: ""
triggers:
  - type: occupied
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: be0ab254feaa96064d1013129e4e8fef
    domain: binary_sensor
    trigger: device
conditions:
  - condition: time
    after: "08:00:00"
    before: "22:00:00"
  - type: is_illuminance
    condition: device
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: c23481b5ffab4692cf41a82b6e4e244d
    domain: sensor
    above: 11
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id:
        - light.lamp_hallway_front
    data:
      brightness_pct: 100
mode: single

What I tried so far is to integrate the following part into different places in the code and nothing worked.

  - type: is_illuminance
    condition: device
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: c23481b5ffab4692cf41a82b6e4e244d
    domain: sensor
    above: 11

Can anybody provide some hints how to do that properly? Many thanks in advance.

I would at the condition to your original automation like below. The way it works is when the automation is triggered the choose option checks to which one it was. If it was not_occupied and it lasted for 5 minutes then the light is turned off. If it was occupied then it checks to see what additional conditions are met. The first is if the trigger occurred between 23:00 and 7:00. If so the light is turned on at a brightness of 10. The second is if the trigger occurred between 20:00 and 23:00. If so the light is turned on at a brightness of 60. The third is if the trigger occurred between 7:00 and 20:00. In addition, the illuminance must be above 11. If so the light is turned on at a brightness of 100. If none of the above conditions are met then as a default the light is turned on at a brightness of 100. Based on this you should be able to adjust the following code to meet your needs.

alias: Motion Hallway front (new)
description: >-
  Turns on light based on time of day when motion is detected; turns off after 5
  minutes of no motion.
triggers:
  - type: occupied
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: be0ab254feaa96064d1013129e4e8fef
    domain: binary_sensor
    trigger: device
    id: motion_on
  - type: not_occupied
    device_id: 51c8ebb5a497aaafe3a816b8c092a313
    entity_id: be0ab254feaa96064d1013129e4e8fef
    domain: binary_sensor
    for:
      minutes: 5
    trigger: device
    id: motion_off
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_on
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: "23:00:00"
                    before: "07:00:00"
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 10
              - conditions:
                  - condition: time
                    after: "20:00:00"
                    before: "23:00:00"
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 60
              - conditions:
                  - condition: time
                    after: "07:00:00"
                    before: "20:00:00"
                  - type: is_illuminance
                    condition: device
                    device_id: 51c8ebb5a497aaafe3a816b8c092a313
                    entity_id: c23481b5ffab4692cf41a82b6e4e244d
                    domain: sensor
                    above: 11
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 100                      
            default:
              - action: light.turn_on
                target:
                  entity_id: light.lamp_hallway_front
                data:
                  brightness_pct: 100
      - conditions:
          - condition: trigger
            id: motion_off
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.lamp_hallway_front
mode: restart
1 Like

THANKS! I'll check this out tonight.

I'd suggest reviewing this as well :down_arrow:

1 Like

mentioning the cookbook…

Thanks for this nice tip. Took me a while to implement this - but I do see that it is worth it. It is simpler to read as well.

Here's my final YAML for controlling my lights based on motion and several conditions. Just in case somebody is interested:

alias: Motion Hallway front (new)
description: >-
  Turns on light based on time of day when motion is detected; turns off after 3
  minutes at night, and 5 minutes during the day/evening.
triggers:
  - entity_id: binary_sensor.motion_sensor_philips_sml001_2
    to: "on"
    id: motion_on
    trigger: state
  - entity_id: binary_sensor.motion_sensor_philips_sml001_2
    to: "off"
    for:
      minutes: 2
    id: motion_off_night
    trigger: state
  - entity_id: binary_sensor.motion_sensor_philips_sml001_2
    to: "off"
    for:
      minutes: 5
    id: motion_off_day
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_on
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: "23:00:00"
                    before: "07:00:00"
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 10
              - conditions:
                  - condition: time
                    after: "20:00:00"
                    before: "23:00:00"
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 60
              - conditions:
                  - condition: time
                    after: "07:00:00"
                    before: "20:00:00"
                  - condition: numeric_state
                    entity_id: sensor.motion_sensor_philips_sml001_illuminance_2
                    below: 12
                sequence:
                  - action: light.turn_on
                    target:
                      entity_id: light.lamp_hallway_front
                    data:
                      brightness_pct: 100
      - conditions:
          - condition: trigger
            id: motion_off_night
          - condition: time
            after: "23:00:00"
            before: "07:00:00"
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.lamp_hallway_front
      - conditions:
          - condition: trigger
            id: motion_off_day
          - condition: time
            after: "07:00:00"
            before: "23:00:00"
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.lamp_hallway_front
mode: restart

1 Like

If you are interested, the following version takes advantage of Trigger Variables to move all of the control logic into each trigger (as opposed to employing a choose in actions). The result is just one action light.turn_on that sets the light to the brightness computed by the respective trigger's level variable (a value of 0 turns off the light).

In Visual Mode:


(I don't have your light entity on my system so that's why it indicates 'Unknown entity')

In YAMl mode
alias: Motion Hallway front (new)
description: >-
  Turns on light based on time of day when motion is detected; turns off after 3
  minutes at night, and 5 minutes during the day/evening.
triggers:
  - alias: Motion Detected
    trigger: state
    entity_id: binary_sensor.motion_sensor_philips_sml001_2
    to: "on"
    variables:
      level: >
        {% set h = now().hour %}
        {% set is_low = 
           states('sensor.motion_sensor_philips_sml001_illuminance_2') | float(0) < 12 %}
        {{ 10 if h < 7 or h == 23 else
           60 if 20 <= h < 23 else
           100 if 7 <= h < 20 and is_low else none }}
      is_true: "{{ level is not none }}"
  - alias: No Motion Night
    trigger: state
    entity_id: binary_sensor.motion_sensor_philips_sml001_2
    to: "off"
    for:
      minutes: 2
    variables:
      level: 0
      is_true: "{{ now().hour < 7 or now().hour == 23 }}"
  - alias: No Motion day
    trigger: state
    entity_id: binary_sensor.motion_sensor_philips_sml001_2
    to: "off"
    for:
      minutes: 5
    variables:
      level: 0
      is_true: "{{ 7 <= now().hour < 23 }}"
conditions:
  - alias: Can proceed to action
    condition: template
    value_template: "{{ is_true }}"
actions:
  - alias: Turn light on/off
    action: light.turn_on
    target:
      entity_id: light.lamp_hallway_front
    data:
      brightness_pct: "{{ level }}"
mode: restart
1 Like