deCONZ Ikea Trådfri Shortcut Button for RGB Lights

Simple little automation to allow the ShortCut Button from Ikea :sweden: (through deCONZ) take control of RGB :rainbow: (well HSV) light(s). The automation allows for 4 different types of interactions, based on the button press (short/long) and the (first) lights state as shown below:

  • Light is Off
    • short press: light turns on
    • long (hold) press: slowly (~4sec) raise the brightness of the light until 100%
  • Light is On
    • short press: light turns off
    • long (hold) press: cycle through the Hue (of HSV) of the light. (~10.8sec for full cycle)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Multiple lights are configurable, but the first one is where the current brightness / hue value is taken from. All the lights need to support the ‘hs’ colour mode (I’m using LIFX lights).

Based on the Tradfri on/off button,

# Copyright (c) 2022,  Matthew Tilney
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.

# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

blueprint:
  name: DeconZ Ikea Button RGB Light
  description: > 
    Interface an Ikea Zigbee Button switch via DeconZ to provide simple control of light(s) with RGB
    Lightoff:
        - short press = turn off
        - long (hold) = cycle through the Hue (of HSV) of the light. (~10.8sec for full cycle)
    Lighton:
        - shor press = turn on
        - long (hold) = slowly raise the brightness up (until the value you desire or 100%)
  domain: automation
  input:
    remote:
      name: Remote
      description: The Ikea Button remote control from Deconz
      selector:
        device:
          integration: deconz
          manufacturer: IKEA of Sweden
          model: TRADFRI SHORTCUT Button
    light:
      name: Light
      description: The RGB light(s) that will be controlled
      selector:
        target:
          entity:
            domain: light
mode: restart
trigger:
- platform: event
  event_type: deconz_event
  event_data:
    device_id: !input 'remote'
variables:
  lights: !input 'light'
action:
- variables:
    event: '{{ trigger.event.data.event }}'
# if on: short press=off, long press cycle HS colour
# if off: short press=on, long press on* increase brightness
- service: system_log.write
  data:
    level: warning
    message: '
    {{lights}}
    {{lights.entity_id[0]}}
    That was the Lights (up)
    #######################
    Current Colour: {% if is_state(lights.entity_id[0],"on")%}{{(state_attr(lights.entity_id[0],"hs_color")[0] + 10) % 360}}{% endif %}
    #######################
    '
    logger: scripts.log
- if:
# for version 1: only checking the first light in the list, assumged (for initial setup, to only be a single light)
  - condition: template
    value_template: >
      {{is_state(lights.entity_id[0],'off')}}
  then:
  - choose:
    - conditions:
      - '{{ event == 1002 }}'
      sequence:
      - service: light.turn_on
        target: !input 'light'
        data:
          transition: 1
    - conditions:
      - '{{ event == 1001 }}'
      sequence:
        repeat:
          while:
          - condition: template
            value_template: '{{ repeat.index < 10 }}'
          sequence:
          - service: light.turn_on
            target: !input 'light'
            data:
              transition: 1
              brightness_step_pct: 10
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 297
    - conditions:
      - '{{ event == 1003 }}'
      sequence:
      - service: light.turn_on
        target: !input 'light'
  else:
  - choose:
    - conditions:
      - '{{ event == 1002 }}'
      sequence:
      - service: light.turn_off
        target: !input 'light'
        data:
          transition: 1
    - conditions:
      - '{{ event == 1001 }}'
      sequence:
        repeat:
          while:
          - condition: template
            value_template: '{{ is_state(lights.entity_id[0],"on") }}'
          sequence:
          - service: light.turn_on
            target: !input 'light'
            data:
              hs_color:
              - '{{  (state_attr(lights.entity_id[0],"hs_color")[0] + 10) % 360}}'
              - 100
              transition: 0.28
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 297

    - conditions:
      - '{{ event == 1003 }}'
      sequence:
      - service: light.turn_on
        target: !input 'light'