Home Home Automation IKEA Tradfri Socket in NodeRED

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)

Buy USB Zigbee Stick CC2531

Buy it using these links to support NotEnoughTech.

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.

Project Download

Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated in style with crypto-currency

Smart Ideas with

Automate your space in with these ecosystems and integrate it with other automation services

Learn NodeRED

NodeRED for beginners: 1. Why do you need a NodeRED server?

0
To server or not to server? That's a very silly question!

Best Automation Projects

ESP8266 WIFI lights under $5

0
Your Sonoff device can't do this!

Free wireless doorbell (Amazon Dash button hack)

0
The most terrifying situation I can imagine* is missing a parcel

New Amazon Dash Buttons

0
You can teach new buttons old tricks!

ESP8266 ceiling light – demo

0
Testing the ESP8266 as a WIFI light controller

Try Tasker and Raspberry Pi for your home automation needs!

0
Raspberry Pi can help you with your Tasker profiles

Smart Home

Zemismart Zigbee switch

0
Zemismart Zigbee switch to control the light and your wall switch!

IFTTT Pro Alternative: Alexa ed.

0
Bypass IFTTT Pro restrictions for Alexa voice actions with this IFTTT Pro Alternative

The answer to almost all Echo needs: Alexa-remote2

0
Alexaremote2 is a Swiss army knife for Amazon Echo devices. Connect, control and listen to all Alexa enabled gadget at home.

This ESP32 is on FIRE! – M5Stack Core

0
This ESP kit is on fire! M5Stack Core merges IoT platform and learning in a very polished stackable module.

From KettleBot to ANY-Bot – SwitchBot

0
Switching anything on and of with SwitchBot -an insanely cute robot that does all the finger pressing for you