Using a sensor as value template in Template light

I have a template light, the following code works fine. With the exception that i cannot get the feedback of the actual light value. Is it not possible to set up the “value_template” as a sensor directly as i have done here? And if no, what should i do different?

- platform: template
  lights:
    kjellergang_ltrapp:
      friendly_name: "Lys Kjellertrapp"
      value_template: sensor.lx_trappelys
      turn_on:
          service: rest_command.set_trappelys
          data_template:
            brightness: 100
      turn_off:
          service: rest_command.set_trappelys
          data_template:
            brightness: 0
      set_level:
        service: rest_command.set_trappelys
        data_template:
            brightness: "{{ ((brightness | float / 255 ) * 100) }}"

the problem is that you haven’t actually defined a template or told the template to look at the state of the entity. you just gave it an entity_id.

value_template: '{{states.sensor.lx_trappelys.state}}'

or maybe even better:

value_template: '{{ states("sensor.lx_trappelys") }}'

1 Like

Thanks, that works, but i got another weird problem, the underlying code now works, with the exeption that the sensor returns 100 as max light, so the “level_template” doesn’t show the right level.

I tried exchanging the line for this:

level_template: ‘{{ (states.sensor.lx_trappelys.state) * 2.55 }}’

But now, the value_template doesn’t work, even though i have changed nothing in that line, that really confuses me.

- platform: template
  lights:
    kjellergang_ltrapp:
      friendly_name: "Lys Kjellertrapp"
      level_template: '{{ (states.sensor.lx_trappelys.state) * 2.55 }}'
      value_template: '{{states.sensor.lx_trappelys.state|int > 0}}'
      turn_on:
          service: rest_command.set_trappelys
          data_template:
            brightness: 100
      turn_off:
          service: rest_command.set_trappelys
          data_template:
            brightness: 0
      set_level:
        service: rest_command.set_trappelys
        data_template:
            brightness: "{{ ((brightness | float / 255 ) * 100) }}"

Please post the configuration for sensor.lx_trappelys.

There is no configuration directly, it is a sensor generated by a custom component, it returns the light level from 0-100, it at the moment returns “98.8”

So it’s not entered in configuration.yaml but automatically discovered by Home Assistant? OK.

Are you sure the sensor’s state contains a numeric value and not a string? This template won’t work if the state is a string:

'{{ (states.sensor.lx_trappelys.state) * 2.55 }}'

In the next template, you appear to treat the sensor’s state as being a string and use int to convert it:

      value_template: '{{states.sensor.lx_trappelys.state|int > 0}}'

Hmm, how can i check? If i write {{states.sensor.lx_trappelys.state|int}} it seems to work (except that it jumps to the wrong value, it show about 1/3 of full of the light is on 100%), but it might be the int-part making the string into a number?

You said the sensor is produced by a custom component so I would check the component’s code.

Or just assume it is a string and convert it to a number using int (integer numbers) or float (floating point numbers).

That may be due to a miscalculation when converting between Home Assistant’s brightness range of 0-255, to the device’s range of 0-100. 100 is approximately 1/3 of 255.

I tried this code, and it seems like neither values work

- platform: template
  lights:
    kjellergang_ltrapp:
      friendly_name: "Lys Kjellertrapp"
      level_template: '{{ states.sensor.lx_trappelys.state|float) * 2.55 }}'
      value_template: '{{ states.sensor.lx_trappelys.state|int > 0 }}'
      turn_on:
          service: rest_command.set_trappelys
          data_template:
            brightness: 100
      turn_off:
          service: rest_command.set_trappelys
          data_template:
            brightness: 0
      set_level:
        service: rest_command.set_trappelys
        data_template:
            brightness: "{{ ((brightness | float / 255 ) * 100) }}"

This works hoever, except the 100 vs 255 problem. I don’t get why also the value template doesn’t work in the first instance, it it exactly the same.

- platform: template
  lights:
    kjellergang_ltrapp:
      friendly_name: "Lys Kjellertrapp"
      level_template: '{{ states.sensor.lx_trappelys.state|int  }}'
      value_template: '{{ states.sensor.lx_trappelys.state|int > 0 }}'
      turn_on:
          service: rest_command.set_trappelys
          data_template:
            brightness: 100
      turn_off:
          service: rest_command.set_trappelys
          data_template:
            brightness: 0
      set_level:
        service: rest_command.set_trappelys
        data_template:
            brightness: "{{ ((brightness | float / 255 ) * 100) }}"

what does that mean? are you getting compile/config check errors? what exactly are you seeing to tell you it’s not working?

I mean it doesn’t show the right level, or state (on/off) of the light. (it doesn’t change when i turn on or change the light level)

using the templates that don’t work from your last posted code above put them into the template editor and see what the results are for both of them.

level_template: '{{ states.sensor.lx_trappelys.state|float) * 2.55 }}'
value_template: '{{ states.sensor.lx_trappelys.state|int > 0 }}'

I just tried that same template (with my own different sensor) in the template editor and it worked fine.

Both return the expected results for me in the template editor. The value template work fine if i comment out the “level_template” if i keep it in, it doesn’t work. I am totally confused.

with the code that’s not working inserted into the config do you get any errors on the homeassistant.log file?

No, i get no errors either.

I think I’m out of ideas at this point. As far as I can see it should all be working with your “non-working” code.

the only other thing I see is that your indentation is off in the turn_on & turn_off sections. the stuff in those sections need moved back to the left a couple of spaces. And in the set_level section the brightness: needs moved two spaces left too. I’m not sure if that’s a copy/paste error tho since you said the other code is working and it looks the same. But it’s something to check tho.

You said sensor.lx_trappelys reports a value from 0 to 100. Your level_template multiplies it by 2.55 which produces a floating-point number. However Home Assistant’s brightness is an integer number between 0 and 255.

Try this:

      level_template: '{{ (states.sensor.lx_trappelys.state | float * 2.55) | round(0) }}'
      value_template: '{{ states.sensor.lx_trappelys.state | int > 0 }}'

I was about to tell @123 that you already tried that but then I looked again at your non-working code and I think I just found your problem in the code.

You only have one ) at the end right after “float” in the level_template but none at the beginning -

level_template: ‘{{ states.sensor.lx_trappelys.state|float) * 2.55 }}’

so try what @123 has above (also adding the “round” ) and it should fix it.

Thanks to everyone, it was a combination of different things i think, the missing ) for one of the lines, and also the custom component suddenly stopped functioning sometime during the day without me noticing. It works now. I am very appreciative of all the help