Advanced control of any light entity from KNX (state, brightness, dim, temperature, color + states feedback)

i would like to have this feature too, since i also use the mdt glastaster as wall switches. Controlling RGB LEDs over HSV Values is currently not possible with this blueprint.

What you try to achieve is not the purpose of this blueprint.

You have to configure your knx light properly in knx.yaml with all related group adresses (see knx integration documentation) and then add whatever card on your dashboard to control it, including dimm.

Since this is not related to this blueprint at all, please use another topic if you still need assistance.

Sorry at the moment I don’t have any device using HSV so I am not gonna investigate this implementation, sorry

first of all. Thank you very much for taking the time to make this available to us!

I have been trinkering around with ETS and get everything to work with one exception.
whenever my KNX actor sends 00 00 00 (which should be “off”) my hue lights go all blue…

any idea what I might be doing wrong?

Thanks & Cheers!

Sounds strongly like the issue i encountered. You are controlling the lights using only DPT232.600 don’t you? I wrote this little Blueprint to fix that issue:

It specifically looks for 00 00 00 and turns the lights off as well as extracting the brightness value from any color value you might set.

I needed this because my Zennio Z35 wall mounts only use the RGB address, but not the dimmer or brightness addresses when updating.

1 Like

HA! …I’m still too stupid to understand why exactly it did not work with kalhimeos blueprint. But yours fixed it! (and yes I use 232.600 …aaand I also have the Z35)

Thanks so much!

Please DM for Beer + Coffee via PayPal …you’ve saved me at least a weekend :slight_smile:

Hi, sorry for the delay of my answer, I have been kept very busy recently.

This 00 00 00 RGB command was indeed a bug, and it should be fixed in the latest update of the blueprint.

I also included the brightness tweak from @Xenolphthalein , thanks for sharing your code.

Cheers

Hi,
thanks for the great blueprint!
I‘m using it for dimming and it works like a charm.

I would like to use it for tunable white temperature too, but my problem is, that I have a busch-jäger
6108/07 which supports dimming (dpt 3.007) but has no option for tunable white temperature.

Would it be possible to add this option (using dpt 3.007 to change the white temperature) or can you help me achieve that?

Thanks in advance!

Best Regards,
Jürgen

Unfortunately relative dimming Hue lights does not work. It stops mid dimming. I think the Hue Commandrate is too low and flooding my Hue network. I Heared that Dimming with Transition is far better but cant do the change. Would this be an Option?

No, I don’t think so. Transition can only be started, not stopped. Thus there is no command that can be used when you release the button at target brightness.

Dont know how other manufacturers are implementing smooth dimming with Hue like Gira but it works pretty good. I think the Transition can be stopped by sending a new command like an absolute dimming value. Read it somewhere but i have to look deeper into this topic.

I am actually working on this. I am in the process of changing the blueprint so that you can stop dimming at any point. In fact I already got very far and I am nearly finished with this. I am just struggling with one litte issue at the moment and have to do some more tests. But then I would present my results in this thread. Hopefully just one or two more weeks.

1 Like

So i just tested one lamp without the hue bridge and with a z2mqtt stick and now dimming works like a charm. Instant response dimming with 50 steps in 4 seconds is no problem. But dimming with Transition could also be beneficial and a bit smoother.

Also the Zigbee network is not getting flooded if there is a dimming mode with transitions!

So tomorrow i will move all my lights to the z2mqtt network and report back if the performance still remains the same.

Switching to Z2Mqtt and the response is faster but i encounteres a problem by dimming groups. Sometimes i just aborts the dimming and stays at 50%. 4s 20 Dimmsteps

With 4s and 10 Dimsteps it works almost flawlessly, way better than the Hue Bridge!

As already mentioned, here comes a big change for the ‘relative dimming’ part of the blueprint. My goal was to be able to stop dimming at any point when you release the dimm button. In the past dimming was always done stepwise without the possibility to stop in between.

I had to change a lot internally and hope that there are no bugs left. Please see the changes as a beta release which has to be tested (by you).
@kalhimeo : I post the code here in this thread. Feel free to upload it to github. I don’t think that I should create an own project for this.

Changes in detail:
In order to be able to stop dimming at any time, you need to use a ‘number helper’ instead of a ‘toggle helper’. Using a toggle helper is still possible (so the new blueprint won’t break your existing automations) but you will not benefit of the new function.

If you have lights that support transition and you use the ‘number helper’, the lights just get triggered once at the beginning and maybe once at the end of the dimming process. This might result in a jumping brightness at the end of the dimming if your lights don’t transition perfectly. E.g.: My lights take ~10% more time for transitioning as they should. So lets say I would like to go from 0% to 100% in 10 seconds. After 9.9 seconds the (internal) brightness of the light will be just at around 90%. When I stop dimming it will instantly be set to 99% and so the brightness jumps.
You can change this behaviour so that your light will also receive an update with every dimm step by activating the new switch ‘Enable HA bus updates with relative dimming and transition’. Either way the KNX bus will receive updates for each dimm step.

In order to keep everything mostly consistent (even without transition and/or a ‘number helper’) there is a change in behaviour when the light does not support transition: Before the change, the dimm process changed the brightness of the light at the beginning of each dimm step. After that there was a small delay and then it started again until the dimm process was stopped. Now the delay comes first and after that the brightness change. So if you have a just few dimm steps combined with a long dimm time (and a toggle helper and no transition), you will notice the delay before the first brightness change.

Minor improvements:
The calculation of the length of each dimm step should be a bit more accurate.
The last dimm step now takes less time if there is no full step left for dimming.

Bug fix:
It was possible to start multiple dimming sequences at once if you managed to stop and start dimming during one ‘dimm step’. This is fixed by replacing the fixed ‘delay’ with a ‘wait_template’.

Internal Aesthetics:
I changed the code to reflect the newest Home Assistant changes (e.g. ‘trigger’ instead of ‘platform’, ‘action’ instead of ‘service’).

Caveats:
I have just tested switching (on and off) and brigthness changes. I have not done any tests for RGB colors or color temperature. So be carefull if you use these features with your lights.
There are now quite a bit more checks and calculations which might result in a higher cpu load and/or longer time needed for the single automation steps. On my hardware (HP T630 Thin Client) I could see delays up to 150ms. This does not seem to be much but could add up when using a lot of dimm steps. On the other hand I never checked how it was before these changes.

(The code will be posted in the next 2 posts because it exceeds the character limit of one post).

Code Part 1:

blueprint:
  name: KNX - Light entities control (new)
  description:
    "Use KNX to control an Home Assistant light entity with optional switching,
    relative dimming, absolute dimming, tunable white, and RGB color telegrams (+
    state feedback)

    **Version**: 2025.01.1

    "
  author: kalhimeo + turboPasqual
  source_url: https://gist.github.com/kalhimeo/88f6e38b45aa872961cc7b748eacef3d
  domain: automation
  input:
    light_entity:
      name: Light Entity
      description: "Choose the light to control and expose with KNX

        "
      selector:
        entity:
          filter:
            domain:
              - light
          multiple: false
    dimm_entity:
      name: Helper for relative dimming function
      description:
        "If you intend to use the relative dimming function, it is necessary
        to manually create a 'toggle helper' or a 'number helper' specificly for this light (don't change
        if unused). A number helper has the advantage that you can stop dimming at any time.
        With a toggle helper the dimming always happens stepwise."
      selector:
        entity:
          filter:
            domain:
              - input_boolean
              - input_number
          multiple: false
      default: []
    dimm_always_send:
      name: Enable HA bus updates with relative dimming and transition
      description:
        If you have lights that support transition and you use a 'number helper' for relative dimming,
        the intermediate dimming steps will be sent to the KNX bus but not to Home Assistant.
        You can enable the intermediate updates of Home Assistant with this switch.
      selector:
        boolean: {}
      default: false
    switch_address:
      name: Switch group address
      description:
        "Group address for switching the lights on and off. DPT 1.001 Example:
        '1/0/1' (leave empty if unused)

        "
      default: ""
    dimm_address:
      name: Relative dimming group address
      description:
        "Group address for relative dimming the lights (+/-). DPT 3.007
        Example: '1/1/1' (leave empty if unused)

        "
      default: ""
    value_address:
      name: Absolute dimming group address
      description:
        "Group address for absolute dimming of the light (0-100%). DPT
        5.001 Example: '1/2/1' (leave empty if unused)

        "
      default: ""
    rgb_color_address:
      name: RGB color group address
      description:
        "Group address for RGB color of the light (r,g,b). DPT 232.600
        Example: '1/3/1' (leave empty if unused)

        "
      default: ""
    temperature_address:
      name: Tunable white temperature group address
      description:
        "Group address for white temperature of the light (0-100%). DPT
        7.600 Example: '1/7/1' (leave empty if unused)

        "
      default: ""
    switch_state_address:
      name: Switch state group address
      description:
        "Group address to send feedback of the state of the light. DPT
        1.001 Example: '1/4/1' (leave empty if unused)

        "
      default: ""
    value_state_address:
      name: Brightness state group address
      description:
        "Group address to send feedback of the brightess of the light.
        DPT 5.001 Example: '1/5/1' (leave empty if unused)

        "
      default: ""
    rgb_color_state_address:
      name: RGB color state group address
      description:
        "Group address to send feedback of the RGB color of the light.
        DPT 232.600 Example: '1/6/1' (leave empty if unused)

        "
      default: ""
    temperature_state_address:
      name: Tunable white temperature state group address
      description:
        "Group address to send feedback of the white temperature of the
        light. DPT 7.600 Example: '1/7/1' (leave empty if unused)

        "
      default: ""
    dimm_time:
      name: Dimm time
      description: Time dimming from 0 to 100% shall take.
      selector:
        number:
          min: 1.0
          max: 20.0
          step: 0.1
          unit_of_measurement: seconds
          mode: slider
      default: 4
    dimm_steps:
      name: Dimm steps
      description: Steps used to dimm from 0 to 100%.
      selector:
        number:
          min: 2.0
          max: 100.0
          step: 1.0
          unit_of_measurement: steps
          mode: slider
      default: 20
    dimm_no_off:
      name: Disable switch off with dimm down command
      description:
        If enabled, the dimm down command won't cause the light to switch
        off when reaching 0% (keep the light at minimum dimm)
      selector:
        boolean: {}
      default: false
    dimm_no_on:
      name: Disable switch on with dimm up command
      description:
        If enabled, the dimm up command won't cause the light to switch
        on (if it's actual state is off)
      selector:
        boolean: {}
      default: false
mode: parallel
max_exceeded: silent
variables:
  light_entity: !input light_entity
  dimm_entity: !input dimm_entity
  dimm_always_send: !input dimm_always_send
  switch_address: !input switch_address
  dimm_address: !input dimm_address
  value_address: !input value_address
  temperature_address: !input temperature_address
  rgb_color_address: !input rgb_color_address
  switch_state_address: !input switch_state_address
  value_state_address: !input value_state_address
  temperature_state_address: !input temperature_state_address
  rgb_color_state_address: !input rgb_color_state_address
  _dimm_time: !input dimm_time
  _dimm_steps: !input dimm_steps
  dimm_no_off: !input dimm_no_off
  dimm_no_on: !input dimm_no_on
  dimm_time: "{{ _dimm_time | float(default=4) }}"
  dimm_steps: "{{ _dimm_steps | int(default=20) }}"
  dimm_step: "{{ (255 / dimm_steps) | round(0,'ceil') }}"
  dimm_delay: "{{ dimm_time * 1000 / 255 * dimm_step }}"
  initial_brightness: "{{ (state_attr(light_entity, 'brightness') | int(default=0)) }}"
  transition_supported: "{{ ((states[light_entity].attributes.supported_features|bitwise_and(32)) == 32) | bool(default=false) }}"
  const_Dimming: 1
  const_DirectionUp: 2
  const_StateOn: 4

triggers:
  - trigger: homeassistant
    event: start
    id: initialize
  - trigger: event
    event_type: automation_reloaded
    id: initialize
  - trigger: event
    event_type: service_registered
    event_data:
      domain: knx
      action: event_register
    id: initialize
  - trigger: state
    entity_id: !input light_entity
    id: light_entity
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input switch_address
      telegramtype: GroupValueWrite
      direction: Incoming
    id: switch_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input dimm_address
      telegramtype: GroupValueWrite
      direction: Incoming
    id: dimm_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input value_address
      telegramtype: GroupValueWrite
      direction: Incoming
    id: value_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input temperature_address
      telegramtype: GroupValueWrite
      direction: Incoming
    id: temperature_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input rgb_color_address
      telegramtype: GroupValueWrite
      direction: Incoming
    id: rgb_color_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input switch_state_address
      telegramtype: GroupValueRead
      direction: Incoming
    id: switch_state_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input value_state_address
      telegramtype: GroupValueRead
      direction: Incoming
    id: value_state_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input temperature_state_address
      telegramtype: GroupValueRead
      direction: Incoming
    id: temperature_state_address
  - trigger: event
    event_type: knx_event
    event_data:
      destination: !input rgb_color_state_address
      telegramtype: GroupValueRead
      direction: Incoming
    id: rgb_color_state_address
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: initialize
        sequence:
          - if:
              - condition: template
                value_template: "{{ switch_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ switch_address }}"
          - if:
              - condition: template
                value_template: "{{ switch_state_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ switch_state_address }}"
          - if:
              - condition: template
                value_template: "{{ dimm_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ dimm_address }}"
          - if:
              - condition: template
                value_template: "{{ value_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ value_address }}"
                  type: percent
          - if:
              - condition: template
                value_template: "{{ value_state_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ value_state_address }}"
                  type: percent
          - if:
              - condition: template
                value_template: "{{ temperature_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ temperature_address }}"
                  type: color_temperature
          - if:
              - condition: template
                value_template: "{{ temperature_state_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ temperature_state_address }}"
                  type: color_temperature
          - if:
              - condition: template
                value_template: "{{ rgb_color_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ rgb_color_address }}"
          - if:
              - condition: template
                value_template: "{{ rgb_color_state_address != '' }}"
            then:
              - action: knx.event_register
                data:
                  address: "{{ rgb_color_state_address }}"
      - conditions:
          condition: template
          value_template:
            "{{ trigger is defined and trigger.platform == 'event' and
            trigger.event.event_type == 'knx_event' and trigger.event.data.direction
            == 'Incoming' and trigger.event.data.destination != '' }}"
        sequence:
          - choose:
              - conditions:
                  - condition: trigger
                    id: switch_address
                  - condition: template
                    value_template: "{{ switch_address != '' }}"
                sequence:
                  - choose:
                      - conditions:
                          - condition: template
                            value_template: "{{ trigger.event.data.data | int(default=0) == 0 }}"
                        sequence:
                          - action: light.turn_off
                            target:
                              entity_id: "{{ light_entity }}"
                      - conditions:
                          - condition: template
                            value_template: "{{ trigger.event.data.data | int(default=0) == 1 }}"
                        sequence:
                          - action: light.turn_on
                            target:
                              entity_id: "{{ light_entity }}"
              - conditions:
                  - condition: trigger
                    id: switch_state_address
                  - condition: template
                    value_template: "{{ switch_state_address != '' }}"
                sequence:
                  - if:
                      - condition: or
                        conditions:
                          - condition: state
                            entity_id: !input light_entity
                            state: "off"
                          - condition: state
                            entity_id: !input light_entity
                            state: unavailable
                          - condition: state
                            entity_id: !input light_entity
                            state: unknown
                    then:
                      - action: knx.send
                        data:
                          address: "{{ switch_state_address }}"
                          payload: 0
                          response: true
                    else:
                      - action: knx.send
                        data:
                          address: "{{ switch_state_address }}"
                          payload: 1
                          response: true
              - conditions:
                  - condition: trigger
                    id: dimm_address
                  - condition: template
                    value_template: "{{ dimm_address != '' }}"
                  - condition: template
                    value_template: "{{ not not dimm_entity }}"
                sequence:
                  - choose:
                      - conditions:
                          - condition: template
                            value_template:
                              "{{ trigger.event.data.data == 0  or trigger.event.data.data
                              == 8 }}"
                        sequence:
                          - if:
                              - condition: template
                                value_template: "{{ states[dimm_entity].domain == 'input_boolean'}}"
                            then:
                              - action: input_boolean.turn_on
                                data: {}
                                target:
                                  entity_id: !input dimm_entity
                            else:
                              - if:
                                  - condition: template
                                    value_template: "{{ (states(dimm_entity) | int | bitwise_and(const_Dimming)) == const_Dimming }}" # Currently Dimming?
                                then:
                                  - variables:
                                      curent_DimmSeconds: >-
                                        {{ now().timestamp() - (states[dimm_entity].last_changed | as_datetime | as_local).timestamp() }}
                                      dimm_Fraction: >-
                                        {{ curent_DimmSeconds * 1000 / dimm_delay }}
                                      current_Brigthness: >-
                                        {% if (states(dimm_entity) | int | bitwise_and(const_DirectionUp)) == const_DirectionUp %}
                                          {{ ((((((((states(dimm_entity) | float(0)) - (states(dimm_entity) | int)) * 1000) + dimm_Fraction * dimm_step) | round(0,'trunc') | int(0)), 255) | min), 0) | max }}
                                        {% else %}
                                          {{ ((((((((states(dimm_entity) | float(0)) - (states(dimm_entity) | int)) * 1000) - dimm_Fraction * dimm_step) | round(0,'trunc') | int(0)), 0) | max), 255) | min }}
                                        {% endif %}
                                  - action: input_number.set_value
                                    metadata: {}
                                    data:
                                      value: "{{ states(dimm_entity) | float - (states(dimm_entity) | int | bitwise_and(const_Dimming)) }}" # Remove Dimming Flag
                                    target:
                                      entity_id: !input dimm_entity
                                  - action: light.turn_on
                                    data:
                                      brightness: "{{ current_Brigthness }}"
                                      transition: 0
                                    target:
                                      entity_id: "{{ light_entity }}"
                      - conditions:
                          - condition: template
                            value_template: "{{ 9 <= trigger.event.data.data <= 15 }}"
                          - condition: template
                            value_template: "{{ not dimm_no_on or states(light_entity) != 'off' }}"
                          - condition: template
                            value_template: "{{ initial_brightness < 255 }}"
                        sequence:
                          - variables:
                              current_dimm_steps: "{{ ((255 - initial_brightness) / 255 * dimm_steps) | round(0, 'ceil') | int }}"
                          - if:
                              - condition: template
                                value_template: "{{ states[dimm_entity].domain == 'input_boolean'}}"
                            then:
                              - action: input_boolean.turn_off
                                data: {}
                                target:
                                  entity_id: !input dimm_entity
                            else:
                              - action: input_number.set_value
                                metadata: {}
                                data:
                                  value: "{{ states(dimm_entity) | int | bitwise_or(const_Dimming) | bitwise_or(const_DirectionUp) + (initial_brightness / 1000) }}" # Add Dimming Flag, add DirectionUp Flag, + Value
                                target:
                                  entity_id: !input dimm_entity
                          - if:
                              - condition: template
                                value_template: "{{ (not dimm_always_send) and transition_supported and (states[dimm_entity].domain == 'input_number') }}"
                            then:
                              - action: light.turn_on
                                data:
                                  brightness: 255
                                  transition: "{{ (255 - initial_brightness) / 255 * dimm_time }}"
                                target:
                                  entity_id: "{{ light_entity }}"
                          - repeat:
                              sequence:
                                - variables:
                                    next_Brightness: "{{ (initial_brightness + (repeat.index * dimm_step), 255) | min }}"
                                    transition_time: >-
                                      {% if repeat.index >= current_dimm_steps %}
                                        {{ (255 - ((initial_brightness + ((repeat.index - 1) * dimm_step), 255) | min)) / dimm_step * dimm_delay }}
                                      {% else %}
                                        {{ dimm_delay }}
                                      {% endif %}
                                - if:
                                    - condition: template
                                      value_template: "{{ transition_supported }}"
                                  then:
                                    - if:
                                        - condition: template
                                          value_template: "{{ dimm_always_send or (states[dimm_entity].domain != 'input_number') }}"
                                      then:
                                        - action: light.turn_on
                                          data:
                                            brightness: "{{ next_Brightness }}"
                                            transition: "{{ transition_time / 1000 }}"
                                          target:
                                            entity_id: "{{ light_entity }}"
                                    - if:
                                        - condition: template
                                          value_template: "{{ repeat.first and (initial_brightness == 0) and (states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_StateOn)) != const_StateOn) }}" # Last sent state was 'off'
                                      then:
                                        - action: knx.send
                                          data:
                                            address: "{{ switch_state_address }}"
                                            payload: 1
                                        - action: input_number.set_value
                                          metadata: {}
                                          data:
                                            value: "{{ states(dimm_entity) | float - (states(dimm_entity) | int) + (states(dimm_entity) | int | bitwise_or(const_StateOn)) }}" # Add StateOn Flag
                                          target:
                                            entity_id: !input dimm_entity

Code Part 2:

                                - wait_template: "{{ ((states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_Dimming)) != const_Dimming)) or ((states[dimm_entity].domain == 'input_boolean') and (states(dimm_entity) == 'on')) }}"
                                  timeout:
                                    milliseconds: "{{ transition_time }}"
                                  continue_on_timeout: true
                                - if:
                                    - condition: template
                                      value_template: "{{ not wait.completed }}"
                                  then:
                                    - if:
                                        - condition: template
                                          value_template: "{{ dimm_always_send or (states[dimm_entity].domain != 'input_number') }}"
                                      then:
                                        - action: input_number.set_value
                                          metadata: {}
                                          data:
                                            value: "{{ states(dimm_entity) | int + (next_Brightness / 1000) }}"
                                          target:
                                            entity_id: !input dimm_entity
                                    - if:
                                        - condition: template
                                          value_template: "{{ transition_supported }}"
                                      then:
                                        - if:
                                            - condition: template
                                              value_template: "{{ value_state_address != '' }}"
                                          then:
                                            - variables:
                                                curent_DimmSeconds: >-
                                                  {{ now().timestamp() - (states[dimm_entity].last_changed | as_datetime | as_local).timestamp() }}
                                                dimm_Fraction: >-
                                                  {{ curent_DimmSeconds * 1000 / dimm_delay }}
                                            - action: knx.send
                                              data:
                                                address: "{{ value_state_address }}"
                                                payload:
                                                  - "{{ ((((((((states(dimm_entity) | float(0)) - (states(dimm_entity) | int)) * 1000) + dimm_Fraction * dimm_step) | round(0,'trunc') | int(0)), 255) | min), 0) | max }}"
                                      else:
                                        - action: light.turn_on
                                          data:
                                            brightness: "{{ next_Brightness }}"
                                          target:
                                            entity_id: "{{ light_entity }}"
                              until:
                                - condition: template
                                  value_template: "{{ (((states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_Dimming)) != const_Dimming))) or ((states[dimm_entity].domain == 'input_boolean') and (states(dimm_entity) == 'on')) or (repeat.index >= current_dimm_steps) }}"
                          - if:
                              - condition: template
                                value_template: "{{ states[dimm_entity].domain == 'input_boolean'}}"
                            then:
                              - action: input_boolean.turn_off
                                data: {}
                                target:
                                  entity_id: !input dimm_entity
                            else:
                              - action: input_number.set_value
                                metadata: {}
                                data:
                                  value: "{{ states(dimm_entity) | float - (states(dimm_entity) | int | bitwise_and(const_Dimming)) }}" # Remove Dimming Flag
                                target:
                                  entity_id: !input dimm_entity
                      - conditions:
                          - condition: template
                            value_template: "{{ 1 <= trigger.event.data.data <= 7 }}"
                          - condition: template
                            value_template: "{{ ((not dimm_no_off) and initial_brightness > 0) or (initial_brightness > 1) }}"
                        sequence:
                          - variables:
                              current_dimm_steps: >-
                                {% if dimm_no_off %}
                                  {{ (([(initial_brightness - 1), 0] | max) / 255 * dimm_steps) | round(0, 'ceil') | int }}
                                {% else %}
                                  {{ (initial_brightness / 255 * dimm_steps) | round(0, 'ceil') | int }}
                                {% endif %}
                          - if:
                              - condition: template
                                value_template: "{{ states[dimm_entity].domain == 'input_boolean'}}"
                            then:
                              - action: input_boolean.turn_off
                                data: {}
                                target:
                                  entity_id: !input dimm_entity
                            else:
                              - action: input_number.set_value
                                metadata: {}
                                data:
                                  value: "{{ states(dimm_entity) | int | bitwise_and(const_StateOn) | bitwise_or(const_Dimming) + (initial_brightness / 1000) }}" # Keep only StateOn Flag, Add Dimming Flag, + Value
                                target:
                                  entity_id: !input dimm_entity
                          - if:
                              - condition: template
                                value_template: "{{ (not dimm_always_send) and transition_supported and (states[dimm_entity].domain == 'input_number') }}"
                            then:
                              - if:
                                  - condition: template
                                    value_template: "{{ not dimm_no_off }}"
                                then:
                                  - action: light.turn_on
                                    data:
                                      brightness: 1
                                      transition: "{{ (initial_brightness - 1) / 255 * dimm_time }}"
                                    target:
                                      entity_id: "{{ light_entity }}"
                                else:
                                  - action: light.turn_on
                                    data:
                                      brightness: 0
                                      transition: "{{ (initial_brightness) / 255 * dimm_time }}"
                                    target:
                                      entity_id: "{{ light_entity }}"
                          - repeat:
                              sequence:
                                - variables:
                                    next_Brightness: >-
                                      {% if dimm_no_off and (initial_brightness - (repeat.index * dimm_step)) < 1 %}
                                        1
                                      {% else %}
                                        {{ (initial_brightness - (repeat.index * dimm_step), 0) | max }}
                                      {% endif %}
                                    transition_time: >-
                                      {% if repeat.index >= current_dimm_steps %}
                                        {{ (((initial_brightness - ((repeat.index - 1) * dimm_step), 0) | max)) / dimm_step * dimm_delay }}
                                      {% else %}
                                        {{ dimm_delay }}
                                      {% endif %}
                                - if:
                                    - condition: template
                                      value_template: "{{ transition_supported }}"
                                  then:
                                    - if:
                                        - condition: template
                                          value_template: "{{ dimm_always_send or (states[dimm_entity].domain != 'input_number') }}"
                                      then:
                                        - action: light.turn_on
                                          data:
                                            brightness: "{{ next_Brightness }}"
                                            transition: "{{ transition_time / 1000 }}"
                                          target:
                                            entity_id: "{{ light_entity }}"
                                - wait_template: "{{ ((states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_Dimming)) != const_Dimming)) or ((states[dimm_entity].domain == 'input_boolean') and (states(dimm_entity) == 'on')) }}"
                                  timeout:
                                    milliseconds: "{{ transition_time }}"
                                  continue_on_timeout: true
                                - if:
                                    - condition: template
                                      value_template: "{{ not wait.completed }}"
                                  then:
                                    - if:
                                        - condition: template
                                          value_template: "{{ dimm_always_send or (states[dimm_entity].domain != 'input_number') }}"
                                      then:
                                        - action: input_number.set_value
                                          metadata: {}
                                          data:
                                            value: "{{ states(dimm_entity) | int + (next_Brightness / 1000) }}"
                                          target:
                                            entity_id: !input dimm_entity
                                    - if:
                                        - condition: template
                                          value_template: "{{ transition_supported }}"
                                      then:
                                        - if:
                                            - condition: template
                                              value_template: "{{ value_state_address != '' }}"
                                          then:
                                            - variables:
                                                curent_DimmSeconds: >-
                                                  {{ now().timestamp() - (states[dimm_entity].last_changed | as_datetime | as_local).timestamp() }}
                                                dimm_Fraction: >-
                                                  {{ curent_DimmSeconds * 1000 / dimm_delay }}
                                            - action: knx.send
                                              data:
                                                address: "{{ value_state_address }}"
                                                payload:
                                                  - "{{ ((((((((states(dimm_entity) | float(0)) - (states(dimm_entity) | int)) * 1000) - dimm_Fraction * dimm_step) | round(0,'trunc') | int(0)), 0) | max), 255) | min }}"
                                      else:
                                        - action: light.turn_on
                                          data:
                                            brightness: "{{ next_Brightness }}"
                                          target:
                                            entity_id: "{{ light_entity }}"
                              until:
                                - condition: template
                                  value_template: "{{ (((states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_Dimming)) != const_Dimming))) or ((states[dimm_entity].domain == 'input_boolean') and (states(dimm_entity) == 'on')) or (repeat.index >= current_dimm_steps) }}"
                          - if:
                              - condition: template
                                value_template: "{{ states[dimm_entity].domain == 'input_boolean'}}"
                            then:
                              - action: input_boolean.turn_off
                                data: {}
                                target:
                                  entity_id: !input dimm_entity
                            else:
                              - action: input_number.set_value
                                metadata: {}
                                data:
                                  value: "{{ states(dimm_entity) | float - (states(dimm_entity) | int | bitwise_and(const_Dimming)) }}" # Remove Dimming Flag
                                target:
                                  entity_id: !input dimm_entity
              - conditions:
                  - condition: trigger
                    id: value_address
                  - condition: template
                    value_template: "{{ value_address != '' }}"
                sequence:
                  - choose:
                      - conditions:
                          - condition: template
                            value_template:
                              "{{ trigger.event.data.data[0] | int(default=0) == 0
                              }}"
                        sequence:
                          - action: light.turn_off
                            target:
                              entity_id: "{{ light_entity }}"
                      - conditions:
                          - condition: template
                            value_template:
                              "{{ trigger.event.data.data[0] | int(default=0) != 0
                              }}"
                        sequence:
                          - action: light.turn_on
                            data:
                              brightness: "{{ trigger.event.data.data[0] | int(default=255) }}"
                            target:
                              entity_id: "{{ light_entity }}"
              - conditions:
                  - condition: trigger
                    id: value_state_address
                  - condition: template
                    value_template: "{{ value_state_address != '' }}"
                sequence:
                  - action: knx.send
                    data:
                      address: "{{ value_state_address }}"
                      payload:
                        - "{{ state_attr(light_entity, 'brightness') | int(default=0) }}"
                      response: true
              - conditions:
                  - condition: trigger
                    id: temperature_address
                  - condition: template
                    value_template: "{{ temperature_address != '' }}"
                sequence:
                  - choose:
                      - conditions:
                          - condition: template
                            value_template:
                              "{{ trigger.event.data.value | int(default=3000) !=
                              0 }}"
                        sequence:
                          - action: light.turn_on
                            target:
                              entity_id: "{{ light_entity }}"
                            data:
                              kelvin: "{{ trigger.event.data.value | int(default=3000) }}"
              - conditions:
                  - condition: trigger
                    id: temperature_state_address
                  - condition: template
                    value_template: "{{ temperature_state_address != '' }}"
                sequence:
                  - action: knx.send
                    data:
                      address: "{{ temperature_state_address }}"
                      type: color_temperature
                      payload:
                        "{{ state_attr(light_entity, 'color_temp_kelvin') | int(default=0)
                        }}"
                      response: true
              - conditions:
                  - condition: trigger
                    id: rgb_color_address
                  - condition: template
                    value_template: "{{ rgb_color_address != '' }}"
                sequence:
                  - if:
                      - condition: template
                        value_template:
                          "{{ trigger.event.data.data[0] | int(default=0) == 0 and
                          trigger.event.data.data[1] | int(default=0) == 0 and trigger.event.data.data[2]
                          | int(default=0) == 0 }}"
                    then:
                      - action: light.turn_off
                        target:
                          entity_id: "{{ light_entity }}"
                    else:
                      - variables:
                          brightness:
                            "{{ ((trigger.event.data.data[0] * 0.299) + (trigger.event.data.data[1]
                            * 0.587) + (trigger.event.data.data[2] * 0.114)) | int }}"
                      - action: light.turn_on
                        target:
                          entity_id: "{{ light_entity }}"
                        data:
                          brightness: "{{ brightness }}"
                          rgb_color:
                            - "{{ trigger.event.data.data[0] | int(default=0) }}"
                            - "{{ trigger.event.data.data[1] | int(default=0) }}"
                            - "{{ trigger.event.data.data[2] | int(default=0) }}"
              - conditions:
                  - condition: trigger
                    id: rgb_color_state_address
                  - condition: template
                    value_template: "{{ rgb_color_state_address != '' }}"
                sequence:
                  - if:
                      - condition: template
                        value_template:
                          "{{ state_attr(light_entity, 'rgb_color')[0] is defined
                          and state_attr(light_entity, 'rgb_color')[1] is defined and state_attr(light_entity,
                          'rgb_color')[2] is defined }}"
                    then:
                      - action: knx.send
                        data:
                          address: "{{ rgb_color_state_address }}"
                          payload:
                            - "{{ state_attr(light_entity, 'rgb_color')[0] | int(default=0) }}"
                            - "{{ state_attr(light_entity, 'rgb_color')[1] | int(default=0) }}"
                            - "{{ state_attr(light_entity, 'rgb_color')[2] | int(default=0) }}"
                          response: true
                    else:
                      - action: knx.send
                        data:
                          address: "{{ rgb_color_state_address }}"
                          payload:
                            - 0
                            - 0
                            - 0
                          response: true
      - conditions:
          - condition: trigger
            id: light_entity
          - condition: template
            value_template: "{{ light_entity != '' }}"
        sequence:
          - if:
              - condition: template
                value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
              - condition: template
                value_template: "{{ switch_state_address != '' }}"
              - condition: template
                value_template: "{{ not (transition_supported and (states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_Dimming)) == const_Dimming)) }}"
            then:
              - choose:
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.to_state.state == 'off' }}"
                    sequence:
                      - if:
                          - condition: template
                            value_template: "{{ states[dimm_entity].domain == 'input_number' }}"
                        then:
                          - if:
                              - condition: template
                                value_template: "{{ (states(dimm_entity) | int | bitwise_and(const_StateOn)) == const_StateOn }}" # Last sent state was 'on'
                            then:
                              - action: knx.send
                                data:
                                  address: "{{ switch_state_address }}"
                                  payload: 0
                              - action: input_number.set_value
                                metadata: {}
                                data:
                                  value: "{{ states(dimm_entity) | float - (states(dimm_entity) | int | bitwise_and(const_StateOn)) }}" # Remove StateOn Flag
                                target:
                                  entity_id: !input dimm_entity
                        else:
                          - action: knx.send
                            data:
                              address: "{{ switch_state_address }}"
                              payload: 0
                  - conditions:
                      condition: template
                      value_template: "{{ trigger.to_state.state == 'on' }}"
                    sequence:
                      - if:
                          - condition: template
                            value_template: "{{ states[dimm_entity].domain == 'input_number' }}"
                        then:
                          - if:
                              - condition: template
                                value_template: "{{ (states(dimm_entity) | int | bitwise_and(const_StateOn)) != const_StateOn }}" # Last sent state was 'off'
                            then:
                              - action: knx.send
                                data:
                                  address: "{{ switch_state_address }}"
                                  payload: 1
                              - action: input_number.set_value
                                metadata: {}
                                data:
                                  value: "{{ states(dimm_entity) | float - (states(dimm_entity) | int) + (states(dimm_entity) | int | bitwise_or(const_StateOn)) }}" # Add StateOn Flag
                                target:
                                  entity_id: !input dimm_entity
                        else:
                          - action: knx.send
                            data:
                              address: "{{ switch_state_address }}"
                              payload: 1
          - if:
              - condition: template
                value_template: "{{ value_state_address != '' }}"
              - condition: template
                value_template: "{{ not (transition_supported and (states[dimm_entity].domain == 'input_number') and ((states(dimm_entity) | int | bitwise_and(const_Dimming)) == const_Dimming)) }}"
              - condition: or
                conditions:
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.brightness is defined and
                      trigger.from_state.attributes.brightness is defined and trigger.to_state.attributes.brightness
                      != trigger.from_state.attributes.brightness }}"
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.brightness is defined and
                      trigger.from_state.attributes.brightness is not defined }}"
                  - condition: template
                    value_template:
                      "{{ trigger.from_state.attributes.brightness is defined
                      and trigger.to_state.attributes.brightness is not defined }}"
            then:
              - if:
                  - condition: template
                    value_template: "{{ trigger.to_state.attributes.brightness is defined }}"
                then:
                  - action: knx.send
                    data:
                      address: "{{ value_state_address }}"
                      payload:
                        - "{{ trigger.to_state.attributes.brightness | int(default=0) }}"
                else:
                  - action: knx.send
                    data:
                      address: "{{ value_state_address }}"
                      payload:
                        - "0"
          - if:
              - condition: template
                value_template: "{{ temperature_state_address != '' }}"
              - condition: or
                conditions:
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.color_temp_kelvin is defined
                      and trigger.from_state.attributes.color_temp_kelvin is defined and trigger.to_state.attributes.color_temp_kelvin
                      != trigger.from_state.attributes.color_temp_kelvin }}"
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.color_temp_kelvin is defined
                      and trigger.from_state.attributes.color_temp_kelvin is not defined }}"
                  - condition: template
                    value_template:
                      "{{ trigger.from_state.attributes.color_temp_kelvin is defined
                      and trigger.to_state.attributes.color_temp_kelvin is not defined }}"
            then:
              - if:
                  - condition: template
                    value_template: "{{ trigger.to_state.attributes.brightness is defined }}"
                then:
                  - action: knx.send
                    data:
                      address: "{{ temperature_state_address }}"
                      type: color_temperature
                      payload:
                        "{{ trigger.to_state.attributes.color_temp_kelvin | int(default=0)
                        }}"
                else:
                  - action: knx.send
                    data:
                      address: "{{ temperature_state_address }}"
                      type: color_temperature
                      payload: "0"
          - if:
              - condition: template
                value_template: "{{ rgb_color_state_address != '' }}"
              - condition: or
                conditions:
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.rgb_color is defined and
                      trigger.from_state.attributes.rgb_color is defined and trigger.to_state.attributes.rgb_color
                      != trigger.from_state.attributes.rgb_color }}"
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.rgb_color is defined and
                      trigger.from_state.attributes.rgb_color is not defined }}"
                  - condition: template
                    value_template:
                      "{{ trigger.from_state.attributes.rgb_color is defined and
                      trigger.to_state.attributes.rgb_color is not defined }}"
            then:
              - if:
                  - condition: template
                    value_template:
                      "{{ trigger.to_state.attributes.rgb_color[0] is defined
                      and trigger.to_state.attributes.rgb_color[1] is defined and trigger.to_state.attributes.rgb_color[2]
                      is defined }}"
                then:
                  - action: knx.send
                    data:
                      address: "{{ rgb_color_state_address }}"
                      payload:
                        - "{{ trigger.to_state.attributes.rgb_color[0] | int(default=0) }}"
                        - "{{ trigger.to_state.attributes.rgb_color[1] | int(default=0) }}"
                        - "{{ trigger.to_state.attributes.rgb_color[2] | int(default=0) }}"
                else:
                  - action: knx.send
                    data:
                      address: "{{ rgb_color_state_address }}"
                      payload:
                        - 0
                        - 0
                        - 0

2 Likes

Hi turboPasqual,

Thank you very much for sharing your updates. Unfortunately I don’t really have time to test and dig into the changes at the moment, but I created a new “secret git” as BETA to make it easier for other users to test the code : knx-light-entities-control.yaml with turboPasqual updates · GitHub

Anyone is welcomed to report feedback here for validation :slight_smile:

can’t get it working. Not sure if something went wrong when copy pasting?!
@turboPasqual could you check?

@chino-lu : The code looks fine as far as i see. What errors do you get?