Skip to content

Instantly share code, notes, and snippets.

@niro1987
Last active August 21, 2024 21:07
Show Gist options
  • Save niro1987/4e49117075cd4b66975e0ecfe3ad72b5 to your computer and use it in GitHub Desktop.
Save niro1987/4e49117075cd4b66975e0ecfe3ad72b5 to your computer and use it in GitHub Desktop.
Home Assistant - Blueprint - ZHA - IKEA TRADFRI - 2 Button Remote - OnOff and Brightness
---
# This automation simulates the use of the IKEA TRADFRI Dimmer control
# connected through ZHA.
# | Button | Action |
# | ----------- | ------------------- |
# | on (short) | Turn the light on |
# | off (short) | Turn the light off |
# | on (long) | Increase brightness |
# | off (long) | Decrease brightness |
blueprint:
source_url: https://github.com/niro1987/homeassistant-config/blob/main/blueprints/automation/niro1987/zha_ikea_tradfri_2button_remote_brightness.yaml
name: ZHA - IKEA TRADFRI - 2 Button Remote - OnOff and Brightness
description: >-
This automation simulates the use of the IKEA TRADFRI on/off switch
connected through ZHA.
domain: automation
input:
remote:
name: IKEA TRADFRI remote control
description: Select the remote control you wish to use.
selector:
device:
integration: zha
manufacturer: IKEA of Sweden
model: TRADFRI on/off switch
light:
name: Light
description: Select the light entity you wish to control.
selector:
entity:
domain: light
mode: restart
max_exceeded: silent
trigger:
- platform: event
event_type: zha_event
event_data:
device_id: !input remote
action:
- choose:
# Short-Press the on button
- conditions:
- condition: template
value_template: '{{ trigger.event.data.command == "on" }}'
sequence:
- service: light.turn_on
target:
entity_id: !input light
data:
brightness: 254
hs_color:
- 38.222
- 52.941
transition: 1
# Long-Press the on button
- conditions:
- condition: template
value_template: '{{ trigger.event.data.command == "move_with_on_off" }}'
sequence:
- repeat:
while: []
sequence:
- service: light.turn_on
target:
entity_id: !input light
data:
brightness_step_pct: 10
transition: 0.5
- delay:
milliseconds: 500
# Short-Press the off button
- conditions:
- condition: template
value_template: '{{ trigger.event.data.command == "off" }}'
sequence:
- service: light.turn_off
target:
entity_id: !input light
data:
transition: 1
# Long-Press the off button
- conditions:
- condition: template
value_template: '{{ trigger.event.data.command == "move" }}'
sequence:
- repeat:
while: []
sequence:
- service: light.turn_on
target:
entity_id: !input light
data:
brightness_step_pct: -10
transition: 0.5
- delay:
milliseconds: 500
# Any other event will cancel the repeat loops (i.e. releasing the [on|off] button)
default: []
@cylonr
Copy link

cylonr commented Feb 6, 2021

Hi :)
Thansk 4 your work :)
It´s possible to when i turn on the light, it will keep the last dimm %?
thanks

@leocaseiro
Copy link

Hey @cylonr you can remove line 63, and should work as you want.

@al13nus
Copy link

al13nus commented Oct 21, 2022

Nice automation. Do you have any good examples how to add motion sensor to it which is not in conflict with remote? I mean, remote always overwrites motion trigger and don't shut off the light if it was manually turned on.

@niro1987
Copy link
Author

niro1987 commented Oct 21, 2022

@al13nus You could create a helper input_boolean that you turn on/off together with the light, but only if the remote is the trigger. If the motion sensor is the trigger, you add a condition checking that the helper is off, and only turn the light off if the helper is off. That way, if the helper is on, the motion sensor will not turn the light off.

It does mean that the 'overwrite' is only valid for a turned on light, if the light is off, it will always be turned on by the motion sensor.

@al13nus
Copy link

al13nus commented Oct 24, 2022

Good idea, will try it out. Thanks!

@al13nus
Copy link

al13nus commented Nov 12, 2022

I combined your 2 button script and motion sensor and getting funny behaviour: so if he tells every time automation is triggered without buttons, that Error: In 'template' condition: UndefinedError: 'dict object' has no attribute 'event'. BUT if you click on a button, then he finds event data and everything is fine. At the end everything works anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment