Light Template not sending turn_on when triggered

I am having some trouble with a Light template. Everything is working as expected, except i can’t get the Turn_on action to work. Turn_off works properly. And if i manually turn on the light this is supposed to control, the light template updates to the correct state. Set_level also works as expected. Any help is much appreciated.

light:
  - platform: template
    lights:
      kitchen_ceiling_lights:
        friendly_name: "Kitchen_ceiling_lights"
        unique_id: kitchen_ceiling_light
        level_template: >
          {% if is_state('switch.kitchen_ceiling_panels', 'on') %}
            {{ ((state_attr('light.kitchen_ceiling_dimmer', 'brightness')|float-255*0.25)/0.75)|round }}
          {% else %}
            0
          {% endif %}
        value_template: >-
          {% if is_state('switch.kitchen_ceiling_panels', 'on') %}
            {% if state_attr('switch.kitchen_ceiling_panels', 'on') %}
              off
            {% else %}
              on
            {% endif %}
          {% else %}
            off
          {% endif %}
        turn_on:
          service: switch.turn_on
          entity_id: switch.kitchen_ceiling_panels
        turn_off:
          service: switch.turn_off
          entity_id: switch.kitchen_ceiling_panels
        set_level:
          service: light.turn_on
          entity_id: light.kitchen_ceiling_dimmer
          data_template:
            brightness: "{{ (brightness|float*0.85 + 255*0.17)|int }}"

What are you trying to do with the following portion of your value_template?

Unless your switch has an attribute named “on” (which would not be normal), the state_attr() function will return null and so the if/then will always return “on”.

Thank you, I hadn’t quite understood what that was doing, I have cleaned that up. Unfortunately the turn_on command is still not working when i toggle the light template on. Here is the change I made.

light:
  - platform: template
    lights:
      kitchen_ceiling_lights:
        friendly_name: "Kitchen_ceiling_lights"
        unique_id: kitchen_ceiling_light
        level_template: >
          {% if is_state('switch.kitchen_ceiling_panels', 'on') %}
            {{ ((state_attr('light.kitchen_ceiling_dimmer', 'brightness')|float-255*0.25)/0.75)|round }}
          {% else %}
            0
          {% endif %}
        value_template: >-
          {% if is_state('switch.kitchen_ceiling_panels', 'on') %}
            on
          {% else %}
            off
          {% endif %}
        turn_on:
          service: switch.turn_on
          entity_id: switch.kitchen_ceiling_panels
        turn_off:
          service: switch.turn_off
          entity_id: switch.kitchen_ceiling_panels
        set_level:
          service: light.turn_on
          entity_id: light.kitchen_ceiling_dimmer
          data_template:
            brightness: "{{ (brightness|float*0.85 + 255*0.17)|int }}"

My goal here is to combine the switch and the dimmer into a single light entity. I have tested the level and value template in developer tools and they are outputting on/off and 0-255 as expected.

Is the dimmer unpowered when the switch is “off”?

If the dimmer is not set up to automatically change to “on” when powered, you likely need a light.turn_on in your turn_on action sequence. And you may need to include a short delay to cover the dimmer’s bootup/connection time:

...
turn_on:
  - service: switch.turn_on
    entity_id: switch.kitchen_ceiling_panels
  - delay:
      milliseconds: 200
  - service: light.turn_on
    entity_id: light.kitchen_ceiling_dimmer
....