I am also looking for the most elegant and effective way to link KNX and HUE through HA.
I read a lot of topics here and the requirements of the users are not always to same (ie. some wants to use toggles, or even “stepped” ways to change brightness and colors as in the cookbook).
I am personally looking for a solution which would be as close as possible to the KNX way of linking GA to objects in light actuators.
So I used some automations to support turning on/off, defining a brightness value, and defining a color (DPT 232.600 - RGB or DPT 251.600 - RGBW), but also send feedback telegrams with the actual state of the hue light. This is my first attempt at HA scripting so do not hesitate to let me know if you see things which could be improved.
First the code and then some remarks :
- You need to activate the “fire_event” in the knx configuration and specify the GA range which will be used for all the control commands of the Hue lights.
You should also expose the state of the Hue light to KNX
fire_event: true
fire_event_filter: ["11/*/*"] # this GA range may vary for you
expose:
- type: 'binary'
entity_id: light.hue_light_1
address: '11/4/1' # this GA is used to feedback the state (ON/OFF) of the Hue light
- Create a KNX binary sensor through which we will switch the state of the Hue ON/OFF
- platform: knx
name: 'KNX to Hue Light 1'
state_address: '11/0/1' # this GA is used to switch the Hue Light ON/OFF
sync_state: False
- Edit the automation yaml file to set brightness, color, and receive feedback about the light state
- alias: "KNX to Hue Light 1 ON/OFF"
trigger:
platform: state
entity_id: binary_sensor.knx_to_hue_light_1
action:
- entity_id: light.hue_light_1
service_template: >
{% if is_state('binary_sensor.knx_to_hue_light_1', 'on') %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
- alias: "KNX to Hue Light 1 VALUE"
trigger:
platform: event
event_type: knx_event
event_data:
address: '11/2/1' # GA to set the Hue brightness
condition:
- condition: template
value_template: '{{ (trigger.event.data.data != 0) }}' # Block read requests (which only send "0")
action:
- service: light.turn_on
data_template:
entity_id: light.hue_light_1
brightness: '{{ trigger.event.data.data[0] | int }}'
- alias: "KNX to Hue Light 1 COLOR"
trigger:
platform: event
event_type: knx_event
event_data:
address: '11/3/1' # GA to set the Hue color
condition:
- condition: template
value_template: '{{ (trigger.event.data.data != 0) }}' # Block read requests (which only send "0")
action:
- service: light.turn_on
data_template:
entity_id: light.hue_light_1
rgb_color:
- "{{ trigger.event.data.data[0] | int }}"
- "{{ trigger.event.data.data[1] | int }}"
- "{{ trigger.event.data.data[2] | int }}"
# Uncomment line below if you use DPT 251.600 - RGBW
# white_value: '{{ trigger.event.data.data[3] | int }}'
- alias: "KNX to Hue Light 1 FB VALUE"
trigger:
platform: state
entity_id: light.hue_light_1
action:
- service: knx.send
data_template:
address: '11/5/1' # this GA is used to feedback the BRIGHTNESS of the Hue light
payload:
- "{{ state_attr('light.hue_light_1', 'brightness') | int }}"
- alias: "KNX to Hue Light 1 FB COLOR"
trigger:
platform: state
entity_id: light.hue_light_1
condition:
condition: template
value_template: "{{ state_attr('light.hue_light_1', 'rgb_color') != None }}"
action:
- service: knx.send
data_template:
address: '11/6/1' # this GA is used to feedback the COLOR of the Hue light
payload:
- "{{ state_attr('light.hue_light_1', 'rgb_color')[0] | int }}"
- "{{ state_attr('light.hue_light_1', 'rgb_color')[1] | int }}"
- "{{ state_attr('light.hue_light_1', 'rgb_color')[2] | int }}"
# Uncomment lines below if you use DPT 251.600 - RGBW
# - >
# {% if state_attr('light.hue_light_1', 'white_value') != None %}
# {{ state_attr('light.hue_light_1', 'white_value') | int }}
# {% else %}
# {{'0'}}
# {% endif %}
# - 0
# - 15
You may wonder why I didn’t use the “Automation” for the ON/OFF trigger and state feedback, but rather the KNX “Binary Sensor” and “Binary Expose”. Here are the 2 reasons :
-
I did not figure out how to differentiate a knx event for writing 0 (turning off the light) and a “GroupValueRead”. So every time a KNX device would send a “read request” on the GA, the light would turn OFF.
-
By using the Binary expose, it is possible to use a read request from KNX to retrieve the status of the Hue light (I didn’t find an easy way to expose a state attribute to show the status of the brightness and color, so those 2 do not support read requests. I could have used some template for that but the code would be even more complex).
Regarding the color to be set, depending of what your KNX switch support, you may opt for the RGB or RGBW DPT (232.600 or 251.600). If you use the RGBW, you should un-comment the 2 lines in the automation yaml code.
I admit that it’s a lot of lines to perform something quite simple, and maybe solutions like this AppDaemon script by da-anda would be more useful, I still have to investigate it.
Anyway, for sure an easier way to connect those 2 technologies together would be great for HA users.