Dimming doesn't work below 20% and above 75%, need help remapping light brightness range

Hi all,

First post here so firstly thank you to everyone contributing. After roughly 2 months of discovering HA I’ve managed to get hold of a HP Elitedesk, get my first ever VM set up, and get HA running along with Z2M and a bunch of dimmer/switch modules (Moes Zigbee). There were roadblocks along the way but I’ve managed to Google/Youtube my way through them thankfully.

I’ve currently only got the lights in HA along with some Eufy cameras which I’m not completely in love with, but the reason for the post is I’m finding the lights don’t seem to dim below 20% or above 75% (from what I can tell).

What I’d like to do is have 0% in my frontend (mushroom cards) be 30% on the lights, and show 100% when the light is at 75%.

I’ve seen the following topic (Remapping light brightness) reference remapping lights but I can’t figure out how to get going with Templates. I’ve seen a Youtube video but it doesn’t relate to lights and although I’ve got this far with HA I’m no programmer so I don’t completely understand code/scripting.

I’m hoping someone can chime in here and help me get this sorted as I don’t really like the idea of having to control light brightness between 20% and 75% on the sliders.

Thank you!

This converts 0–100 into 30–75 as a template sensor, but the same template should work in a light that supports brightness templates:

template:
  - sensor:
      - name: Converted brightness
        state: "{{ (states('input_number.brightness')|int(0) * 0.45 + 30)|int(0) }}"
        unit_of_measurement: "%"

If you wanted to use different values, the 0.45 is from ((75 - 30) / 100). Here’s the same thing but with tunable values:

template:
  - sensor:
      - name: Converted brightness
        state: >
          {% set out_min = 30 %}
          {% set out_max = 75 %}
          {{ (states('input_number.brightness')|int(0)*(out_max-out_min)/100+out_min)|int(0) }}
        unit_of_measurement: "%"

Thank you!

Apologies in advance for the stupid question, but how to I go about implementing this? Lets say I’ve got a device named ‘kitchen_lights’, how would I add the above (ideally the 2nd tunable value version you shared) with the kitchen lights?

Create an Input Number helper called brightness from min 0 to max 100:

Use that on your dashboard as the control.

Then create an automation with a State trigger referencing your input_number.brightness and an Action to call the light.set_brightness with the adjusted number. You can set the trigger up via the UI but you’ll need to use the Edit as YAML function to set up the template in the action.

trigger:
  - platform: state
    entity_id: input_number.brightness
action:
  - service: light.turn_on
    target:
      entity_id: light.kitchen_lights
    data:
      brightness_pct: >
        {% set out_min = 30 %}
        {% set out_max = 75 %}
        {{ (states('input_number.brightness')|int(0)*(out_max-out_min)/100+out_min)|int(0) }}

I’ve just had a go: it’s easiest to create the automation to call the service to set a brightness of e.g. 50%, then switch to Edit as YAML mode (three dots at the top) and adjust to look like this (but with your kitchen lights):

Got that all in place, but what do I use in the dashboard? I’m using Mushroom Cards at the moment but when I use the Mushroom Light Card I’m not able to find the automation/input number? Is it just a case of using the normal light entity?

Apologies again if this is super basic stuff! I’ll get there eventually haha. Thank you for the help so far!

Edited:
Ignore the above, I was being stupid! Got the Mushroom Light Card in place but I’m seeing an error saying “Failed to call service input_number/turn_off. Service not found” when I click it. Any ideas?

Does your light do colors or effects?

No, just brightness control for these ones.

This light template will create a new light, and you’ll hide the old light

light:
  - platform: template
    lights:
      truncated_light:
        friendly_name: Truncated Light
        value_template: "{{ is_state('light.your_light', 'on') }}"
        turn_on:
          service: light.turn_on
          target:
            entity_id: light.your_light
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.your_light
        level_template: >
          {% set level = state_attr('light.your_light', 'brightness') or 0 %}
          {{ (1.8182 * level - 92.727) | int }}
        set_level:
          service: light.turn_on
          data:
            brightness: "{{ 0.55 * brightness + 51 }}"
        availability_template: "{{ states('light.your_light') in ['on', 'off', 'unknown'] }}"

EDIT: I should mention that you need to replace light.your_light with whatever your light entity_id is. Make sure to keep the quotes around that entity_id too if it’s wrapped in quotes. Also, you may want to rename truncated_light and Truncated Light to whatever you want to name it. It can’t share the same name as your source light light.your_light.

Just to confirm, I’ve added my light/ID and the code looks like below, but where/how do I add this?:

light:
  - platform: template
    lights:
      truncated_light:
        friendly_name: Truncated Light
        value_template: "{{ is_state('light.kitchen_lights', 'on') }}"
        turn_on:
          service: light.turn_on
          target:
            entity_id: light.kitchen_lights
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.kitchen_lights
        level_template: >
          {% set level = state_attr('light.kitchen_lights', 'brightness') or 0 %}
          {{ (1.8182 * level - 92.727) | int }}
        set_level:
          service: light.turn_on
          data:
            brightness: "{{ 0.55 * brightness + 51 }}"
        availability_template: "{{ states('light.kitchen_lights') in ['on', 'off', 'unknown'] }}"

you add it in configuration.yaml

Thank you. Added, but I won’t know it’s working until the weekend now lol.

I’d need to do this for all the lights that I need to adjust the brightness range for right? Is it a case of copying and pasting the same code and changing the light entity, or simply add in all the other light entity ID’s after the kitchen_lights one?

Why can’t you test it now? Also, what hardware is this? A lot of times hardware has the ability to truncate the dimming starts and stops before it even is exposed to HA.

I won’t be at the property until the weekend, and I can’t tell from the dashboard if it’s working or not, unless there’s a way to test it?

I’m using Moes dimmer modules for the lights, with dimmable spotlights/downlights. As far as I’m aware more traditional rotary dimmers do have something to physically limit the range but with these it’s all software.

Just use the newly created entity. FYI this only affects the entity in HA, this does not modify the actual hardware ranges. If that’s what you’re after, this will not solve that problem. There’s nothing in HA that will solve that problem. That can only be adjusted via the hardware’s firmware.

Another stupid moment. I’ve got it added as a Mushroom card now with a brightness slider and when it turns on it does only turn the light on to 75% which is good news!

But when I go to dim it via the slider it’s showing error “Failed to call service light/turn_on. must contain at least one of entity_id, device_id, area_id.”

Any ideas?

Yeah, I forgot the entity_id, just add it to the service

Got it working, thank you so much for the help!

Is there an article that explains the maths? Just in case I need to adjust the 20%/75% figures for other lights.

I just know the math. You can use the compensation integration to derive the values for you. Or use excel to fit a line to your data points

Thanks for all your help! Much appreciated!

Hello,
Sorry to bring this topic again. I used @petro template. Everything works like a charm. However, I receiving an error every time when I turn off the light:

Logger: homeassistant.components.template.light
Source: components/template/light.py:456
Integration: Template (documentation, issues)
First occurred: 21:02:24 (1 occurrences)
Last logged: 21:02:24

Received invalid brightness : -92 for entity light.truncated_light. Expected: 0-255

Could You help on this?