title | subtitle | tags | date | repository | remote | ||||
---|---|---|---|---|---|---|---|---|---|
Controlling IKEA Tradfri devices from your computer |
IKEA is cheap and everywhere |
|
2019-04-24 |
[email protected]:wschenk/tradfri-cli.git |
I stumbled upon a fun blogpost about the Dumbass Home and it turned me onto the IKEA Trådfri line of products. So I got a couple, and figured out how to control them from my laptop (or say a Raspberry PI) from node. Here's how to do it.
- Go to IKEA and buy stuff
- Setup IKEA Trådfri Gateway and Lights as normal
- Install the
node-tradfri-client
library - Copy the below scripts
First, set up a switch, lightbulb, and a gateway. The gateway needs to be plugged into the router which is a bit of a pain. You need at least one controller connected to a device to get the gateway to recognize things; once you have that it should be fairly straightforward. When in doubt, move closer to the gateway.
We are going to use the node-tradfri-client library, the delay library, and the conf node library to store values after the fun.
mkdir ikeatest
cd ikeatest
npm init
yarn add node-tradfri-client delay conf
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/gateway.js" language="js" %}}
Look at the back of your gateway to get the security token. We will use this to get an
access token to the gateway, which we will then use to communicate with the device.
Set the IKEASECURITY
token in the environment and then run this script:
$ export IKEASECURITY=akakakak
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/connection.js" language="js" %}}
Calling the observeDevices()
method will make the client start listening for devices that the gateway is connected to. The library itself keeps track of what it knows inside of the tradfri.devices
hash, so we'll pause for a bit to give it time to listen and then print out what it found.
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/devices.js" language="js" %}}
We can register a listener callback to watch for when thing change, keeping our program running forever watching for the lights to go on and off!
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/device_watcher.js" language="js" %}}
Now that we have code that can react to changes, lets write some code that controls things!
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/device_changer.js" language="js" %}}
This lets you add multiple commands on the line, so if we wanted to make a few changes at once you could do something like this:
node device_changer.js 65538 --on --color efd275 65543 --brightness 50 --on 65540 --on
The library also has methods to deal with scenes and rooms all at once. Let's take a look at a room watcher:
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/scenes.js" language="js" %}}
Lets write another small utility to be able to change a room to a preset setting!
{{% code file="articles/2019/controlling_ikea_tradfri_with_node/scene_changer.js" language="js" %}}
You can also update the settings of the devices in the controller, which we aren't going to cover. You can also add additional scenes and update them. These are documented further in the fantastic library.
Have fun playing around!
References: