Shelly EM Auto Toggle solar panel production

Shelly EM Auto Toggle solar panel production

Step 1: What You Need would need:

Wi-Fi connection Shelly EM (with 2 clamps - 2x50A worked fine for me) Wifi relay (e.g. Shelly 1) Node.js application


Step 2: Connect Shelly EM

First you need to wire the terminals to the Shelly EM (first terminal is P1+, P1-, second is P2+, P2-): connect as shown in the manual. Then bring it near the counter and plug in the power. Connect the neutral input to N and the line input to L. Then attach the first clamp (P1) to the cable going to your house and the other clamp to the cable coming from the solar panel inverter. There may be some oddities in the symbols (negative usage): ignore them for now. Turn on your electricity meter and follow the instructions in the user manual to connect Shelly EM to your WiFi network. Once you get the current power consumption in your app, change the direction of the clamp and measure the consumption so you get a positive number from P1 and a negative number from P2 (positive production - negative consumption ) can be obtained.


Step 3: Get API Token and EM Information Shelly EM In the Shelly Cloud app

Go to User Settings and click the Get Key button. The key is YOUR_KEY and the server is YOUR_SERVER. Go to main page. Open the EM room and click EM. Go to Settings, Device Information and copy the Device ID (YOUR_ID - only alphanumeric characters, not the one in brackets) and Device Channel (YOUR_CHANNEL). smart switch If you have a Shelly 1, you don't need to do anything else. Otherwise, you'll need to figure out which URL to request to turn the device on or off. Two of these are YOUR_TURN_ON and YOUR_TURN_OFF. You need to know your device consumption (YOUR_DEVICE_CONSUMPTION). We recommend entering a slightly higher number (for example, if your device consumes 1900W, enter 2000W).


Step 4: Set Up Your Node.js Application

shelly_server = 'YOUR_SERVER';
shelly_key = 'YOUR_KEY;
shelly_channel = 'YOUR_CHANNEL';
shelly_id = 'YOUR_ID';
turn_on_url = 'YOUR_TURN_ON';
turn_off_url = 'YOUR_TURN_OFF';
device_consumption = YOUR_DEVICE_CONSUMPTION; // e.g. for 2kW put: 2000

const device = function(status) {
    if (status == 'on') {
            .then(res => res.text());
    else if (status == 'off') {
            .then(res => res.text());

fetch(shelly_server + '/device/status?channel=' + shelly_channel + '&id=' + shelly_id + '&auth_key=' + shelly_key)
    .then(res => res.json())
    .then(json => {
        if(json.isok) {
            emeters =;
            home_consumption = emeters[0].power; // > 0
            solar_panels_production = - emeters[1].power; // > 0
            available_energy = solar_panels_production - home_consumption;
            if(available_energy < 0) {
            } else if(available_energy > device_consumption) {
        } else {
            // Shelly EM is not reachable

Step 5: Run the Application

Your Node.js application should now run continuously. I have it running every 60 seconds, but you can increase or decrease this number based on your maximum response time to turning the device on or off.


Step 6: Done! congratulation!

Now you have a device that automatically turns on when you don't pay your bill and turns off automatically when you pay your electricity bill.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.