Automation to adjust LED brightness based on measured luminosity (lux)?

I have a Shelly Dimmer 2 connected to a dimmable LED.

I also have a Shelly Motion which can measure luminosity in lux - e.g.:

Is there some way to setup a HA automation, that sets the LED brightness, based on the ambient light levels?

Firstly - how would you trigger this automation? Should it be done on some kind of periodic schedule, or is there some way to do it when the light levels change by > X amount?

I saw in the HA docs you can do a numeric trigger - but this is on the actual value - e.g.:

trigger:
  - platform: numeric_state
    entity_id: sensor.shellymotionsensor_60a423beb6c4_luminosity
    for:
      hours: 0
      minutes: 0
      seconds: 10
    above: '15'

However, there’s no way to trigger based on the delta, or change in a value, is there?

Secondly, how would you “templatise” (if that’s the right word) the luminosity value in the brightness? For example, let’s say the measured lux in this area is between 100-500 - and we just want to scale some fixed value by “lux / 100” - how would you do that in HA?

Yes, trigger on any change of state of the lux sensor and use a condition template to check the difference between the trigger to and from states. e.g. a 5 lux change:

trigger:
  platform: state
  entity_id: sensor.shellymotionsensor_60a423beb6c4_luminosity
condition:
  platform: template
  value_template: "{{ ( trigger.from_state.state|float(0) - trigger.to_state|float(0) )|abs > 5 }}"

I use this. You would have to develop your own equation specific to your sensor and needs:

template:
  - sensor:
    - name: 'Calculated Light Brightness No Late Dim'
      unit_of_measurement: "Int"
      state: >
        {{ ( [0, (-0.0428 * states('sensor.weatherflow_brightness')|float(0) + 291 )|int(0), 255]|sort )[1] }}

It is used like this:

  - service: light.turn_on
    target:
      brightness: "{{ states('sensor.calculated_light_brightness_no_late_dim')|int(0) }}"
    data:
      entity_id: light.lifx_front_porch
      kelvin: 2700
1 Like