Greenpower zigbee dimmer from Niko (friends of Hue)

Hello the world,

I’m proud of what I did even if it is still an alpha version of the script.
Ultimately, I’d like to convert it into a blueprint but that will be for later I guess.
I’m using a Niko dimmer switch for Hue system. It is Zigbee switch but no power is required, it is generating the necessary electricity from the movement of the button when pressed.

image

For this to work, I’ve created 8 template input_boolean like so:
Short press on a button

- binary_sensor:
    name: button1_short_press
    state: true
    auto_off: 1
  trigger:
    platform: event
    event_type: hue_event
    event_data:
      device_id: d0909d23bcb033625890ad126fc18d61 # <-- from listening to hue_event
      unique_id: 08b66a92-d988-4abb-adc3-41a4a78b5ea6 # <-- from listening to hue_event
      type: short_release # <-- from listening to hue_event

Long press on a button

- binary_sensor:
    name: button1_long_press
    state: true
    auto_off: 1
  trigger:
    platform: event
    event_type: hue_event
    event_data:
      device_id: d0909d23bcb033625890ad126fc18d61 # <-- from listening to hue_event
      unique_id: 08b66a92-d988-4abb-adc3-41a4a78b5ea6 # <-- from listening to hue_event
      type: repeat # <-- from listening to hue_event

And you repeat that for the other 3 buttons, changing the unique_id of the above.

Then, my automation: in theory (1 button, to repeat for the other 3)

alias: Niko dimmer switch
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.button1_short_press
    to: "on"
    id: bouton1_short
  - platform: state
    entity_id:
      - binary_sensor.button1_long_press
    to: "on"
    id: bouton1_long
condition: []
action:
  - if:
      - condition: trigger
        id: bouton1_court
    then:
      - if:
          - condition: state
            entity_id: binary_sensor.button1_long_press
            state: "off"
        then:
          - ... (here you put what a short button1 clic should do
  - if:
      - condition: trigger
        id: bouton1_long
    then:
      - repeat:
          sequence:
            - ... (here you put what a long button1 clic should do
          until:
            - condition: or
              conditions:
                - condition: state
                  entity_id: binary_sensor.button1_short_press
                  state: "on"
                - condition: state
                  entity_id: binary_sensor.button1_long_press
                  state: "off"
mode: parallel
max: 10

And, practically, here is a full version, (in French, sorry) of my final result:

alias: Niko dimmer switch
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.button1_short_press
    to: "on"
    id: bouton1_court
    alias: Bouton 1 court
  - platform: state
    entity_id:
      - binary_sensor.button1_long_press
    to: "on"
    id: bouton1_long
    alias: Bouton 1 long
  - platform: state
    entity_id:
      - binary_sensor.button2_short_press
    to: "on"
    id: bouton2_court
    alias: Bouton 2 court
  - platform: state
    entity_id:
      - binary_sensor.button2_long_press
    to: "on"
    id: bouton2_long
    alias: bouton 2 long
  - platform: state
    entity_id:
      - binary_sensor.button3_short_press
    to: "on"
    id: bouton3_court
    alias: Bouton 3 court
  - platform: state
    entity_id:
      - binary_sensor.button3_long_press
    to: "on"
    id: bouton3_long
    alias: bouton 3 long
  - platform: state
    entity_id:
      - binary_sensor.button4_short_press
    to: "on"
    id: bouton4_court
    alias: Bouton 4 court
  - platform: state
    entity_id:
      - binary_sensor.button4_long_press
    to: "on"
    id: bouton4_long
    alias: Bouton 4 long
condition: []
action:
  - if:
      - condition: trigger
        id: bouton1_court
    then:
      - if:
          - condition: state
            entity_id: binary_sensor.button1_long_press
            state: "off"
        then:
          - service: light.turn_on
            data:
              brightness: 254
            target:
              entity_id: light.chambre_enfants
    alias: "Bouton 1 court : Allumer"
  - if:
      - condition: trigger
        id: bouton3_court
    then:
      - if:
          - condition: state
            entity_id: binary_sensor.button3_long_press
            state: "off"
        then:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.chambre_enfants
    alias: "Bouton 3 court : Eteindre"
  - if:
      - condition: trigger
        id: bouton1_long
    then:
      - repeat:
          sequence:
            - service: light.turn_on
              data:
                brightness: >
                  {% set new_brightness =
                  int(state_attr('light.chambre_enfants','brightness'),0) + 5 %}
                  {% if new_brightness >= 250 %}
                     254
                  {% else %}
                    {{ new_brightness }}
                  {% endif %}
              target:
                entity_id: light.chambre_enfants
            - delay:
                milliseconds: 100
          until:
            - condition: or
              conditions:
                - condition: state
                  entity_id: binary_sensor.button1_short_press
                  state: "on"
                - condition: state
                  entity_id: binary_sensor.button1_long_press
                  state: "off"
    alias: "Bouton 1 long : Luminosité ++"
  - if:
      - condition: trigger
        id: bouton3_long
    then:
      - repeat:
          sequence:
            - if:
                - condition: template
                  value_template: >-
                    {{ int(state_attr('light.chambre_enfants','brightness'),0)
                    <= 5 }}
              then:
                - service: light.turn_off
                  data: {}
                  target:
                    entity_id: light.chambre_enfants
              else:
                - service: light.turn_on
                  data:
                    brightness: >
                      {% set new_brightness =
                      int(state_attr('light.chambre_enfants','brightness'),0) -
                      5 %} {% if new_brightness < 0 %}
                         0
                      {% else %}
                        {{ new_brightness }}
                      {% endif %}
                  target:
                    entity_id: light.chambre_enfants
            - delay:
                milliseconds: 100
          until:
            - condition: or
              conditions:
                - condition: state
                  entity_id: binary_sensor.button3_short_press
                  state: "on"
                - condition: state
                  entity_id: binary_sensor.button3_long_press
                  state: "off"
    alias: "Bouton 3 long : Luminosité --"
  - if:
      - condition: trigger
        id: bouton2_court
    then:
      - if:
          - and:
              - condition: state
                entity_id: binary_sensor.button2_long_press
                state: "off"
              - condition: state
                entity_id: light.chambre_enfants
                state: "on"
        then:
          - service: light.turn_on
            data:
              color_temp_kelvin: >
                {% if state_attr('light.chambre_enfants','color_temp_kelvin') |
                int == 4000 %}
                  2202
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 2202 %}
                  2702
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 2702 %}
                  3003
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 3003 %}
                  3508
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 3508 %}
                  4000
                {% endif %}
            target:
              entity_id: light.chambre_enfants
    alias: "Bouton 2 court : Couleur++"
  - if:
      - condition: trigger
        id: bouton4_court
    then:
      - if:
          - and:
              - condition: state
                entity_id: binary_sensor.button4_long_press
                state: "off"
              - condition: state
                entity_id: light.chambre_enfants
                state: "on"
        then:
          - service: light.turn_on
            data:
              color_temp_kelvin: >
                {% if state_attr('light.chambre_enfants','color_temp_kelvin') |
                int == 4000 %}
                  3508
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 3508 %}
                  3003
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 3003 %}
                  2702
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 2702 %}
                  2202
                {% elif state_attr('light.chambre_enfants','color_temp_kelvin')
                | int == 2202 %}
                  4000
                {% endif %}
            target:
              entity_id: light.chambre_enfants
    alias: "Bouton 4 court : Couleur--"
  - if:
      - condition: trigger
        id: bouton2_long
    then:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.sleep_time
      - service: input_boolean.toggle
        data: {}
        target:
          entity_id: input_boolean.wakeup_time
      - repeat:
          sequence: []
          until:
            - condition: state
              entity_id: binary_sensor.button2_long_press
              state: "off"
    alias: "Bouton 2 long : Procédure de réveil"
  - if:
      - condition: trigger
        id: bouton4_long
    then:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.wakeup_time
      - service: input_boolean.toggle
        data: {}
        target:
          entity_id: input_boolean.sleep_time
      - repeat:
          sequence: []
          until:
            - condition: state
              entity_id: binary_sensor.button4_long_press
              state: "off"
    alias: "Bouton 4 long : Procédure de coucher"
mode: parallel
max: 10