Skip to main content

Example 1 - Power calculation, energy meter, daily values

Example 1a - Power calculation

Task:

The electrical power of a consumer is to be calculated from measured voltage (LINEVOLT_1, LINEVOLT_2 in volts) and current signals (LINECURRENT in amperes). The voltage is measured redundantly, whereby one measuring point can be switched off. The output should be in the unit "kW". Determination of the energy consumed and delivered in "kWh".

Solution:

// Constants for unit conversion
uConv_Ws2kWh = 1e-3 / 3600; // Ws -> kWh

LINEVOLT = max(LINEVOLT_1, LINEVOLT_2); // V
POWER = LINEVOLT * LINECURRENT; // W
POWER_KW = POWER / 1000.0; // kW
WAUF_KWH = integrate(max(POWER, 0.0), {storage: 'sto_WAUF'}) * uConv_Ws2kWh; // kWh
WABG_KWH = -integrate(min(POWER, 0.0), {storage: 'sto_WABG'}) * uConv_Ws2kWh; // kWh

Functions used

Explanation:

  • uConv_Ws2kWh: First, a factor is defined for the unit conversion from Ws to kWh. This improves the readability of the following code. Such definitions are best placed centrally at the beginning of the formula set.

  • LINEVOLT: The maximum of the two measured mains voltages is taken. If one fails and becomes 0.0, the other still provides a plausible value.

  • POWER and POWER_KW: The electrical power is first calculated in W (watt) and separately in kW (kilo-watt). This separation is necessary because the subsequent integral function should always be fed with SI base units of the "MKS" system, in this case the power in W.

  • WAUF_KWH, WABG_KWH: The calculated power is fed separately to the two integrators integrate() via the min() and max() functions according to its sign and thus the power flow direction. Both integrators hold their current meter reading (in SI base units!) in the specified memory locations. Integration adds the dimension of time as s (second) to the integrand. The integrated value therefore has the energy unit "Ws" (watt-second). This is converted into kWh (kilo-watt-hour) using the scaling factor calculated above.

  • If the units of the measurement signals to be integrated (kV, kA, mA) or the integrated value (kJ, MWh, etc.) change later on, the value stored in the integrator can easily continue to be used, provided it is available in SI base units. In this case, only an easily understandable, comprehensible scaling of the input variable or output variable is necessary, but no permanent rescaling of the value stored in the integrator or a forced restart.

Example 1b - Daily intermediate values

Task:

Now the calculation is to be extended so that daily meter readings at the time of the end of operation, the consumption of the last 7 days and the current consumption of the current day are output.

Solution:

In addition to the formula text from example 1a:

newDay = timer({ minute: 15, hour: 3 }); // Reference time, shift change

_hist_WAUF_KWH = buffer(WAUF_KWH, newDay, {size:8, storage:'sto_h_WAUF'});
WEEK_WAUF_KWH = _hist_WAUF_KWH[0] - _hist_WAUF_KWH[7];
YDAY_WAUF_KWH = _hist_WAUF_KWH[0] - _hist_WAUF_KWH[1];
TDAY_WAUF_KWH = WAUF_KWH - _hist_WAUF_KWH[0];

_hist_WABG_KWH = buffer(WABG_KWH, newDay, {size:8, storage:'sto_h_WABG'});
WEEK_WABG_KWH = _hist_WABG_KWH[0] - _hist_WABG_KWH[7];
YDAY_WABG_KWH = _hist_WABG_KWH[0] - _hist_WABG_KWH[1];
TDAY_WABG_KWH = WABG_KWH - _hist_WABG_KWH[0];

Functions used

Explanation:

  • Variables for internal use are marked with a _ as the first character. This improves readability.

  • newDay: The timer() function generates a pulse signal which, with the given configuration, generates a short pulse every morning at 3:15. This pulse is then used to save the counter readings.

  • _hist_WAUF_KWH, _hist_WAUF_KWH: The buffer() function is used to transfer energy counter values to a ring buffer of length 8. Whenever a positive edge is present with the newDay signal, the values in the ring buffer are shifted one position further back and the current value WAUF_KWH or WABG_KWH is stored at position [0]. These memories also save their contents in a persistent location in order to continue calculating with the previously determined counter values after a restart.

  • WEEK_*: The energy consumption of the last 7 days results from the difference between the value recorded in the last night and the 8 nights before [7].

  • YDAY_*`: The energy consumption of the last day (yesterday) results from the difference between the value recorded in the last [0] and penultimate [1] night.

  • TDAY_*: The energy consumption of the current day (today) is the difference between the absolute value and the value stored last night.

As the stored historical values only change once a day, data transfer to the cloud or to OSF files is very efficient.