Template Sensor %

Hi

I would like to rewrite below template sensor.

I would like to set if state is “off” 0.15

If state is “on” , 100% brigthness_pct = 10 , I want the sensor to calculate the watt based on the brigthness_pct?

The perfect setup would be if I could only add the max consumption and it would do the calculations for me based on the brigthness_pct?

  • platform: template
    sensors:
    watt_light_kjellerstue_1:
    unit_of_measurement: “W”
    value_template: >-
    {% if is_state(‘light.kjellerstue_1’, ‘on’) %}
    10
    {% else %}
    0.15
    {% endif %}

Brightness_pct can be used to set the brightness, but it’s not exposed as an attribute so you’d have to use brightness which is an integer from 1 to 255.

So basically if you’re saying that 100% (brightness 255) should equal 10, then presumably 50% should equal 5, so I think (in my slightly sleepy state) the formula would be to multiply the brightness level by 10 and divide by 255.

So…

- platform: template
  sensors:
    watt_light_kjellerstue_1:
      unit_of_measurement: "W"
      value_template: >-
        {% if is_state('light.kjellerstue_1', 'on') %}
          {{ (state_attr('light.kjellerstue_1', 'brightness')*10) / 255 }} 
        {% else %}
          0.15
        {% endif %}

I can’t work out off the top of my head whether that will always return a round number so you might need to round the decimal points in the output :man_shrugging:

Also if ‘10’ is a changeable number you could swap that out for the state of an input_number, and update the input number whenever you need to adjust the calculation.

Anyway, hopefully this gets you most, if not all, the way there.

Hi Mac
Thanks for your help, this was just what I needed :slight_smile:

1 Like