Skip to content

Instantly share code, notes, and snippets.

@niro1987
Last active November 30, 2023 18:51
Show Gist options
  • Save niro1987/f6e84c27b304f0bf3be16a8f439e8efd to your computer and use it in GitHub Desktop.
Save niro1987/f6e84c27b304f0bf3be16a8f439e8efd to your computer and use it in GitHub Desktop.
Home Assistant - Blueprint - Zigbee2MQTT - IKEA TRADFRI - 5 Button Remote - Warm White Lights
---
# This automation simulates the use of the IKEA TRADFRI Remote control
# connected through Zigbee2MQTT.
# | Button | Action |
# | -------- | -------------------- |
# | Power | Toggle the light |
# | Dim-Up | Increase brightness |
# | Dim-Down | Decrease brightness |
# | Right | Increase temperature |
# | Left | Decrease temperature |
blueprint:
source_url: https://gist.github.com/niro1987/f6e84c27b304f0bf3be16a8f439e8efd
name: Zigbee2MQTT - IKEA TRADFRI - 5 Button Remote - Warm White Lights
description: >-
This automation simulates the use of the IKEA TRADFRI Remote control
connected through Zigbee2MQTT.
domain: automation
input:
remote_entity:
name: Remote Sensor Entity
description: The sensor entity created by Zigbee2MQTT
selector:
entity:
domain: sensor
light_entity:
name: Light
description: The light entity to control.
selector:
target:
entity:
domain: light
mode: restart
variables:
var_light_entities: !input light_entity
trigger:
- platform: state
entity_id: !input remote_entity
to:
- "toggle"
- "toggle_hold"
- "brightness_up_click"
- "brightness_down_click"
- "arrow_left_click"
- "arrow_right_click"
- "brightness_up_hold"
- "brightness_down_hold"
- "arrow_left_hold"
- "arrow_right_hold"
- "brightness_up_release"
- "brightness_down_release"
- "arrow_left_release"
- "arrow_right_release"
action:
- choose:
# Short-Press on the power button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "toggle" }}'
sequence:
- service: light.toggle
target: !input light_entity
# Short-Press on the dim-up button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "brightness_up_click" }}'
sequence:
- service: light.turn_on
target: !input light_entity
data:
brightness_step_pct: 20
transition: 0.5
# Short-Press on the dim-down button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "brightness_down_click" }}'
sequence:
- service: light.turn_on
target: !input light_entity
data:
brightness_step_pct: -20
transition: 0.5
# Short-Press on the color-up button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "arrow_left_click" }}'
sequence:
- service: light.turn_on
target: !input light_entity
data:
color_temp: >-
{% if state_attr(var_light_entities.entity_id[0], "color_temp") - 18 < 153 %}
{{ 153 }}
{% else %}
{{ state_attr(var_light_entities.entity_id[0], "color_temp") - 18 }}
{% endif %}
transition: 0.5
# Short-Press on the color-down button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "arrow_right_click" }}'
sequence:
- service: light.turn_on
target: !input light_entity
data:
color_temp: >-
{% if state_attr(var_light_entities.entity_id[0], "color_temp") + 18 > 500 %}
{{ 500 }}
{% else %}
{{ state_attr(var_light_entities.entity_id[0], "color_temp") + 18 }}
{% endif %}
transition: 0.5
# Long-Press on the power button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "toggle_hold" }}'
sequence:
- service: light.turn_on
target: !input light_entity
data:
brightness: 254
color_temp: 400
# Long-Press on the dim-up button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "brightness_up_hold" }}'
sequence:
- repeat:
while: []
sequence:
- service: light.turn_on
target: !input light_entity
data:
brightness_step_pct: 10
transition: 0.5
- delay:
milliseconds: 500
# Long-Press on the dim-down button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "brightness_down_hold" }}'
sequence:
- repeat:
while: []
sequence:
- service: light.turn_on
target: !input light_entity
data:
brightness_step_pct: -10
transition: 0.5
- delay:
milliseconds: 500
# Long-Press on the color-up button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "arrow_left_hold" }}'
sequence:
- repeat:
while: []
sequence:
- service: light.turn_on
target: !input light_entity
data:
color_temp: >-
{% if state_attr(var_light_entities.entity_id[0], "color_temp") - 18 < 153 %}
{{ 153 }}
{% else %}
{{ state_attr(var_light_entities.entity_id[0], "color_temp") - 18 }}
{% endif %}
transition: 0.5
- delay:
milliseconds: 500
# Long-Press on the color-down button.
- conditions:
- condition: template
value_template: '{{ trigger.to_state.state == "arrow_right_hold" }}'
sequence:
- repeat:
while: []
sequence:
- service: light.turn_on
target: !input light_entity
data:
color_temp: >-
{% if state_attr(var_light_entities.entity_id[0], "color_temp") + 18 > 500 %}
{{ 500 }}
{% else %}
{{ state_attr(var_light_entities.entity_id[0], "color_temp") + 18 }}
{% endif %}
transition: 0.5
- delay:
milliseconds: 500
# Any other event will cancel the repeat loops.
default: []
@dbara
Copy link

dbara commented Apr 28, 2021

This is the corresponding YAML:

min_mireds: 250
max_mireds: 454
effect_list:
  - blink
  - breathe
  - okay
  - channel_change
  - finish_effect
  - stop_effect
 
supported_color_modes:
  - color_temp

brightness: 3
color_mode: color_temp
color_temp: 358
linkquality: 173
state: 'OFF'
update:
  state: idle
update_available: false
friendly_name: TestLampe
supported_features: 47

@dbara
Copy link

dbara commented Apr 28, 2021

It's the IKEA E27 1000lm LED (z2m: LED1732G11)

@dbara
Copy link

dbara commented Apr 28, 2021

Oh, btw. is there a way to select multiple remotes and/or multiple lights?
Let's say two remotes control four lights.

@niro1987
Copy link
Author

This would suggest that it does support color_temp. Can you check the logbook to see if the mqtt sensor entity is being changed to the required states please.

State Action
arrow_left_click Short Press Left
arrow_right_click Short Press Right
arrow_left_hold Long Press Left
arrow_right_hold Long Press Right

Multiple lights and/or remotes:
That would become tricky because changing the brightness for example increases or decreases the brightness by 10 percent. To change the color_temp the blueprint reads the current value and increases or decreases it by a fixed amount, it can't read from multiple entities.

@dbara
Copy link

dbara commented Apr 29, 2021

For color temp, why not just read the first value and set to all?
I'ts not perfect for all situations, but i believe one normally chooses all lights to be the same color.

I've got these Errors:

Error while executing automation automation.aaaaa_zigbee2mqtt_ikea_tradfri_5_button_remote_warm_white_lights: Error rendering data template: UndefinedError: 'var_light_entity' is undefined
22:59:00 – (FEHLER) Automatisierung

aaaaa Zigbee2MQTT - IKEA TRADFRI - 5 Button Remote - Warm White Lights: Choose at step 1: choice 5: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'var_light_entity' is undefined
22:59:00 – (FEHLER) Automatisierung - Die Nachricht ist zum ersten Mal um 22:59:00 aufgetreten und erscheint 2 mal

Template variable error: 'var_light_entity' is undefined when rendering '{% if state_attr(var_light_entity, "color_temp") - 18 < 153 %} {{ 153 }} {% else %} {{ state_attr(var_light_entity, "color_temp") - 18 }} {% endif %}'
22:59:00 – (FEHLER) helpers/template.py

@niro1987
Copy link
Author

Errors:
I've updated the code, can you retest it please.

Multiple lights and/or remotes:
Come to think of it, you could just create one automation for each light that you want to control. They would all fire at the same time.

@dbara
Copy link

dbara commented Apr 30, 2021

I'll test it on monday, as I'm not home currently.
One rule per light would normally be fine, but what if you have three light sources in one room, wich each has six or seven bulbs.

@dbara
Copy link

dbara commented Apr 30, 2021

I've seen something like it here:
https://community.home-assistant.io/t/turn-light-on-and-off-based-on-detected-motion/255994

If I could code it by myself I would do it, but I'm not a pro here :D

@niro1987
Copy link
Author

I’ll have a look

@niro1987
Copy link
Author

niro1987 commented May 3, 2021

@dbara
I've updated the code. It should now work fully and be able to target multiple lights. Please reset and share your findings.
Please be aware that while you can select multiple lights, you will have to select them using the Pick Entity button.

@dbara
Copy link

dbara commented May 3, 2021

Hey Niro, thank you for helping this much.
Current state is:

y Short Press Power
n Short Press Left
n Short Press Right
y Short Press Up
y Short Press Down
y Long Press Power
n Long Press Left
n Long Press Right
y Long Press Up
y Long Press Down

I get the following logs:

Error while executing automation automation.aaaaa2_zigbee2mqtt_ikea_tradfri_5_button_remote_warm_white_lights: Error rendering data template: UndefinedError: 'dict object' has no attribute 'entity_id'
22:19:51 – (FEHLER) Automatisierung
aaaaa2 Zigbee2MQTT - IKEA TRADFRI - 5 Button Remote - Warm White Lights: Choose at step 1: choice 4: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'entity_id'
22:19:51 – (FEHLER) Automatisierung - Die Nachricht ist zum ersten Mal um 22:19:51 aufgetreten und erscheint 2 mal

Hope this time I provided all helpful informations?
Thanks for your patience and work.

@niro1987
Copy link
Author

niro1987 commented May 3, 2021

It sounds like you selected the target lights either by area or by device, not by entity.

@dbara
Copy link

dbara commented May 3, 2021

Wow, you're right. Now I get another error:

Error while executing automation automation.aaaaa2_zigbee2mqtt_ikea_tradfri_5_button_remote_warm_white_lights: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
22:44:13 – (FEHLER) Automatisierung - Die Nachricht ist zum ersten Mal um 22:44:11 aufgetreten und erscheint 2 mal
aaaaa2 Zigbee2MQTT - IKEA TRADFRI - 5 Button Remote - Warm White Lights: Choose at step 1: choice 4: Error executing script. Error for call_service at pos 1: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
22:44:13 – (FEHLER) Automatisierung - Die Nachricht ist zum ersten Mal um 22:44:11 aufgetreten und erscheint 4 mal

@niro1987
Copy link
Author

niro1987 commented May 4, 2021

One of the selected lights doesn't have a property color_temp

@dbara
Copy link

dbara commented May 4, 2021

It's only one Light currently, and it's the same as before, it should support color_temp.
Can I provide more information somehow?

@niro1987
Copy link
Author

niro1987 commented May 5, 2021

When you got this error, was the light turned on or off before you pressed the (left or right) button?
You cannot change the color_temp while the light is turned off. Turn it on first, then change the color_temp with left or right.

@dbara
Copy link

dbara commented May 5, 2021

This is crazy, I repaired the bulb, now it works -.-
Only right & left are switched for me, eg. left button goes orange instead of blue and vice versa.

@dbara
Copy link

dbara commented May 5, 2021

*re-paired..
Tried it now with two bulbs simultaneously, works like a charm.
Only issue still left & right arrow seem to be swapped.

Cheers and thanks a lot already

@niro1987
Copy link
Author

niro1987 commented May 5, 2021

Swapped left and right

@dbara
Copy link

dbara commented May 5, 2021

Love you, it works :)
I hope this is the last one, maybe just a quick question.
When I press the buttons rapidly the action seems to pause a while. Then I have to wait several seconds before I can continue, is there a way to improve it?
And would it be ok for you if I fork this code? I would like to try customizing the brightness steps, maybe I can learn how to help myself out instead of giving you headaches ;)

@niro1987
Copy link
Author

niro1987 commented May 5, 2021

I believe the repeated button press is blocked by the remote itself. Saw that same behavior in a YouTube video by Frenck so I never gave it any thought.

Fork all you want. Make sure to change the source_url on your fork before you import it the first time.
Happy to help the community.

@FlexxFR
Copy link

FlexxFR commented May 22, 2021

Hi niro1987, thanks a lot of this blueprint. I have some issues with the color temp for IKEA lights.
On/of is working, dimming is working, changing colortemps is not. The lamps are capable of switching cold to warm white. (LED1732G11)

I'm using a lightgroup to change 3 of these exact same lights.

Some error logs:

Logger: homeassistant.components.automation.woonkamer_verlichting_eettafel_remote
Source: helpers/script.py:1337
Integration: Automation (documentation, issues)
First occurred: 8:02:24 (30 occurrences)
Last logged: 8:03:33

Woonkamer Verlichting Eettafel - Remote: Choose at step 1: choice 10: Error executing script. Error for repeat at pos 1: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Woonkamer Verlichting Eettafel - Remote: Choose at step 1: choice 5: Error executing script. Error for call_service at pos 1: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Woonkamer Verlichting Eettafel - Remote: Error executing script. Error for choose at pos 1: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Woonkamer Verlichting Eettafel - Remote: Choose at step 1: choice 4: Error executing script. Error for call_service at pos 1: Error rendering data template: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
Woonkamer Verlichting Eettafel - Remote: Error executing script. Error for choose at pos 1: Error rendering data template: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'



Logger: homeassistant.components.automation.woonkamer_verlichting_eettafel_remote
Source: components/automation/__init__.py:508
Integration: Automation (documentation, issues)
First occurred: 8:02:24 (14 occurrences)
Last logged: 8:03:33

Error while executing automation automation.woonkamer_verlichting_eettafel_remote: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Error while executing automation automation.woonkamer_verlichting_eettafel_remote: Error rendering data template: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

Any idea how to fix this?

@niro1987
Copy link
Author

niro1987 commented May 22, 2021

Hi @FlexxFR,

The error says that the selected light entity does not have a color_temp property while the action was performed. This could be caused by one or all of the following:

  1. When creating the automation, you selected the light by area or by device. For this blueprint to work you need to select it by entity.
  2. The light entity was turned off when you pressed one of the temperature buttons. For the temperature change to work, the light has to be on.
  3. The light entity does not have a color_temp property. Some lights that can change between warm-white and cold-white use the kelvin property.

@FlexxFR
Copy link

FlexxFR commented May 22, 2021

I used the entity selection but used a "light group", switching to 3 induvidual light entities did the trick.
Now it's working 👍

Although, HA is reproting the lightgroup is supporting the color_temp.

min_mireds: 250
max_mireds: 454
effect_list: stop_effect, okay, breathe, finish_effect, channel_change, blink
supported_color_modes: **color_temp**
entity_id: light.eettafel_lamp_1, light.eettafel_lamp_2, light.eettafel_lamp_3
friendly_name: Verlichting Eettafel
icon: mdi:lightbulb-group
supported_features: 44

Is it correct groups are not supported?

At first I still believed it was not working because I was expecting a "step-mode" like the original IKEA mode, but it's working more gradually by holding the buttons. Great, even better :)

@niro1987
Copy link
Author

Great to hear that you've got it working now!
By the looks of it, the light group does allow a color_temp to be set but it does not actually report what it's current color_temp value is.

@dbara
Copy link

dbara commented Jul 4, 2021

Hey Niro, I now had the Time to fork it, but somehow it doesn't seem to work.. I already deleted it, as I thought my edits were the issue, now I have a fresh fork but it still doesn't work properly.
I get the error "Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"
Happenes for the light color (color temp).

Do you have any idea?

The fork is here:
https://gist.github.com/dbara/911b76bb842bf2c76362c0ac9abb443b

@dbara
Copy link

dbara commented Jul 5, 2021

Hey again, I found the issue and how to fix it. When you select one entity, it adds it differently than when you add multiple. You have to edit the automation as yaml and correct it.

Wrong:
light_entity:
entity_id: light.0xbc...

Correct:
light_entity:
entity_id:
- light.0xbc...

Have a good day

@RaulVKoNe
Copy link

Hola de nuevo, encontré el problema y cómo solucionarlo. Cuando selecciona una entidad, la agrega de manera diferente que cuando agrega varias. Tienes que editar la automatización como yaml y corregirla.

Incorrecto: light_entity: entity_id: light.0xbc ...

Correcto: light_entity: entity_id: - light.0xbc ...

Que tenga un buen día

Thanks! I had the same problem with the Ikea bulbs and solved it with your tip

@jelflein
Copy link

replace every var_light_entities.entity_id[0] with var_light_entities.entity_id

@legionGer
Copy link

legionGer commented Nov 30, 2023

Okay, can someone help me out here? I got the same issue with the colors. The Tradfri bulb has the property of "color_temp" but it won't change the color, always gives the same error:

Logger: homeassistant.components.automation.zigbee2mqtt_ikea_tradfri_5_button_remote_warm_white_lights
Source: components/automation/__init__.py:676
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 17:48:25 (57 occurrences)
Last logged: 19:48:19

Error while executing automation automation.zigbee2mqtt_ikea_tradfri_5_button_remote_warm_white_lights: Error rendering data template: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Error while executing automation automation.zigbee2mqtt_ikea_tradfri_5_button_remote_warm_white_lights: Error rendering data template: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

I already repaired the bulb twice, no change. I can change the color using the dev menu or my own automatisation just fine.

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