Trying to automate a light to turn off only if it's a set percentage (40%)

So I think there may be two solutions to this issue. I first have an automation that will turn on a lightstrip to 40% brightness & colour temperature of ~2500K when motion is detected as long as the lightstrip was already off to begin with (that way if it’s already on, then it’s not going to dim or change off the existing colour) this is working for me without any problems.

I would like an automation to turn this lightstrip off, under the following conditions:
-there has been no motion in the past 2 mins
-as long as the lightstrip is currently 40% brightness

OR

Turn off my first automation once no motion detected after 2 mins.

I used to have this configured this way via SmartThings without much of a hassle, but I’m trying to move everything over to HA. I’ve spent the last 3 hours trying to get HA to recognise the brightness of the lightstrip but it just won’t play ball and I’m completely clueless.

Here’s the current YAML for the automation I’ve currently got:

alias: Kitchen Motion night {ON}
description: >-
  Runs when kitchen motion sensor detects motion at night time, as long as
  kitchen lightstrip is currently off
trigger:
  - type: motion
    platform: device
    device_id: a099df7a277b4ac8185313f087d59b3a
    entity_id: binary_sensor.kitchen_motion_sensor_iaszone
    domain: binary_sensor
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
    after: "21:00:00"
    before: "06:00:00"
    enabled: true
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: a42aa752385cc87092757cc14e8cc9c6
        entity_id: light.kitchen_island_lightstrip
        domain: light
action:
  - type: turn_on
    device_id: a42aa752385cc87092757cc14e8cc9c6
    entity_id: light.kitchen_island_lightstrip
    domain: light
    brightness_pct: 40
  - service: light.turn_on
    data:
      entity_id: light.kitchen_island_lightstrip
      color_temp: 397
mode: single

trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_motion_sensor_iaszone
    to: 'off'
    for:
      minutes: 2
condition:
  - condition: state
    entity_id: light.kitchen_island_lightstrip
    attribute: brightness
    state: 40
action:
  - service: light.turn_off
    target:
      entity_id: light.kitchen_island_lightstrip

This doesn’t work unfortunately. The lightstrip still remains on after 2 mins.

When I press ‘Test’ on the condition, it comes back with “Condition did not pass”

Oh hang on. The brightness attribute is 0 to 255.

condition:
  - condition: state
    entity_id: light.kitchen_island_lightstrip
    attribute: brightness
    state: 102 # 40% of 255

Or to be safe:

condition:
  - condition: numeric_state
    entity_id: light.kitchen_island_lightstrip
    attribute: brightness
    below: 104 # 41% of 255
    above: 99 # 39% of 255

Brilliant, this worked! Thanks so much for your help and prompt responses. I actually got similar code using the visual editor but never got it working properly, so damn annoying haha!

I also tried the other one where you set it between 99 & 104 and that also works, but I’d rather have the exact amount since the % is always going to be exactly 40% as it’s set by automation every time.

I also tried using ChatGPT and it would always get syntax wrong or provide incorrect answers

Have a great day :smiley:

Yeah I’ve seen the gibberish config it writes.