IKEA Tradfri Socket in NodeRED

Making Smart Sockets better since 2016!

I got another product from the furniture and meatballs company: IKEA Tradfri Socket. This smart socket comes on its own or bundled with a 2 button remote. I actually got both, but I will talk about the button in my next article (a reason to follow me on social media!). I already covered most of the IKEA things in NodeRED, so let’s make IKEA Tradfri Socket a little bit better.

Meatballs of automation & IKEA Tradfri Socket

You might have seen an article about timers in NodeRED earlier on. I wanted to write about timers in detail, otherwise, this article would be much longer. Skim over that write up if you have any questions. My today’s aim is to improve the IKEA Tradfri Socket in NodeRED.

Here are the things that IKEA Tradfri Socket can do in NodeRED:

  • Alexa & Google Assistant compatible
  • Works on LAN only
  • Recovery of timers on reboot|restart
  • Unlimited timers
  • Dashboard Timers
  • Day of the week support
  • Online status of each timer
  • Easy integration with Home Assistant

This is an impressive list. Before we start, I have to point you to a couple of articles about Zigbee2MQTT if this is your first time with IKEA things in NodeRED.

Getting things ready

To make the IKEA Tradfri Socket compatible you will need CC2531 USB Zigbee stick flashed with a custom firmware. Once you have the stick ready you can pair the socket by holding the button in the pinhole at the bottom for a couple of seconds. On release, you should see a new device added to your config file.

Edit the configuration.yaml file and name the new devices to something you will remember.

IKEA Tradfri Socket in NodeRED

Smart sockets are probably one of the easiest things to automate. All you can do with it is to toggle the socket ON|OFF. That’s it (unless you have a Sonoff POWR2 which measures power use).

In this tutorial, I will use the following nodes:

 node-red-contrib-alexa-home-skill
 node-red-contrib-schedex  
 node-red-dashboard 

Zigbee2MQTT

To connect with IKEA Tradfri Socket in NodeRED you will use MQTT. To query the status submit the payload: {"state":""} to zigbee2mqtt/devicename/get. The response will be posted to the topic: zigbee2mqtt/devicename and it will look like this:

{"state":"ON","linkquality":92}

To control the IKEA Tradfri Socket in NodeRED simply send msg.payload set as ON|OFF. For some reason, responses from IKEA Tradfri Socket are duplicated, so I used a rate limit node to discard the duplicate messages coming within 1 sec.

Alexa

There is the entire article explaining how Alexa in NodeRED works (the easy way) so please read it first. Because Alexa commands pass true|false I will need a switch node to translate this into something compatible with the Zigbee2MQTT commands.

Switch node way

You can use a switch node & change node combination to detect the true|false payload and change it to ON|OFF which will toggle the IKEA Tradfri Socket in NodeRED.

Function way

If you want to keep things even more organised, you can combine the 3 nodes above in a single function node:

Function Node: Switch (Alexa)
var x = msg.payload;

if(x === true){
    msg.payload = "ON";
    return [msg, null];
}

if(x === false){
    msg.payload = "OFF";
    return [null, msg];
}

Either way, the voice commands will work just fine.

Google Assistant

To connect the IKEA Tradfri Socket in NodeRED to Google Assistant I’m going to use gBridge. Read on how to create devices (you can have up to 4 devices in the free plan) and you can use MQTT to connect the socket with Google Devices. Since the gBridge uses different payloads, you will need to convert it in the way as with Alexa. Use the topic: gBridge/xxxx/devicename/onoff to receive the commands from Google Assistant – the payload will be 1|0.

Switch node way

You can use a switch node & change node combination to detect the 0|1 payload and change it to ON|OFF which will toggle the IKEA Tradfri Socket in NodeRED.

Function way

If you want to keep things even more organised, you can combine the 3 nodes above in a single function node:

Function Node: Switch (gBridge)
var x = msg.payload;

if(x === 1){
    msg.payload = "ON";
    return [msg, null];
}

if(x === 0){
    msg.payload = "OFF";
    return [null, msg];
}

To get the status updates over to Google Assistant, you will need to post ON|OFF to the MQTT topic: gBridge/xxxx/devicename/onoff/set. just remember that the state of the socket is stored in msg.payload.status. You can use a change node to reassign the msg.payload.

Dashboard and timers

It’s possible to add a large number of timers to each socket. You will be limited by the memory of the Raspberry Pi, but there is no other limit. I’m working with MQTT update node to create a status notification. You can actually change the colour of the text in the dashboard based on variables:

Function Node: Format Colours
switch (msg.payload.state) {

case "ON" : 
    msg.color = "green";
    msg.payload = "ON";
    break;
    
case "OFF" : 
    msg.color = "red";
    msg.payload = "OFF";
    break;
    
default : 
    msg.color = "black";
    msg.payload = "not updated";
    break;
}

return msg;

Just make sure to set the text node (value field) to:

I also added a switch to my dashboard, and the switch simply sends ON|OFF payload. The switch isn’t updated to reflect the current state.

Lastly, I dropped 2 timers in NodeRED to create morning and evening behaviour. You will spend some time organising your dashboard panel to get the timers displayed correctly.

Sadly, that part is a pain in the neck, and you will have to sit through it all and make sure your dashboard has a correct order of things:

  • Timer Status (text)
  • T1 ON (picker)
  • Set ON (button)
  • T1 OFF (picker)
  • Set OFF (button)
  • Reset Button (button)
  • Mon (switch)
  • Tue (switch)
  • Wed (switch)
  • Thu (switch)
  • Fri (switch)
  • Sat (switch)
  • Sun (switch)

Conclusion

And that’s how you add IKEA Tradfri Socket in NodeRED! Once you break it down into pieces, it’s not that hard even if the flow on its own looks intimidating. The benefits are clear, the GUI is practical and with recovery options, you will never miss a schedule again. Let me know what do you think about this project in this Reddit thread.

Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE