Help needed with Template Light

Hi guys!

Quite new to Hass.io, I’m running into a brick wall with the problem below. I would appreciate any help or guidance!

I’m trying to group two IKEA Tradfri lights using the code below which is working great until i try to implement set_level.
The Tradfri lights only has the brightness attribute when they are “on”.
The new entity id “light.hallway” has no brightness attribute, even though “light.panel_hallway_1” and “light.panel_hallway_2” is turned on and got the brightness attribute.

I want to set brightness to both lights and always the same value, i do this today with the below code but i was hoping to use entity id “light.hallway” instead of “light.panel_hallway_1” and “light.panel_hallway_1”.
Also down the road use “states.light.hallway.attributes.brightness” later instead of “states.light.panel_hallway_1.attributes.brightness” because i have a fixture with 6 pulps i want to change to Tradfri and this would come in handy.

automation: 
  - alias: Adjust brightness hallway
    trigger:
      platform: state
      entity_id: input_number.hallway_brightness
    action:
      service: light.turn_on
      data_template:
        entity_id: 
          - light.panel_hallway_1
          - light.panel_hallway_2
        brightness: '{{ trigger.to_state.state | int * 2.55 | round(0) }}'

This is the description from https://home-assistant.io/components/light.template/

set_level
    (action) (Optional) Defines an action to run when the light is given a brightness command.

The below code works fine and im getting a entity id named “light.hallway”.

platform: template
lights:
  hallway:
    friendly_name: "Hallway lighting"
    turn_on:
      service: light.turn_on
      entity_id:
        - light.panel_hallway_1
        - light.panel_hallway_2
    turn_off:
      service: light.turn_off
      entity_id:
        - light.panel_hallway_1
        - light.panel_hallway_2

When i try to the code to get brightness something breaks and my entity id “light.hallway” is gone. and i cannot figure out what is wrong.

platform: template
lights:
  hallway:
    friendly_name: "Hallway lighting"
    level_template: "{{ states.light.panel_hallway_1.attributes.brightness|int }}"
    value_template: "{{ states.light.panel_hallway_1.attributes.brightness|int > 0 }}"
    turn_on:
      service: light.turn_on
      entity_id:
        - light.panel_hallway_1
        - light.panel_hallway_2
    turn_off:
      service: light.turn_off
      entity_id:
        - light.panel_hallway_1
        - light.panel_hallway_2
    set_level:
      service: light.turn_on
      data_template:
        brightness: "{{ brightness }}"

This is the log right after a “hassio homeassistant restart”:

2017-10-18 08:37:11 ERROR (MainThread) [homeassistant.components.light.template] UndefinedError: 'None' has no attribute 'attributes'
2017-10-18 08:37:12 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/tasks.py", line 179, in _step
result = coro.send(None)
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 381, in async_process_entity
new_entity, self, update_before_add=update_before_add
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 210, in async_add_entity
yield from entity.async_update()
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 210, in coro
res = func(*args, **kw)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/light/template.py", line 220, in async_update
if state in _VALID_STATES:
UnboundLocalError: local variable 'state' referenced before assignment
2017-10-18 08:37:18 WARNING (MainThread) [homeassistant.setup] Setup of notify is taking over 10 seconds.
2017-10-18 08:37:19 WARNING (MainThread) [homeassistant.setup] Setup of sensor is taking over 10 seconds.
2017-10-18 08:37:20 WARNING (MainThread) [homeassistant.setup] Setup of media_player is taking over 10 seconds.
2017-10-18 08:37:21 WARNING (MainThread) [homeassistant.setup] Setup of device_tracker is taking over 10 seconds.
2017-10-18 08:37:34 WARNING (MainThread) [homeassistant.setup] Setup of tts is taking over 10 seconds.
2017-10-18 08:37:48 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Total udgift, the state is unknown.
2017-10-18 08:37:48 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Affugter effekt, the state is unknown.

I came across this thread because I’m having difficulties with Template Light aswell.

However I think I see your problem with the brightness. You also need to add which entity_id’s the brightness should be written to. Just like you did for the turn on and turn of actions.

Of course! That makes perfect sense, I will try it later!

Thank you very much for your time @tbrasser

no problem! I’m also trying to merge multiple lights, however my setup is a little bit more complicated because every light also has a switch which I want to keep in sync.

I don’t know whether my config helps you, but you can see my struggles/progress here: Combining Z-Wave Relays with Milight Lights

I’ll have a look.

Cheers!

Hi @tbrasser
I totally forgot this thread and i was replying, thinking that this was my new thread.
If you wanna have a look at that one as it is the up-to-date version of my light template, though a different problem with the “light.group” that does not turn off, even though the entities do.

Sorry for the confusion!