Need logic example to regulate brightness attribute based on measured lux

I want to regulate the brightness of my HUE lamps based on the measured Lux from my Xiaomi light sensor. The light sensor is in a room where i want to keep constant light intensity between a certain time and sunset.
I have all sensors etc installed succesfully but need an example of how to configure the logic that takes said Lux value and adjusts the brightness attribute of several entities.
Does this belong in a script that is called using a time pattern of in a template?
I now have it working via an automation which calls a scene, but i rather have a function that in- or decreases brightness “seamless”.

Please advise.

Sounds like a job for a PID controller.

This shows my age… studied Automatic Control in the 70-80’s and PID is the first topic.

Thanks!

perhaps you know this as well:
i get and output of the PID controller and would like to write that into the brightness attribute of a light.

  1. would i be able to write lamp.brightness = PID_output ?
  2. where would i place the statement to write the brightness and what code is to be used? can i place it in the configuration of the PID?

Depending on your light device’s abilities and how you have set the maximum you may need to change the service call shown below. Not all light devices/integrations support setting by brightness percentage, so you would need to use the brightness value (0-255).
The gist of how to do it is the following automation:

alias: PID light
description: "Set light brightness based on PID sensor output"
trigger:
  - platform: state
    entity_id:
      - sensor.pid_light_sensor
    not_from:
      - unknown
      - unavailable
condition: []
action:
  - service: light.turn_on
    data:
      brightness_pct: "{{ trigger.to_state.state | int }}"
    target:
      entity_id: light.your_light
mode: single

Thanks!
Made this with a hue bulb, now on to the “simple” task of tuning the controller.

- id: '1675151412758'

  alias: LUX CONTROL

  description: PID output controlling the brightness
  trigger:
  - platform: state
    entity_id:
    - sensor.lux_regulator
    not_from:
    - unknown
    - unavailable
  condition: []
  action:
  - service: light.turn_on
    data:
      brightness: '{{ trigger.to_state.state | int }}'
    target:
      entity_id: light.fireplace
  mode: single
~~~