Template light does not get level and icon

I’ve a shelly that is controlling the built-in bathroom lighting. The lighting is turned on/off using a impulse-function thus the output needs to be “turned on → 0.1s pause → turned” off to toggle on/off.
As you understand this does not report correct status as the icon will always be off.

I’m creating a template lightning with help of input_boolean and input_number.

  • The input boolean is triggered by events in the shelly, ensuring to always toggle no matter if I control it using hass or press the switch physically. This works well.
  • The dim level is set using a helper. I “dim the light” by adding a longer pause (e.g 2s) but it’s similar to toggle on/off described above. This mimics a long press
  • The template looks like this:
- platform: template
  lights:
    bathroom_main:
      unique_id: "template_bathroom_main"
      friendly_name: "Bathroom main"
      level_template: "{{ (states('input_number.bathroom_main_dim_level')|int/100*255)|int }}"
      icon_template: >-
        {% if is_state('input_boolean.bathroom_main', 'on') %}
          {% if states('input_number.bathroom_main_dim_level')|int == 100 %}
            mdi:lightbulb-on
          {% elif states('input_number.bathroom_main_dim_level')|int == 40 %}
            mdi:lightbulb-on-40
          {% elif states('input_number.bathroom_main_dim_level')|int == 5 %}
            mdi:lightbulb-on-10
          {% endif %}
        {% else %}
            mdi:lightbulb-on
        {% endif %}
      value_template: "{{  states('input_boolean.bathroom_main')  }}"
      turn_on:
        service: script.impulse_function_bathroom_main
      turn_off:
        service: script.impulse_function_bathroom_main

This works great in general. Pressing the icon will toggle the light on/off. It’s also report the correct status (on/off).

What is not working is the brightness attribute and updating the icon.

This is how the state looks like:

My template works when I test it:

Any input what I’m missing?

What is setting the value of input_number.bathroom_main_dim_level?

If it’s script.impulse_function_bathroom_main then we need to see its contents.

I’m setting it like this and the input_number is set properly:

alias: Dim bathroom to minimal
sequence:
  - type: turn_on
    device_id: abc123
    entity_id: light.shelly_bathroom_main_switch_0
    domain: light
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 425
  - type: turn_off
    device_id: abc123
    entity_id: light.shelly_bathroom_main_switch_0
    domain: light
  - service: input_number.set_value
    data:
      value: 5
    target:
      entity_id: input_number.bathroom_main_dim_level
mode: single

Look at the example in the documentation and compare it to your example. Your version is missing set_level.

I can’t set the level on the lights, as I understand it set_level is used to actually set the level with a command. level_template according to the documentation “Defines a template to get the brightness of the light.”.

My scripts is “setting the level by pausing”. This used for motion-detection and sets the level during the day. It writes a number to an input_number, which I want the template light to read.

Your script appears to set the Input Number’s value to 5 and never to any other value.

How can the Template Light’s brightness, which is based on the Input Number’s value, be anything other than 5?

I have 3 different scripts, which pauses different about of time and sets a different amount of value.
They are identical and should be merged in the future.

If that was meant to answer my question, I didn’t understand it.

All I see above is a single script that sets a fixed value of 5. It explains why the icon never changes (and is always mdi:lightbulb-on-10).

I have a similar script which sets the value to 40. And another to 100. There’s no point in showing the code since they are similar.

The input number gets updated properly, so this is not the problem.

According to the Template Light you posted above, it calls one script.

How are making that Template Light use the other scripts?

I’ve a motion detector that triggers the different scripts based on the time of the day. This is how the light gets turned on/off 99% of the time.

The template light has basically two functions:

  • Toggling light on/off → calling the impulse-function seen in the yaml in the first post. ← Works
  • Update the icon based on the current diming level (i.e input_number)
  • Get the brightness level based on input_number

Interesting, but I think it’s obvious at this point that it doesn’t help the Template Light to work correctly.


EDIT

The values of value_template and level_template should be reported only in response to what the Template Light specifies.

For example, if light.example is a Template Light and you use light.turn_on to set its brightness to 50%, then value_template should report on and level_template should report 127 (apprx. 50% of 255).

In other words, you don’t control a Template Light via those two options, they’re meant to provide a confirmation to the Template Light being turned on/off or setting its brightness.