Auto adjusting brightness

I’m trying to get my hallway lights to adjust to the sun and weather conditions. Using the Buienradar integration there is a sensor for solar strength. I’m hopeing to use this value to calculate an approptiate brightness level. With the calculation below I would have a 50 brightness nightlight and as long as the irradiation doesn’t go over 1000kW I should be fine (255 is max brightness).

- id: '1581631610216'
  alias: Lampen - Gang movement
  description: ''
  trigger:
  - entity_id: binary_sensor.motion_sensor_158d0001b19074
    platform: state
    to: 'on'
  - data:
      brightness: '{{ ((states.sensor.br_irradiance.state | int) / 5) + 50 }}'
    entity_id: light.gang_lamp
    service: light.turn_on

Can someone help me find the error in this formula?

"{{ states('sensor.br_irradiance.state')|int // 5 + 50 }}"

Nope, unfortunately that’s not working. I’ve tried it in the Automations.yaml and also trough the GUI.

Using the Developer tools > template, I got this to work.
brightness: {{ states.sensor.br_irradiance.state|int // 5 + 50 }}

But once I copy it into the automation it’s not doing anything

When I replace this line by the outcome of the template builder:
brightness: 208
That works fine in the automation

  - data:
      brightness:

Should be

  - data_template:
      brightness:
1 Like

Overlooked that one. Good spot!

"{{ states('sensor.br_irradiance') | int // 5 + 50 }}"

Though to be fair, Noto already caught this one

1 Like

Thanks, that did the trick :smiley: :smiley:

data_template:
brightness: “{{ states.sensor.br_irradiance.state|int // 4 + 30 }}”

After running this for some days I must say I’m really pleased with the outcome. No more beeing blinded at night when I walk into the hallway or useless light during the day. Also added a condition so the light can’t be triggered by our cat when we’re not home/sleeping. Finally added in a IF statement so the brightness doesn’t go over the lights max value.

Brightness at the lowest setting was good enough but brightness 0 didn’t turn on the light. That’s why the +1 is included.

- id: '1581631610216'
  alias: Lampen - Gang movement
  description: ''
  trigger:
  - entity_id: binary_sensor.motion_sensor_158d0001b19074
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: alarm_control_panel.ha_alarm
    state: disarmed
  action:
  - data_template:
      brightness: '{% if ((states.sensor.br_irradiance.state|int // 4 + 1) < 255) -%}{{states.sensor.br_irradiance.state|int // 4 + 1}}{%- else -%}255{%- endif %}'
    entity_id: light.gang_lamp
    service: light.turn_on

Thanks for your help!

1 Like