Use brightness as a conditon

Hi,

I have an automation that switches a light to a certain brightness level when one of my PIRs detected motion. This works fine.

alias: gang loop
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.gang_pir1gang_263
    to: 'on'
  - platform: state
    entity_id: binary_sensor.gang_pir2gang_269
    to: 'on'
condition: []
action:
  - repeat:
      until:
        - condition: and
          conditions:
            - condition: state
              entity_id: binary_sensor.gang_pir1gang_263
              state: 'off'
              for:
                hours: 0
                minutes: 0
                seconds: 5
                milliseconds: 0
            - condition: state
              entity_id: binary_sensor.gang_pir2gang_269
              state: 'off'
              for:
                hours: 0
                minutes: 0
                seconds: 5
                milliseconds: 0
      sequence:
        - service: light.turn_on
          target:
            entity_id: light.gang_ganglamp_110
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.gang_ganglamp_110
mode: single

I want to add a condition to this automation to NOT run this automation when the lamp already has a brightness >99%.
.
Reason: when I switch the light manually on (at 100%), I want to switch it also manually off, not via the automation .Sometimes I need this light on while nobody triggers the PIRs.

If I paste below code in the tempate editor while the lamp is out.
{{state_attr(‘light.gang_ganglamp_110’,‘brightness’) | int > 99}} → returns false
{{state_attr(‘light.gang_ganglamp_110’,‘brightness’) | int < 99}} → returns true

When I add this code to the automation as template, the light no longer switches on when one of the PIRs get triggered, not when I use ">"or “<” in the condition.

What do I do wrong here?

Thanks for help!!

Grtz
Jules

extra config info:

Light:

You don’t need to use a template. Numeric state conditions support attributes.

condition:
  condition: numeric_state
  entity_id: light.gang_ganglamp_110
  attribute: brightness
  above: 99

https://www.home-assistant.io/docs/scripts/conditions/#numeric-state-condition

1 Like

Thanks! Numeric state, does not give the option for brightness the pull down menu.


How can I fix that?

Turn your light on then try it again.

1 Like

I used this code as condition in yaml

  condition: numeric_state
  entity_id: light.gang_ganglamp_110
  attribute: brightness
  below: 99

then I converted back from yaml editor to visual editor

even after restarting my automations
The light no longer switches on when a PIR detects motion.

When your light is off the brightness attribute is unknown or none.

Try this:

condition:
  condition: or
  conditions:
    - condition: numeric_state
      entity_id: light.gang_ganglamp_110
      attribute: brightness
      above: 99
    - condition: state
      entity_id: light.gang_ganglamp_110
      attribute: brightness
      state: none

Unfortunately no success.

this is what I put in my condition:

condition: or
conditions:
  - condition: numeric_state
    entity_id: light.gang_ganglamp_110
    below: '99'
  - condition: state
    entity_id: light.gang_ganglamp_110
    state: none

note my small modification where I changed brightness above 99 to below 99 as only want this automation to execute when the brightness is not 100%.

The light does not switch on and I got this message on the trace timeline.

Look at the step details. Why did the conditions fail?

I’m no coding expert but when I look at the step details it seems like the numeric state condition checks the state of the entity (on/off) while we need it to check an attribute (brightness).

How can I fix this?

Look at what I wrote and look at what you have. You are missing attribute: brightness in both conditions.

shit, my bad. Dropped out when rebuilding it. I just updated the condition.

condition:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: light.gang_ganglamp_110
        attribute: brightness
        below: '98'
      - condition: state
        entity_id: light.gang_ganglamp_110
        attribute: brightness
        state: none

I got 2x the same error on the two conditions.
result: false message: attribute ‘brightness’ of entity light.gang_ganglamp_110 does not exist
see screenshot.

Wouldn’t this work?

condition:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: light.gang_ganglamp_110
        attribute: brightness
        below: 98
      - condition: state
        entity_id: light.gang_ganglamp_110
        state: 'off'
1 Like

It works although I’m not sure if the message I get “attribute does not exist” is desired in coding.

My other problem is that this automation does not solve my problem.
What I want:
1: the light to switch on (dimmed) when PIR is triggered and after time-out switch off .
2: when I manually double click the light switch, the light should go to 100%. The automation should not switch off the light.

Problem:
The switch is in reach of the PIR, so the repeat-until loop will be running once the switch is pressed.
When the light is set to 100% during the loop, the light will dim after timeout and continue in the loop.

In summary I have 2 questions:

1: is it OK to get the message “attribute does not exist”?
2: how can I improve the automation to make it work as intended.

id: '1639313865361'
alias: gang loop
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.gang_pir1gang_263
    to: 'on'
  - platform: state
    entity_id: binary_sensor.gang_pir2gang_269
    to: 'on'
condition:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: light.gang_ganglamp_110
        attribute: brightness
        below: 98
      - condition: state
        entity_id: light.gang_ganglamp_110
        state: 'off'
action:
  - repeat:
      until:
        - condition: and
          conditions:
            - condition: state
              entity_id: binary_sensor.gang_pir1gang_263
              state: 'off'
              for:
                hours: 0
                minutes: 0
                seconds: 5
            - condition: state
              entity_id: binary_sensor.gang_pir2gang_269
              state: 'off'
              for:
                hours: 0
                minutes: 0
                seconds: 5
      sequence:
        - service: light.turn_on
          target:
            entity_id: light.gang_ganglamp_110
          data:
            brightness: 40
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.gang_ganglamp_110
mode: single

Thanks for help!

Ok, you can’t use the brightness attribute in a numeric condition as it does not exist when the light is off.

Also I did not even look at your actions. I have no idea why you are doing any of that.

I’m on my mobile but will help you with a couple of simple automations to do what you want later.

Hi Tom,

Let me try to explain why I’d like to use brightness in my automation.

In our hallway I want to switch on the light based on motion and switch off after x-time no motion.
I have an automation that switched the lights to a 40% brightness (dimmed) when one of the PIRs is triggered. After X-time no motion, the lights should switch off.

I don’t want the lights to switch based on motion when I need to be in the hallway many times. For example when I put my groceries away. To override the automation I want to double click the wall switch (Fibaro Dimmer 2 with pulse sensor) which sets the brightness to 100%. In the automation I want to kill the loop when brightness is 100% and also prevent the loop to restart.
When I switch the lights off, the automation should take over again.

Looking forward to your suggestions how I can do this or what alternatives you see for this.

Sorry, this thread hasn’t seen any activity but I’m trying to do something very similar - assigning the brightness of a switch to a variable.

Is there a different attribute other than brightness to use?

Hi

I use the brightness in conditions like this:

  - conditions:
      - condition: template
        value_template: >
          {{(float( state_attr('light.my_light', 'brightness') ,0)/2.54)  > 10}}

My threshold to detect an off state is 10 in this case. The division of 2.54 converts it to brightness percentage.

cheers

3 Likes

image

After checking out the traces, it looks like the UI adds quotes to the value. Once I went into the YAML and removed the quotes, it worked as expected.

condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: light.laundry_overheadlight_laundry_overheadlight_dimmer
        attribute: brightness
        state: 255