Remapping light brightness range

I pieced together the bits of configs mentioned above for an ESPHome Tuya dimmer with min/max of 15%/100%, and it mostly worked. However the dimmer value of the template light didn’t update in Home Assistant when I adjusted the dimmer level on the device itself. So I needed to add a reverse mapping in level_template (which was suggested before, but for value_template, which I think is a typo?). Anyway, here’s a full working config:

- platform: template
  lights:
    kitchen_under_cabinet_lights_dimmer_ui:
      friendly_name: "Kitchen Under Cabinet Lights"
      level_template: >
        {% if state_attr('light.kitchen_under_cabinet_lights_dimmer', 'brightness') <= 39 %}
        1
        {% else %}
        {{ 1.17647 * (state_attr('light.kitchen_under_cabinet_lights_dimmer', 'brightness') | float - 38.25) | int }}
        {% endif %}
      value_template: "{{states('light.kitchen_under_cabinet_lights_dimmer')}}"
      turn_on:
        service: light.turn_on
        entity_id: light.kitchen_under_cabinet_lights_dimmer
      turn_off:
        service: light.turn_off
        entity_id: light.kitchen_under_cabinet_lights_dimmer
      set_level:
        service: light.turn_on
        data_template:
          entity_id: light.kitchen_under_cabinet_lights_dimmer
          brightness: "{{ ( 0.85 * brightness | float + 38.25 ) | int }}"  

(Apologies for bumping an old thread, but this was the only thing I could find regarding this issue.)

2 Likes

Hi guys,

I’m wondering how to get the remapping working with an RGBWW light-component. I’m using a Texas Instrument TLC59208F Output component for controlling LED-strip. The nature of this 94Khz PWM signal causes non-linearity. Below 15% brightess is basically lights off.

I tried to add examples to output and light component but ESPHome complains about non-existing type. Do I need to configure 5 of these template outputs because using RBGWW? Where to find properties like “value_template” and “level_template”. Can’t find in search. Help is appreciated!

[EDIT]
Larger snippet with context of platform:output would be helpful.

Perhaps this is of use to you? Template Light - Home Assistant

I think a feature request would be a good thing. I can image how many are out there that want light brightness scale remapping.

I managed through automations to deal with low brightness levels, which the LED lamps still easily handles once they are turned on, by shortly putting the lamps at their threshold level they need to actually get going and light up, and set them back to the previous lower level using a scene.create service.
But with automations it is not possible to limit the scale on te low end in a good manner, because once you set the light below the low threshold at which it actually will turn off, the automation that would prevent this is too late.

Some feature request about this subject that I had found:

1 Like

Can you help me please, I need a template as following:
30%=0%
65%=100%

Thank you

I think it should be
"{{ ( 0.1373 * brightness | float(default=255) +30 ) | int(default=165) }}"

You can define min_power: 30% in the output component to remap the brightness range of light between 30-100%. Home Assistant frontend will consider 30% as 0%. This can be achieved when using a RobotDyn AC dimmer.

here’s my ESPHome monochromatic light dimmer configuration:

output:
  - platform: ac_dimmer
    id: dimmer1
    gate_pin: D7
    zero_cross_pin:
      number: D6
      mode:
        input: true
    min_power: 32%


light:
  - platform: monochromatic
    output: dimmer1
    name: Dimmerized Light
    default_transition_length: 2000ms

This is exactly what I needed, thank you @mkfink!

I’ve annotated my version to explain the math mapping values between the ranges 0-255 and 0-100%.

light:
  - platform: template
    lights:
      primary_bedroom_dimmer_corrected:
        friendly_name: "Bedroom Dimmer Corrected"
        # level_template maps from the 0-255 physicial/device-native brightness range to the user-facing range 0-100%
        # My minimum % brightness is 30%
        # 76.5 = 0.3 * 255 # alternately you could do 77 = math.ceil(0.3 * 255)
        # 1.4286 ~= 255 / (255 - 76.5) # alternately you could do 1.4326 ~= 255 / (255 - 77)
        level_template: >
          {% if state_attr('light.primary_bedroom_dimmer_switch', 'brightness') <= 76.5 %}
          1
          {% else %}
          {{ 1.4286 * (state_attr('light.primary_bedroom_dimmer_switch', 'brightness') | float - 76.5) | int }}
          {% endif %}
        value_template: "{{states('light.primary_bedroom_dimmer_switch')}}"
        turn_on:
          service: light.turn_on
          entity_id: light.primary_bedroom_dimmer_switch
        turn_off:
          service: light.turn_off
          entity_id: light.primary_bedroom_dimmer_switch
        # set_level's data_template maps from the 0-100% user-facing brightness range to the 0-255 physical/device-native brightness range
        # My minimum % brightness is 30%
        # 0.70 = 100% - 30%
        # 76.5 = 77 - 0.5 # because (0.7 * 255) + 77 = 255.5 which is too high. alternately you could do 77 = math.ceil(0.3 * 255)
        set_level:
          service: light.turn_on
          data_template:
            entity_id: light.primary_bedroom_dimmer_switch
            brightness: "{{ ( 0.70 * brightness | float +  76.5) | int }}"
3 Likes

I was looking for the same thing, meaning I wanted to scale my LED lamp because it starts at 22% DC. So I found this thread.
And yes, one might use home assistant template sensors but I’d rather have it done in the ESP32. I think the hint of @koencomposer is very helpful and showed me the way to go:

# LEDC PWM output
output:
  - platform: ledc
    pin: GPIO21    
    id: pwm_output
    frequency: 1220Hz
    min_power: 0.22
    zero_means_zero: true

# Usage as a light
light:
  - platform: monochromatic
    output: pwm_output
    name: "C3 floor lamp"

Very important to set zero_means_zero: true because otherwise the lamp is never off.

I was facing a similar issue with a 0-10v dimmer where the light would turn off at 22%. I tried using templates but then I discovered the HA HACS integrations lightener. Releases · fredck/lightener (github.com). It’s all done via the GUI. I set the Brightnes mapping as such.

1: 22
20: 40
40: 60
60: 80
80: 90
90: 95
100: 100

1 Like