Brightness in scenes does not work with template lights

Dear all, I have a lamp hanging over my dining table with four ikea trädfri bulps (for dimming purposes) and I also have a shelly switch in the wall behind the haptic switch. To get both bulbs and switch working togehter I have configured a template light. This ensures the switch is turned on if necessary together with the bulbs and only turns of the bulbs if the switch is pressed again. This works really like a charm, also diming the lights is possible via the appropriate cards in the frontend.

What however not works is setting the template light to a certain light level / brigthness when using scenes. Neither when using scene.apply within an automation nor when manually activating a scene. Is this a known problem? How can I tackle this??

Please see below my code snippets:

Template Light

  - platform: template
    lights:
      diningroom_lamp:
        friendly_name: "Esszimmerlampe (unified)"
        level_template: "{{ state_attr('light.bulbs_diningroom', 'brightness')|int }}"
        value_template: "{{ is_state('switch.light_diningroom', 'on') and is_state('light.bulbs_diningroom', 'on')}}"
        color_template: "{{ state_attr('light.bulbs_diningroom', 'rgb_color') | float}}"
        turn_on:
          - service: homeassistant.turn_on
            target:
              entity_id: switch.light_diningroom
          - service: light.turn_on
            target:
              entity_id: light.bulbs_diningroom
            data:
             transition: 1
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.bulbs_diningroom
          data:
            transition: 1
        set_level:
          - service: homeassistant.turn_on
            target:
              entity_id: switch.light_diningroom        
          - service: light.turn_on
            target:
              entity_id: light.bulbs_diningroom
            data_template:
              brightness: "{{ brightness }}"
        set_temperature:
          service: light.turn_on
          target:
            entity_id: light.bulbs_diningroom
          data_template:
            color_temp: "{{ color_temp }}"

Scene:

- id: livingroom_lunch
  name: Essen
  icon: mdi:silverware
  entities:
    light.diningroom_lamp: 
      state: 'on'
      brightness_pct: 50
      color_temp: 300    
    light.led_tv_panel: 'off'
    light.livingroom_corner:
      state: 'on'
      brightness_pct: 1
    light.stonewall_livingroom: 'off'
    light.livingroom_spots:
      state: 'on'
      brightness_pct: 20
      color_temp: 500
    media_player.samsung_qled: 'off'

Code behind my button in the lovelace UI to activate the scene

                  - type: button
                    entity: scene.livingroom_eat
                    show_name: false
                    tap_action:
                      action: call-service
                      service: scene.turn_on
                      service_data:
                        transition: 2
                        entity_id: scene.livingroom_eat
                      target: {}
                    hold_action:
                      action: none

Hello,
did you find a solution for this? I run in the same problem at the moment…

No, unfortunately not.

The issue we had was that the turn_on function can receive the brightness variable along with it. In the case of a scene call, this seems to be typically the case. However, during manual operation, users will either click separately to turn the light on/off or adjust the slider, which triggers either set_level or turn_on/turn_off independently.

Therefore, we need to include the brightness variable handling within the turn_on function to ensure it works properly. How this works with your special hardware behind, I do not know.
But in your case you should add something like this call in the turn_on function:

turn_on:
... your other calls...
  - service: light.turn_on
    target:
      entity_id: light.bulbs_diningroom
    data_template:
      brightness: "{{ brightness | default(255) | int) }}"
      transition: 1

The 255 can also be also a variable (input_number) you use for storing the brightness even if the light was turned off, to turn it on with the same brightness next time in case of no brightness is sent to the turn_on function.

So far my findings. This worked for me in scenes now perfectly.