Shelly EM Auto Toggle Based on Solar Panels Production

Introduction: Shelly EM Auto Toggle Based on Solar Panels Production

P1: house consumption (e.g. "P1 = 1kW" ⇒ we are consuming 1kW)
P2: solar panels production (e.g. "P2 = - 4kW" ⇒ we are producing 4kW)

The electric heater consumes 2kW when turned on.

  • We want to turn it on if the solar panel productions excedes of at least 2kW the current energy consumption.
  • We want to turn it off if we are consuming more than the solar panel production

Step 1: What You Need

You will need:

  • Wi-Fi connection
  • Shelly EM (with two clamps - in my case 2x50A were fine)
  • Wi-Fi relay (e.g. Shelly 1)
  • A Node.js application

Step 2: Connect Your Shelly EM

Turn off the general electricity counter.

  1. The first thing to do is to wire the clamps to the Shelly EM (P1+, P1- for the first clamp, P2+, P2- for the other one): connect them as showed in the User Guide.
  2. Then, bring it near to your counter and connect the power supply: neutral input to N, and line input to L.
  3. Now, attach the first clamp (P1) to the wire that goes to your house, and the other clamp to the wire that comes from the solar panels inverter. It's possible that you will have something strange with signs (a negative consumption): just don't care right now.
  4. Turn on the electricity counter and follow the User Guide's instructions to connect the Shelly EM to your Wi-Fi.
  5. Once you have the current power consumption on your app, you can change the direction of the clamps to have a positive number from P1, and a negative number (positive production - negative consumption) from P2, since we are measuring the consumption.

Step 3: Get Your API Tokens and Your EM Info

Shelly EM

From the Shelly Cloud app, go to "User Settings" and then click on the "Get key" button.

The key will be YOUR_KEY, and the server YOUR_SERVER.

Now go to the main page. Open the room of your EM, and then click on the EM. Go to "Settings", "Device informations" and copy the device ID (YOUR_ID - just the alphanumeric one, not the one in brakets) and device channel (YOUR_CHANNEL).

Smart switch

If you have a Shelly 1, you don't need to do nothing more. Otherwise, you should find out which is the URL to request to turn on or off your device. These two will be YOUR_TURN_ON and YOUR_TURN_OFF.

You will need to know which is your device's consumption (YOUR_DEVICE_CONSUMPTION). I suggest you to put a slightly higher number (i.e. if your device consumes 1900W, put 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 Your Application

Now, you should run your Node.js application continuosly. I run it every 60 seconds, but you can increase or decrease this number based on the maximum response time you want for turning on or off your device.

Step 6: Done!

Congratulations! Now you have a device that turns on automatically when you wouldn't pay anything for it, and that turns off automatically when you would pay the electricity for it!

Be the First to Share


    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • For the Home Contest

      For the Home Contest
    • Make It Bridge

      Make It Bridge



    5 months ago

    Hi! Shelly EM doesn’t support DC voltage.


    7 months ago

    Can someone do it for me? I give all the information because this is to difficult to understand? I download node js but don’t see where I can type the program.
    There are missing steps?!?


    Question 8 months ago on Step 2

    I cannot get a positive number in the equivalent of your CASA, my PV is correct at -3.6kw but CASA is e.g -1.7kw. which I assume means demand 1.9kw so 1.7 going back to grid. If I reverse the grid CT it is wrong if no solar production. I have the CT clamps in different directions. Is this a settings/display thing or just due to the your CT orientation.


    Question 8 months ago on Step 2

    Thank you for the info, but is it not possible to only use one of the clamps on the connection to the grid ? in case I am dumping energy to the grid sufficient for my purpose then we would swich shelly 1 ON. If possible what would NodeJS look like in this case?
    thank you for the help.


    Question 11 months ago on Step 5


    I need to make the same at home. But I believe that I need to know some basics that I do not have. Hardware no problem. Then. How can I run this program? Where? What server? I believe that in your explanation, jump to step 4 quickly without some important details. Can you explain it? Thanks in Advance


    Question 2 years ago on Introduction

    Hello Luca,
    You wrote: "and the other clamp to the wire that comes from the solar panels inverter". What do you mean? directly from the solar panels (DC) or after the inverter (AC)?
    Thanks for your response


    Answer 1 year ago

    Hi mohtak
    What I think they mean is connecting the clamp around the AC cable that comes out of your inverter (probably going into your Distribution board). This will then read the amount of solar power your inverter is creating.


    2 years ago

    I would suggest you run NodeJS continually as a web server and have the check run as an interval timer.


    Question 2 years ago on Step 5

    Thank you for this instructable. How can I set the time interval for repeat? I'm not expert of node.js :)