PSU control with Ikea Trådfri and no additional HW

Hi!

I wrote a guide to how I set up the PSU control plugin in conjunction with Ikea Trådfri on Reddit and thought that I might as well post it here too:

Inspired by this post by u/tollsjo I wanted to try my hand at creating a setup where I could control the power of my Ender 3 without any additional hardware, given that I already have an Ikea Trådfri Wireless Control Outlet and an Ikea Trådfri Gateway.

The idea basically being that I could get Octoprint to turn off the printer when a print is done, and in my case also turn off the lights and the ABL sensor.

You'll need

  • Octoprint running on a device that can run node (which should be basically any device that's capable of running octoprint?) and access to a console on that device (e.g. ssh-access to a raspberry pi)
  • An Ikea Trådfri Gateway (take note of the 16 character alphanumerical key on the back, you'll need that later)
  • An Ikea Trådfri Wireless Control Outlet

My Hardware Setup

  • Octoprint running through the default Octopi image on a Raspberry Pi 2b
  • Ender 3, Tru-Lev and IKEA Jansjö lamp all connected to an Ikea Trådfri Wireless Control Outlet, conveniently named "Ender" in the Trådfri app

Software setup

  1. Connect to your device in your preferred manner and open a console, in my case SSH into my rPi using the terminal.
  2. Install git

sudo apt-get update

sudo apt-get install git

  1. Install node

  2. Navigate to folder of choice (I chose to stay in the home folder of the default pi user)

  3. Clone the tradfri-cli repository

git clone https://github.com/wschenk/tradfri-cli.git

  1. Install the tradfri-cli repository by navigating into it and running node install

cd tradfri-cli

npm install

  1. Test that your setup has run correctly (and check that the Trådfri devices you want to control are listed)

IKEASECURITY=PUTYOURSECURITYCODEHERE node devices.js

  1. Test that you are able to successfully control the devices (in this example, turning on an outlet called Ender)

IKEASECURITY=PUTYOURSECURITYCODEHERE node device_changer.js "Ender" --on

  1. In Octoprint, install the PSU control plugin as you do with any other plugin

  2. Configure the PSU control plugin to use the Tradfri-cli script using the built in settings.

a) Set "Switching method" to "System Command"

b) Set "On System Command" to

cd /home/pi/tradfri-cli/ && IKEASECURITY=PUTYOURSECURITYCODEHERE node device_changer.js "Ender" --on

c) Set "On System Command" to

cd /home/pi/tradfri-cli/ && IKEASECURITY=PUTYOURSECURITYCODEHERE node device_changer.js "Ender" --off

d) Set "Sensing method" to internal

e) Activate "Automatically turn PSU OFF when idle"

(of course, for b and c you'll need to change your script location to match where you installed the script, add your security code and change the name of the device to match yours)

To test that everything is working fine you can test the PSU control plugin by pressing the newly added lightning-button in the menu bar of your Octoprint dashboard.

Happy printing!

10 Likes

Good work :slight_smile:

Wow perfect thanks ! :blush:

Super tutorial very clear !

My Octoprint runs on a debian server (all i hade in hand at the time) and after installing node tried tu launch device.js but i've got an error :frowning:

[11:53] root@HomeServer:/home/octoprint/tradfri-cli# node device.js

module.js:550

throw err;

^

Error: Cannot find module '/home/octoprint/tradfri-cli/device.js'

at Function.Module._resolveFilename (module.js:548:15)

at Function.Module._load (module.js:475:25)

at Function.Module.runMain (module.js:694:10)

at startup (bootstrap_node.js:204:16)

at bootstrap_node.js:625:3

And that even with the IKEASECURITY Variable.

I'm running node v8.17.0 and npm 6.13.4

Any idea ?

I just responded to you on Reddit as well but might as well respond here too:

It seems you've omitted the "s" from devices.js, try correcting the filename and see if the issue persists.

I ... I ... duh ...

Everything works great ! Thanks ! Next time i'll be more attentive...

Hah, no problem!

I'm just happy that I could help

Something goes wrong. It worked to this day. Now it is not. Any idea what should i do?

Blockquote
Looking up IKEA Tradfri gateway on your network
Connecting to TRADFRI-Gateway-xxxxxxxxxxxxx.local <-- xxxx is for sensitive data
Securely connecting to gateway
(node:6637) UnhandledPromiseRejectionWarning: Error: The provided credentials are not valid. Please re-authenticate!
at TradfriClient. (/home/pi/tradfri-cli/node_modules/node-tradfri-client/build/tradfri-client.js:105:47)
at Generator.next ()
at fulfilled (/home/pi/tradfri-cli/node_modules/node-tradfri-client/build/tradfri-client.js:5:58)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:6637) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6637) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I think this is the line to focus on:

Error: The provided credentials are not valid. Please re-authenticate!

Looks like your authentication has lapsed. Check your Ikea security key, it might’ve changed from an Ikea upgrade or something?

Hi!

I've done everything correctly and the commands for turning my printer on and off work just fine from shell. However, when copying/pasting the same commands, including the cd statement to the PSU control plugin, nothing seems to happen. The log just shows:

2020-04-26 11:26:05,496 - octoprint.plugins.psucontrol - INFO - Switching PSU On
2020-04-26 11:26:09,139 - octoprint.plugins.psucontrol - INFO - Switching PSU Off

No error messages, and I've verified the syntax is correct including the &&. Again, if I copy/paste the command lines from the plugin settings to shell, everything works just fine. Octoprint is running with the same user as I'm using when testing it in a shell, so it shouldn't be a user permission issue. Any thoughts?

Hmm no... I've no idea...

Maybe you could ask the PSU-control author how to debug?

I managed to resolve my issue by creating two shell scripts (on and off) and pointing PSU Control to those instead of the node scripts directly. Working well now. :smiley:

1 Like

Great to hear!

Would the scripts be easier to set up than my version you think? If so, would you mind sharing?

Not sure if it's easier, the scripts still call the tradfri-cli scripts.

#!/bin/bash
export IKEASECURITY=blahblahblah
cd /opt/octoprint/dev/tradfri-cli
/snap/bin/node device_changer.js "3D printer" --on
exit 0

and

#!/bin/bash
export IKEASECURITY=blahblahblah
cd /opt/octoprint/dev/tradfri-cli
/snap/bin/node device_changer.js "3D printer" --off
exit 1

Then simply make the scripts executable with chmod and call them from the plugin.

1 Like

Hi, i might just be stupid but im in the tradfri repository and type npm install but it just says command not found. Everything ive done without any problems, what am i doing wrong?

I suspect you missed step 3 of the guide - install node.

If you installed that, then maybe you need to restart the session to get the path updated.

(That would be my guess as well)

1 Like

It works perfect for me since I use those 2 script
I was having the same issue as stated upper
Thanks for this great stuff
I'm so please to see the morning the printer off while it was printing during my sleep

I really like the idea ! I am confused about one thing, Node should be installed on the octoprint right ?
If so, there are a lot of options and I'm not sure which i should take for my raspberry 4 running octoprint

EDIT : I found some help in the octoprint discord. The new problem that I have is I cannot find devices.js. However, I found a devices.Ts in the src folder, but changing the command to this file didn't work either