Jump to content
New translation feature for the Shelly Forum ×

Multiple Pro3EM data collection and action


Recommended Posts



Example how to read data from  3 Pro EM meters measuring the consumption on the 3 different zones and script which is run on Pro3EM with ADD-ON to EV Charger which  disconnects (EV charging) for 30 min if the total power (represented by the consuming value on the right of the picture) is above a certain value.
 
image.thumb.png.0159df9d1d1ea2f414cf7890756a8308.png
 
A JScript which can do that: 
 
// Configuration

var deviceIPs = ['192.168.3.102', '192.168.3.246', '192.168.0.240']; // Add your device IPs here
const switchOff = 2000 ; // Total power in W above this device will switch off output

// Function to read data from a single Shelly Pro3EM
function readEMData(ip, callback) {
  Shelly.call(
    "http.get",
    { url: "http://" + ip + "/rpc/EM.GetStatus?id=0" },
    function (response, error_code, error_message) {
      if (response && response.message && response.code === 200) {
        var data = JSON.parse(response.body);
        var total_power = Math.round(data.total_aprt_power);
        callback(null, total_power); // Pass the total power to the callback
      } else {
        callback("Error: " + response.message, null);
      }
    }
  );
}

// Function to calculate the total power from multiple devices
function calculateTotalPower(ips) {
  var totalPower = 0;
  var count = 0;

  ips.forEach(function(ip) {
    readEMData(ip, function(err, power) {
      if (err) {
        print(err); // Handle the error, for example by logging it
      } else {
        totalPower += power;
      }
      count++;
      // Check if all requests are processed
      if (count === ips.length) {
        print("Total power: " + totalPower + " W");
        if (totalPower > switchOff) {        
          Shelly.call("Switch.set", {'id': 100, 'on': false});
        print("Switch OFF!");  
        }
      }
    });
  });
}




// Execution: 
Timer.set(5000 /* ms */, true /* repeating */, function () {
   calculateTotalPower(deviceIPs);
   }
);

 

Then you need to set Auto-ON in the Add-on relay: 
image.thumb.png.c49d944131ec5a22d959316155336540.png

As a last step do not forget to set a script to start when device boot. 

image.thumb.png.20d13e00a82bb510383a22817ff09b0a.png

With firmware 1.4, All pro devices will support virtual components. This will allow to monitor total consumption from the Shelly App and control the max power.  I will update this script when this new feature are available. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...