Sending payload to Ikea Trådfri

So I’m pretty new with AppDaemon. However, I used to code a lot with Python for some years ago. So I just need a nudge in the right direction. :slight_smile:

I got a color bulb from Ikea (LED1624G9). And I manage to use my Xiaomi Mini Switch to turn it on/off and add delays. However, is there a way to send a color payload to it? And how do I do this?

Thanks in advance!

Can you show the code you have already to turn it on and off, and then I can show you where to add color information.

Thanks for your reply.

Just made a simple one to teach my kid the colors. But it sort of defeats the purpose if I can’t set them. :slight_smile:

import appdaemon.plugins.hass.hassapi as hass
import datetime
from datetime import datetime
import sys
import time

class testa(hass.Hass):
    def initialize(self):
        self.log("Waiting for event...")
        self.listen_event(self.event_received, "deconz_event")

    def event_received(self, event_name, data, kwargs):
        event_data = data["event"]
        event_id = data["id"]
        event_received = datetime.now()
        if event_id == "ringklocka":
            if event_data == 1001 or event_data == 1002 or event_data == 1003 or event_data == 1004 or event_data == 1005 or event_data == 1006:
                self.log(str(data))
                self.turn_on("light.ikea")
                time.sleep(2)
                self.turn_off("light.ikea")

You don’t need to import datetime twice, you can remove “import datetime”. Why do you import sys and time, if you don’t use it in your app?

if event_data == 1001 or event_data == 1002 or ....

can be simplified to

if event_data in [1001, 1002, 1003, 1004, 1005, 1006]

Never use time.sleep if it can not be avoided. In your case it would be better to create an additional function something like turn_lights_off, which just turn off the light and then you call it in your event_received function with self.run_in(self.turn_lights_off, 2).

For the colors, you just need to add color="orange" to the turn_on method.

self.turn_on("light.ikea", color="orange")

I assume you want to loop through the colors when you press the button? I would suggest to create an input_select with the desired colors, then take the color from there and at the end of the event_received you use the service input_select.select_next, to change the input select to the next entry.

Thanks a lot. I used some copy and pasted code. That’s why it lacks some finesse. Did not see that I imported datetime twice, however. sys was from an earlier idea I had where I appended some libs to the path. Just forgot to remove it. How much easier it would be if AppDaemon was Java. :smiley:

Thanks for the ideas and input. Guest it would be easiest to create a tuple and then random for a color each press. However, it does not turn on when I’ve added color as a parameter. Tried exactly what you wrote, self.turn_on(“light.ikea”, color=“orange”) and the log fires. But the lamp stays off.

Also, is there a way to write hex codes instead of the color name?

Could be that the color attribute is not supported by the ikea bulbs or maybe it is color_name instead of just color, can’t remember. What attributes does your light have?

color_name it was! Thanks to both of you helping out. :slight_smile: