Tutorial continuation, here: Creating an energy monitoring system

Hello everyone, we are now resuming the tutorial. Today, I will show you how, starting from an energy sensor that only provides current power values in watts, to set up a system for tracking energy consumption (kWh) via Node-RED.

Have fun…

As I showed in my last tutorial « Controlling a solar battery via Node-RED – Gladys », it is relatively simple to create a control system in Node-RED. The results can then be transmitted to Gladys via Matter, MatterBridge or MQTT.
But let’s first explore another path. Let’s take a look at Europe: while the French, a nuclear advocate, shakes his head and the German « Michel » frowns, full of worries, what is happening there? The Austrians, for their part, have been climbing mountains for decades and dotting them with balcony solar panels. But there are also many other high-quality and affordable solar products. Take, for example, the company MyPV, which has long offered a heating element in its range. It is called AC ELWA and is controllable (this is not an advertisement, just an example)…
Let’s see what we can do with it…

This heating resistor can be controlled via an HTTP request; in other words, it is enough to send commands to the resistor, as you would with a browser, and observe what happens. In Node-RED, this is done using a node called « http request »:

What happens if we enter this command exactly in a browser and send it (the heater returns all the information in the form of an XML document):

et it is precisely this « power » entry that interests us. The next node is therefore an « XML node » to correctly format the value returned by the HTTP request:

Now we have the data in Node Red and we just need to read the correct date with the « power » value, which we do with a function node and a small function:

We therefore now have, in our flow, in msg.payload, the current power value of the resistor. This is where the magic happens. In another function, we simply proceed as follows:
We systematically collect the data received, convert it into kWh, add it to the stack and, when the internal stack (2 elements) is full, we move on to the next step. In other words: the stack contains two values. The entire flow is launched every 15 minutes (we will come back to this in a moment), collects two values and returns every 30 minutes the kWh value of the heating resistor, i.e. its power over a 30-minute period.
Here is the function:

// ***************************************************************

// Query the current measurement
let watt = msg.payload;

if (watt === 2147483648) {
watt = 0;
}

// 1. Read the two memories
let messungen = flow.get(« solar_messungen ») || ;
// Retrieve the current total of the meters (important for Gladys)
let kwhGesamtzaehler = flow.get(« solar_kwh_gesamt ») || 0;

messungen.push(watt);

// 2. Check if we have recorded 2 measurements (30 min)
if (messungen.length >= 2) {

// Calculate the two "Wh" blocks (watts × 0.25 hour)
let wh1 = messungen[0] * 0.25;
let wh2 = messungen[1] * 0.25;

// Consumption of the last 30 minutes in kWh
let kwh30Min = (wh1 + wh2) / 1000;

// Add the consumption to the total meter
kwhGesamtzaehler += kwh30Min;

// Round the value to 3 decimal places
kwhGesamtzaehler = Math.round(kwhGesamtzaehler * 1000) / 1000;

// 3. Prepare the memory for the next 30 minutes
flow.set("solar_messungen", []);             // Clear 15-Min-Messungen
flow.set("solar_kwh_gesamt", kwhGesamtzaehler); // Permanently save the total counter

// 4. Send message to Gladys
msg.topic = "solar-wr/kwh-total";
msg.payload = {
       wr_zaehler_kwh: kwhGesamtzaehler
 }; // Gladys receives the cumulative meter reading
return msg; 

} else {
// If it is the first measured value, save it in the memory
flow.set(« solar_messungen », messungen);
return null;
}

// ***********************************************************************

Here is where this function fits into the flow:

We start by adding an « inject » node and configure it so that it triggers the flow every 15 minutes, in the example between 10 a.m. and 8 p.m. every day

Here are the debug outputs. It performs two collections, then sends the data:

We just need to send the data to MQTT at the end of the flow using « mqtt out », then we can finally continue in Gladys :slight_smile:

In Gladys, we need the MQTT protocol, which must be accessible:

The rest is then very simple. Step 1: Create a virtual MQTT device:

Step 2: We add a function to monitor energy consumption:

Step 3: In the advanced functions of the energy sensor, we assign the values to the MQTT topic data.

Here is the information, it comes from Node Red, last function and then goes to Gladys via MQTT:

In the « Energy Monitoring » section, check again if the new energy monitoring device has been correctly assigned:

You just need to choose a suitable energy rate, then indicate the heating resistor as a meter.
That’s all…
Then add an energy tracker to the dashboard and indicate the heating resistor as the data source:

The result:

I thank all those who helped me overcome my beginner’s difficulties. If you spot an error, please let me know. If you see a way to simplify things, let me know.
In this tutorial, you have therefore discovered a method for querying a local energy meter via an HTTP request, then creating a suitable energy consumption monitoring system in Gladys.
Have fun, best regards, Thomas

1 Like