Hi,
just wanted to share finished template that took me way too long to write.
The problem:
Working brightness range for my light dimmer was 50-100, so the HA value of 0 needs to be translated to 50, value of 254 need to be translated to 99 but for the energy efficiency I want the value of 255 to stay 255 - dimmer fully on.
Solution:
Light template that calculates the right values in both directions:
light:
- platform: template
lights:
edison_lamp_fixed:
#unique_id needed for sharing entity with voice assistant
unique_id: "edisonlampdixed25012024"
friendly_name: "Edison Lamp Fixed"
#Get the state of the light, 'if' fixes the 'unknown' state error when starting ha
value_template: "{{'on' if is_state('light.bulb_edison', 'on') else 'off'}}"
#Translate brightness from range 50-100 to 0-255 for the panel, |int(50) fixes the none > int error when booting.
level_template: >-
{% set input_min = 50 %}
{% set input_max = 100 %}
{{ ((min(state_attr('light.bulb_edison', 'brightness')|int(input_min),input_max) | float - input_min) / (input_max-input_min) ) * 255 }}
turn_on:
service: light.turn_on
entity_id: light.bulb_edison
turn_off:
service: light.turn_off
entity_id: light.bulb_edison
set_level:
service: light.turn_on
entity_id: light.bulb_edison
#Translate the range 0-255 from the panel to 50-100 and set it to the dimmer, save some energy and dimmer life by using 100% duty cycle (255) when brightness is set to 100
data_template:
brightness: >-
{% set input_min = 50 %}
{% set input_max = 100 %}
{% if brightness == 255 %}
255
{% else %}
{{ (brightness | float / 255) * (input_max - input_min) + input_min }}
{% endif %}
This gives no errors when booting, and can be easely modified for any input_min and input_max values of the dimmer. I hope it will be useful for someone, and let me know if it can be improved - I’m just a beginner.