Remapping light brightness range

I have created a dimmer for some led lights using ESPhome and the monochrome light component.

The problem I have is that the LEDs only dim to 30% before they go off.

I would some how like to remap this so 30%=0%dimming level and 100%=100%

Can this be done.

I think you can create a template light with a custom template for the brightness.

Looks like this might work.

I am very much a learn by example.

Any ideas where to start. I looked at the documentation but can’t relate the examples to what I want to do.

For changing the scale of a variable, I found this resource. The first answer contains a nice formula.

So in the template editor this seems to work (0 transforms to 30, that’s what you wanted):

{% set brightness = 0 %}
{{ ((0.7*(brightness-100))+100) | int }}

In a template light, you can use this in the ‘set_level’ part:

        set_level:
          - service: light.turn_on
            data_template:
              entity_id: light.kitchenlight
              brightness: "{{ ((0.7*(brightness-100))+100) | int }}"

I’m not sure if you need to change the level_template as well. It’s early :slight_smile: If you need more help, just let me know.

So near but yet so far.

Been messing around and learning a bit of templating but can’t quite get it to work

I have set up a new light template as follows

    - platform: template
      lights:
        kitchen_plinth_lights:
          friendly_name: "Kitchen plinth lights"
          level_template: "{{state_attr('light.plinth_lights', 'brightness')}}"
          value_template: "{{states('light.plinth_lights')}}"
          turn_on:
            service: light.turn_on
            entity_id: light.plinth_lights
          turn_off:
            service: light.turn_off
            entity_id: light.plinth_lights
          set_level:
            service: light.turn_on
            data_template:
              entity_id: light.plinth_lights
              brightness: "{{ ((0.88*(brightness-256))+256) | int }}"

The template light now controls the plinth lights. Turns on/off the lights and adjust the brightness.

However I still have a problem

The scale is 0 to 255 so changed this in the template formula but the template light adjusts the plinth light to the same brightness and does not seem to use the formula.

Any ideas

brightness: "{{ ( 1.4326 * state_attr('light.plinth_lights', 'brightness')|float - 110.3 )|int }}"

This maps 77 (30% of 255) to 0, and 255 to 255.

Though I think you are going to get a weird feedback effect. The brightness attribute and brightness variable should be seperate entities.

Thanks for the pointer all working now.

When I said it was working it seems not quite.

Changing the brightness is a step behind

I am using kitchen plinth lights to control the plinth lights

The turning on and off works find but changing the brightness is one step behind. It is as if the brightness level is sent before the slider moves.

The formula is working to correct the level.

Can you post the full code code you are using now for your template light?

Also, what do you mean with this: I am using kitchen plinth lights to control the plinth lights

So here is the code for the template light

   - platform: template
     lights:
       kitchen_plinth_lights:
         friendly_name: "Kitchen Plinth lights"
         turn_on:
           service: light.turn_on
           entity_id: light.plinth_lights
         turn_off:
           service: light.turn_off
           entity_id: light.plinth_lights
         set_level:
           service: light.turn_on
           data_template:
             entity_id: light.plinth_lights
             brightness: "{{ ((0.88*(state_attr('light.kitchen_plinth_lights', 'brightness')-256))+256) | int }}"

So the setup is that “Kitchen plinth lights” is the entity visible in the frontend and what the user is in control of for turning on, off and dimming.

In the background there is an entity called “plinth lights” (generated from esphome). User has no access to this in the frontend.

so user adjusts “kitchen plinth lights” and HA passes the modified brightness levels and on/off state to “plinth lights” which controls the esphome dimmer.

Hope this now makes sense.

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!