Delete Unresponsive Bulb From IKEA Tradfri Gateway

A couple of years ago I bought into the IKEA Smart Home ecosystem with their Tradfri product line and ended up with a lot of lightbulbs, plugs and even the air purifier. Unfortunately, the system hasn’t been reliable at all, very often devices go unresponsive for a couple of minutes, hours, days or even forever. That’s why this blog post exists.

For some weird reason, one IKEA lightbulb went into an unavailable state in the IKEA Home App and wasn’t associated to any room, it was just part of the list of devices. There was no way to remove the bulb or re-assign it somehow. Funnily enough, Apple’s HomeKit Home App could still see and work with the bulb, but the IKEA remote couldn’t. This situation was just a mess and super confusing. When hitting the Tradfri remote, the light bulb was ignored, when using the Home app or Siri everything was fine. At some point, I just wanted to reset this thing without deleting everything. Good thing, it’s possible with some command line utilities.

If you’re starting from scratch, there might be better solutions out there. Or just don’t even start with smart home stuff. It’s cool while it works, it’s a pain when it doesn’t.

Install coap-client

To communicate with the Tradfri Gateway, first compile and install coap-client. There are several repositories available on GitHub, with instructions how to build the client. I used the home-assistant's project fork of libcoap from GitHub, with the instructions from this page full of examples how to use the coap-client.

Build and compile the client:

git clone --depth 1 --recursive -b dtls https://github.com/home-assistant/libcoap.git
cd libcoap
./autogen.sh
./configure --disable-documentation --disable-shared --without-debug CFLAGS="-D COAP_DEBUG_FD=stderr"
make
make install

Authenticate Against the Gateway

To authenticate against the Tradfri Gateway and make API calls, you need to obtain the Gateway Code, which is printed on the back of the Gateway itself. Let’s get the key and go ahead and set a few environment variables to execute the commands later on:

TF_GATEWAYCODE=start1234  # Tradfri Gateway Code
TF_USERNAME=jonas # Pick a username
TF_GATEWAYIP=192.168.1.23 # The IP Address of your Tradfri Gateway

Next, we’re going to obtain a PreShared Key to communicate with the Gateway further:

coap-client -m post -u "Client_identity" -k "$TF_GATEWAYCODE" -e "{\"9090\":\"$TF_USERNAME\"}" "coaps://$TF_GATEWAYIP:5684/15011/9063"

If everything was successful, the output of the last command should look like this:

{"9029":"1.17.0019","9091":"1234preSharedKey"}

Take your version of 1234preSharedKey and set the variable:

TF_PRESHARED_KEY=1234preSharedKey

Get All Devices

With the next command, you’ll get a (long) list of ID’s of all devices connected to the Tradfri Gateway:

coap-client -m get -u "$TF_USERNAME" -k "$TF_PRESHARED_KEY" "coaps://$TF_GATEWAYIP:5684/15001"

The output will look like this:

v:1 t:CON c:GET i:33a1 {} [ ]
[65582,65552,65550,65569,65579,65568,65575,65580,65562,65589,65570,65561,65588,65565,65577,65578,65585,65581,65587,65545,65546]

To make things easier, take all the IDs from above, iterate through the list and find the correct device by name. This is how it’s done (remove the comma from the list and replace it with a space):

for i in 65547 65582 65552 65550 65569 65579 65568 65575 65580 65562 65589 65570 65561 65588 65565 65577 65578 65585 65581; do coap-client -m get -u "$TF_USERNAME" -k "$TF_PRESHARED_KEY" "coaps://$TF_GATEWAYIP:5684/15001/$i";done

Next, search for the name of the device you want to remove. You can take the exact name of the device in the Tradfri Home App. You can find the ID right after the key 9001. The device in my example below has the name Living Room:

{"9001":"Living Room","9003":65547,"9002":1531416268,"9020":1638362220,"9054":0,"9019":0,"3":{"0":"IKEA of Sweden","1":"TRADFRI bulb E14 WS opal 400lm","2":"","3":"2.3.050","6":1,"7":8705,"8":0},"5750":2,"3311":[{"5850":0,"5849":0,"5851":0,"5717":0,"5711":454,"5709":0,"5706":"f1e0b5","5710":0,"9003":0}]}

Take note of the ID.

Remove the Device / Bulb

Finally, let’s remove this thing from the Gateway once and for all. To make this happen, run the following command and put your device ID to the end of the URL. In the example below, the Bulb I want to remove has the ID 65547:

coap-client -m delete -u "$TF_USERNAME" -k "$TF_PRESHARED_KEY" "coaps://$TF_GATEWAYIP:5684/15001/65547"

Now the device is gone from your gateway. If you want to do more with the coap-client, this repository is a great resource.