Turn lights off after 2 minutes when lux below 200 not working

Hi, I have been living with this for a year now and have decided to ask for help. I have an automation that I set up to turn lights on if luminance is below 200 and then switch the lights off 2 minutes after occupancy is not detected. The lights won’t turn off after 2 minutes. They stay on. My config is as follows, any advice is welcome. I am using a Hue sensor through a Zigbee device.

alias: Kitchen Lights On
description: "Trun Kitchen Lights on when luminance is below 200 and time out of 2 minutes if not occupied"
trigger:
  - type: occupied
    platform: device
    device_id: 1b8983149995a740136b71a326b6ba7e
    entity_id: 595d57fc3cf9d4827ab96553bbd46b7b
    domain: binary_sensor
    id: kitchen-on
    for:
      hours: 0
      minutes: 0
      seconds: 0
    enabled: true
  - type: not_occupied
    platform: device
    device_id: 1b8983149995a740136b71a326b6ba7e
    entity_id: 595d57fc3cf9d4827ab96553bbd46b7b
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: kitchen-off
condition:
  - condition: or
    conditions:
      - type: is_illuminance
        condition: device
        device_id: 1b8983149995a740136b71a326b6ba7e
        entity_id: b315baa31e6eba12c3a26a195ec85a65
        domain: sensor
        below: 200
action:
  - choose:
      - conditions:
          - condition: trigger
            id: kitchen-on
        sequence:
          - service: light.turn_on
            data: {}
            target:
              area_id: kitchen
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: kitchen-off
        sequence:
          - service: light.turn_off
            data:
              transition: 20
            target:
              area_id: kitchen
    default: []
mode: single

You have two triggers, so why not two automations? Much easier to debug and maintain.

May I ask why you use device triggers? I only ask because more people seem to be doing it lately - is there a blog or YouTube video that recommends it? I’ve never used them myself - are there advantages?

You have a condition of below 200, but is this true when the lights are on (and you want them to shut off after no occupancy)?

alias: Kitchen Lights On
description: "Trun Kitchen Lights on when luminance is below 200 and time out of 2 minutes if not occupied"
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion
    from: 'off'
    to: 'on'
    variables:
      is_true: "{{ states('sensor.kitchen_illuminance') | int(0) < 200 }}"
  - platform: state
    entity_id: binary_sensor.kitchen_motion
    from: 'on'
    to: 'off'
    for:
      minutes: 2
    variables:
      is_true: "{{ true }}"
condition:
  - condition: template 
    value_template: "{{ is_true }}"
action:
  - if: "{{ trigger.to_state.state == 'on' }}"
    then:
      - service: light.turn_on
        data: {}
        target:
          area_id: kitchen
    else:
      - service: light.turn_off
        data:
          transition: 20
        target:
          area_id: kitchen
mode: single

Replace binary_sensor.kitchen_motion and sensor.kitchen_illuminance with the entity_ids of your actual motion and illuminance sensors.


EDIT

Correction. Removed extraneous closing parenthesis.

1 Like

I got it from a YouTube video, from what I remember it kind of made sense to use triggers. I watched this video How to use Trigger IDs in Home Assistant - Tutorial - YouTube

I did not consider having two separate automations.

Hi Ben, yes basically, if the lights come on (because lx is below 200) it should switch off after 2 minutes (of no occupancy), but if luminance goes above 200 it won’t switch off after 2 minutes of no occupancy.

That is the problem I am having, it works at night, but during the day when it (luminance) dips below 200 the lights come on when motion is detected, but then it gets brighter outside and the lights won’t switch off after 2 minutes of no occupancy detected because luminance is above 200.

I would have thought that the OR condition was not like an AND condition.

@123 I get the following error when I try to save

Message malformed: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['trigger'][0]['variables']['is_true']

Question, does this line need an extra bracket?

“{{ states(‘sensor.signify_netherlands_b_v_sml003_illuminance’) | int(0) < 200 ) }}”

That’s due to a typo I made in the template. I have corrected the example posted above. Simply remove the closing parenthesis after 200.

| int(0) < 200 ) }}”
               ^
               |
         Remove this 

The ‘OR’ doesn’t even do anything there; it’s for having multiple conditions where you only need one of them to be true. In your case, you only have the one.

1 Like

YAAY, it worked. I managed to save it by changing it to the visual editor and back to YAML and saved it and removed the )

So basically what you are saying is that the condition must be met before it can trigger the action right? But if I have 2 or more conditions with an OR in there, then either one of that one must be validated before the action is actioned. Is that right?

Yep, that’s it exactly.

1 Like