HomeHome AutomationIKEA 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 with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

Smart Ideas with

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

client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image

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

Tuya SDK for beginners: Intro to Tuya Cloud API

0
Working with Tuya Cloud API. A guide to Cloud automation for beginners, get started with REST!

NEST your old thermostat under $5

0
Nest-ing up your older thermostat under $5

Nora – Google Assistant in NodeRED

0
Integrate Google Assistant with NodeRED thanks to Nora - NodeRED home automation

DIY Smart Washing Machine – for about 15 bucks!

0
Learn how to add washing machine notifications to your Google Home on the cheap

Sonoff Zigbee Bridge – review

0
Sonoff line up will soon include Sonoff Zigbee Bridge and more Zigbee sensors - here is the first look

Smart Home

Remotes aren’t done with us yet: SwitchBot Remote

0
If you hate having 20 remotes hidden in your sofa, or your parents despise talking to Alexa to turn the lights on - SwitchBot Universal Remote is here to deal with all this!

We’ve seen this before: SwitchBot K10+ PRO

0
This is an odd one. Building on the success of SwitchBot K10+ they released SwitchBot K10+ PRO - but is the experience actually better?

Is this the smart panel we have waited for?

0
ITEAD has released a new smart panel: Sonoff NSPanel Pro 120 - have they learned the lesson from the terrible launch of the original Pro? Let's see what's new.

Aqara FP1E detects motionless humans

0
This isn't exactly a new device, it's an interaction of the original Aqara presence sensor. Aqara FP1E brings Matter, ZigBee and new triggers to your smart home

SwitchBot S10: cleaning re-imagined!

0
SwitchBot S10 promises unattended vacuuming and mopping so you can focus on things you love and care for. Does it deliver?