Copying an attribute of a light to a sensor (by template?)

Hi,
I am using a from HA unsupported tuya device to measure the water level. Within tuya I therefore change the brightness of a lamp depending on the water level, i.e. 90% water level = 90% brightness.

The lamp can be seen and worked with in HA. Now, I would like to copy this brightness value in a template sensor variable, which I later can display i.e. as a gauge. I only worked with sensors as a source in templates.

This isn’t working, but you might get an idea what I am looking for:

      - name: "MeterL Water Prozent"
        device_class: power
        unit_of_measurement: "%"
        state: "{{ ( states('light.l_f_jnb_watertank_level.brightness') | float | round(0))  }}"

Thanks!

The brightness attribute only exists when the light is on.

The attribute brightens is a value from 0 to 255. You want the attribute brightness_pct.

You can not use the device class “power” for an entity with the unit of measurement “%”. See: https://www.home-assistant.io/integrations/sensor#device-class

Try this:

configuration.yaml

template:
  - sensor:
      - name: "MeterL Water Prozent"
        unit_of_measurement: "%"
        state: >
          {% if is_state('light.l_f_jnb_watertank_level','on') %}
            {{ state_attr('light.l_f_jnb_watertank_level','brightness_pct') }}"
          {% else %}
            0
          {% endif %}
      - name: "MeterL Water Prozent"
        device_class: moisture
        unit_of_measurement: "%"
        state: "{{ (state_attr('light.l_f_jnb_watertank_level', 'brightness') | int(0) / 255 * 100) | round(0) }}"