Remapping light brightness range

This might be confusing, because you will be getting conflicting advice from me and @tom_l. But I think this is the correct code for brightness in your template light:

brightness: "{{ (0.698 * (brightness-255)) +255 | int }}"

This will map the range 0-255 to 77-255 (77 is 30% of 255).

I’m not sure about your value_template. Perhaps you also need to adjust the range, but I am not sure. If it doesn’t work properly right now, you might try:

value_template: >
  {% if state_attr('light.plinth_lights', 'brightness') < 77 %}
  0
  {% else %}
  {{ ((1.433 * state_attr('light.plinth_lights', 'brightness')-255)) + 255) | int }}
  {% endif %}

This will map 77-255 to 0-255, so the other way around.

I don’t have time to try this in my own setup right now. Perhaps @tom_l can lend a hand as well. Especially since we seem to have conflicting opinions about the right solution :slight_smile:

Wouldn’t be the first time I was wrong. Just double checked my calcs, not sure how I came up with the previous result, but the linear equation is actually:

Y = 0.7*Brigt +77 

x=0 → y=77 (30.2% of 255)
x=255 → y=255.5

Yours is close but it gives 81 out for 0 in (32% of 255). As I said pretty close.

So:

"{{ ( 0.77 * state_attr('light.plinth_lights', 'brightness')|float +77 )|int }}"

Should do it except for this problem I pointed out earlier:

Which is why you are seeing:

Say you start with a brightness of 77. The equation will set it to 0. Now your brightness is zero for the next time you go to change it but the minimum it ever should be is 77.

You need to separate the brightness attribute from the light slider. Not something that is possible if using the core light card.

The calculation, they might be a bit different but that’s okay. I don’t get why you are using the state attribute for brightness in the set_level part? It should be {{brightness}} in my opinion.

Changed to {{brightness}} from the attribute and all now seems to work ok.

Thanks for all the help.

Great to hear.

Hi guys,

I’m trying to replicate the above but I seems to be unable so. In principle I want to achieve the same. I have a light that only works between 48% and 100% of the brightness and therefore I want to create a template that remaps this range from 0 to 100%.

The template now looks as the below and as a result every brightness change in the template results in the light to be set to 30%. Brightness up/down does not alter the the original value unless this was not 30%. Also change the state.attribute to brightness throws me an error.

Suggestions are welcome :slight_smile:

- platform: template
  lights:
    living_room_table_lamp:
      friendly_name: "Lamp2"
      turn_on:
        service: light.turn_on
        entity_id: light.ac_141205a_3
      turn_off:
        service: light.turn_off
        entity_id: light.ac_141205a_3
      set_level:
        service: light.turn_on
        data_template:
          entity_id: light.ac_141205a_3
          brightness: "{{ ( 0.77 * state_attr('light.Lamp2', 'brightness')|float +77 )|int }}"

You should use the brightness variable in the set-level part. Please see my comments further up.

This is what I tried earlier:

data_template:
          entity_id: light.ac_141205a_3
          brightness: "{{ (0.698 * (brightness-255)) +255 | int }}"

but that throws the following error when changing brightness:

You need more parentheses I believe. ) before |int. Right now you are only converting 255 to integer. Not the whole calculation. Looking back, my example is incorrect :slight_smile:

That seemed indeed to be the problem. Thanks!

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:

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