Help! Light brightness by lux level automation

(might be silly but)
why do u need an AND condition if you only have one condition ?

I do have more conditions, but these aren’t the problem in this case so I just simplified the automation at that point.

Like this:

controllightwithlux:
  sequence:
    - service: light.turn_on
      entity_id: light.lamp1
      data_template:
        transition: 5
        brightness: '{{ brightness }}'

I mean your script that has data and data template.

the scope of your jinja varaibles is wrong. You’re tyring to create variables in the variables section, then use the bri variable in the brightness section. They need to be encompassed in a single section.

This should work:

- alias: 'Cool automation name here'
  trigger:
    platform: time
    minutes: '/1'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.room1_lux
        below: '14000'
  action:
    - service: script.turn_on
      entity_id: script.controllightwithlux
      data_template:
        variables:
          brightness: >
            {%- set lux = states.sensor.room1_lux.state | int -%}
            {%- set perc = (14000 - lux) / (14000 / 100) | int -%}
            {{ ((76.5 * perc) / 100) | int }}
1 Like

You’re my man! Thank You petro!

That simply did the job! I’ve marked it as the solution :clap:t3::clap:t3::clap:t3:

1 Like

So I have to ask, why are you triggering this automation once every minute, when it really only needs to run whenever the state of sensor.room1_lux changes (and it’s below 14000)? Why not this:

- alias: Blah
  trigger:
    platform: numeric_state
    entity_id: sensor.room1_lux
    below: 14000
  condition:
    # Put your other conditions here...
  action:
    # Run your actions...

This will trigger whenever the state or any attribute of sensor.room1_lux changes. Yes, I know what the docs say about the numeric_state trigger, but they’re wrong; see documentation PR 6054. I’ve looked at the code, and even tested it - it simply does not just fire once when the state “crosses a threshold.”

pnbrucker:
I knew that question would come…

  1. I had trouble in the past having the numeric_state to trigger my automation
  2. I want my script to refresh every minute in order to follow the decrease in lux from the light sensor - unless this could be done by adding some line directly in the script?

That’s my point. When the lux value decreases, it will generate a state_changed event for that entity, which will cause the numeric_state trigger to fire (assuming the value is below 14000.) No need to fire every minute if the value isn’t changing.

I’d be more than happy to help if you try the numeric_state trigger again and it for some reason doesn’t work the way you expect. I currently use it in five automations and I’ve had no problems with it.

So what you’re saying is that for every update my light sensor triggers a value below the threshold it would update the script with the new brightness? I will give it a go ASAP.

And regarding the numeric_state trigger I’ve implemented in the automation now and it already seems to work - but I’ll let you know it it gives me trouble.

Thank you! :sunglasses:

Petro, it seems that you are quite skilled in these types of automations. If I want to have a fixed brightness when the LUX reaches below a certain threshold how do I do it - and/or where am I wrong?

    variables:
      brightness: >-
        {% if states.sensor.room1_lux.state | int <= 5000 %}
          40
        {% elif states.sensor.room1_lux.state | int < 5000 %}
          {%- set lux = states.sensor.room1_lux.state | int -%}
          {%- set perc = (13500 - lux) / (13500 / 100) | int -%}
          {{ ((30 * perc) / 100) | int }}
        {% endif %}

Yes. Even if the state (i.e., the lux level) doesn’t change, but just an attribute of the sensor.room1_lux entity changes, it will also trigger the automation (as long as the state, interpreted as a numeric_value, is less than the below value of 14000.)

I just verified with one of my automations. It’s triggered using a numeric_state trigger where the entity is estimated outdoor illuminance (using a custom sensor platform I wrote.) The illuminance entity updated three times, even though the state didn’t change. (It has an attribute which provides the weather condition that goes into the estimation, and it changed to rain, to cloudy, and back to rain, and the automation triggered each time.)

FYI, if you really wanted the automation to trigger only when the state changed (and its new value is below 14000), that could be done using a template trigger.

That is nice, I didn’t knew that! For sure I have some other automations where this would be very appropriate too. :muscle:t3::+1:t3::+1:t3:

You are close, you just need an else instead of elif.

And you could reorganize it so you only get the state once. EDIT: Not even sure if this makes a difference time wise because we are just accessing the object. But it does make it nice to edit in the future.

    variables:
      brightness: >-
        {%- set lux = states.sensor.room1_lux.state | int -%}
        {% if lux <= 5000 %}
          40
        {% else %}
          {%- set perc = (13500 - lux) / (13500 / 100) | int -%}
          {{ ((30 * perc) / 100) | int }}
        {% endif %}
3 Likes

Damn so close… Thank you so much for the help!
Now I can start changing my other automations as well. :slight_smile:

hi,

please let me ask why you run this with the script you posted above? And not from within the automation itself? Seems that would be simpler. Unless you call the script from elsewhere too of course.

something like:

 - service: light.turn_on
   entity_id: light.lamp1
   data_template:
     transition: 5
     brightness: >
        {%- set lux = states('sensor.room1_lux') | int -%}
        {% if lux <= 5000 %}
          40
        {% else %}
        {%- set perc = (13500 - lux) / (13500 / 100) | int -%}
          {{ ((30 * perc) / 100) | int }}
        {% endif %}

If you turn something like that into a script, you can use it on more than one device. Basically, call a script and pass variables to the script.

sure, good point. more than one device, or more than one automation. sort of a module one can use in different settings. nice indeed.

Ignore what I said here. Seems I had come to this conclusion incorrectly.

1 Like

Hi Russel,
could you please explain logic after your code and numbers? it works great, but I’m trying to better understand what those numbers are (14000 and 76.5), to make thinks adapted to my number of installed lights :slight_smile:
I’m using it with 6 xiaomi lightbulbs on top, and xiaomi lightsensor on my desk. and when I add additional lightbulbs i get more lumens, say 1 lightbulb gives 47 lum with script (97 max), 5 gives 105 lumen, and 6 gives 116.

I’m in 60 square feet room with no windows and its not bright enought :slight_smile:

          brightness: >
            {%- set lux = states.sensor.lumi_lumi_sen_ill_mgl01_8dbf783c_illuminance.state | int -%}
            {%- set perc = (14000 - lux) / (14000 / 100) | int -%}
            {{ ((76.5 * perc) / 100) | int }}