# I search for a Solution for Power Consumption Fallback

**URL:** <https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332>\
**Category:** General\
**Tags:** function-node\
**Created:** [9 November 2022 14:56 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332 "2022-11-09T14:56:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [9 November 2022 14:56 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/1 "2022-11-09T14:56:14Z")

</div>

Hi,

i search for a Solution to restore the Power Consumption if my Shelly get a powerloss and the Total Power Consumption falls back to 0

So far I've made it this far:

I have a MQTT IN with the actually results of the Shelly (optional for testing a Inject Node)  
One goes into a Function Query for Influx DB to Read the latest Power Consumption stored in the Influx DB  
Other goes after Rename the Payload into a Join Node  
Both goes to this function Node:

```auto
var totalstarttime = msg.payload.TotalStartTime;
var totalkwh = msg.payload.Total;
var yesterdaykwh = msg.payload.Yesterday;
var todaykwh = msg.payload.Today;
var period = msg.payload.Period;
var watt = msg.payload.Power;
var voltage = msg.payload.Voltage;
var ampere = msg.payload.Current;
var factor = msg.payload.Factor;
var scheinleistung = msg.payload.ApparentPower;
var blindleistung = msg.payload.ReactivePower;

var totalDB = msg.payload.totalkwh;

if (ampere === 0 && watt === 0) {

    return [null, msg];

} else {

    if (totalDB > totalkwh) {

        msg.payload = {
            totalstarttime: totalstarttime,
            totalkwh: (totalDB + totalkwh).toFixed(3),
            yesterdaykwh: yesterdaykwh,
            todaykwh: todaykwh,
            period: period,
            watt: watt,
            voltage: voltage,
            ampere: ampere,
            factor: factor,
            scheinleistung: scheinleistung,
            blindleistung: blindleistung,

        }

        return msg;

    } else {

        msg.payload = {
            totalstarttime: totalstarttime,
            totalkwh: totalkwh,
            yesterdaykwh: yesterdaykwh,
            todaykwh: todaykwh,
            period: period,
            watt: watt,
            voltage: voltage,
            ampere: ampere,
            factor: factor,
            scheinleistung: scheinleistung,
            blindleistung: blindleistung,

        }

        return msg;

    }

}

```

The If Power and Current = 0 is for stopping Input to the Influx Database if Power and Current = 0

The other one is my problem:  
if (totalDB \> totalkwh)  
If the actually Value from Shelly (totalkwh) smaller than the Database Value (totalDB)  
Add the actually Value to the Database Value

But there is the Problem:  
eg.: Database have 6.500 kwh  
actuall Value is 0.100 kwh

i gettin 6.600kwh thats correct, but after that add this value is added to InfluxDB

Now i have 6.600kwh in Influx DB and a actually value from Shelly of 0.150kwh

After the Second pass i have then not 6.650kwh but the wrong 6.750kwh because it asks the InfluxDB a second Time for the latest value.

I hope I explained that a bit understandable

 ![Unbenannt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53e40e7d13f06a1824608e219395b10fc7abc576.jpeg)

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [9 November 2022 15:44 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/2 "2022-11-09T15:44:17Z")

</div>

I am still having problems with my routine for doing this - the overall maths is wrong.

However, here is the relevant part. I check the reading coming in from the consumption monitor (PZEM-004 in my case) and compare it with it's last reading AND the overall reading for today. This is a dirty routine, but I am still working on it...

```auto
//let elecPowerToday = msg.pzem.ENERGY.Today; //Today Power from PZEM

let elecLastPowerToday = global.get("elec.LastPowerToday", 'storeInFile') || 0; //Context PowerToday
let elecNewPower = msg.pzem.ENERGY.Today; //Today Power from pzem
let elecOldPower = msg.payload[0].today; //Today Power from last db pzem

//let negPower = elecOldPower - elecNewPower
//if PZEM has been reset recalc NewPower XCPT when @midnight!!
if ((elecNewPower < elecOldPower) && resetTimeFlag === 0 ) {
    elecNewPower = elecNewPower + elecOldPower;
}

let elecNewPowerToday = elecNewPower; //in case of pzem reset

```

It may help just a bit...

---

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [9 November 2022 17:03 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/3 "2022-11-09T17:03:31Z")

</div>

Hi,

thanks @mudwalker i try your code.

I have found another thread here in Nodered Forum with this code:

```auto
// Given current total energy from Shelly in msg.payload
let runningTotalEnergy = global.get("runningTotalEnergy", "storeInFile") || 0
let previousShellyValue = global.get("previousShellyValue", "storeInFile") || 0
// test whether the Shelly has reset since last input
if (msg.payload < previousShellyValue) {
    // yes it has, just add new value to the total
    runningTotalEnergy += msg.payload
} else {
    // no so add the difference between this and last to the running total
    runningTotalEnergy += msg.payload - previousShellyValue
}
global.set("previousShellyValue", msg.payload, "storeInFile")
global.set("runningTotalEnergy",runningTotalEnergy, "storeInFile")
msg.payload = runningTotalEnergy
return msg;

```

I'll try to combine the codes to get it running.

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [9 November 2022 17:37 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/4 "2022-11-09T17:37:14Z")

</div>

Thank you. I will be able to spend some time on it tomorrow.

My maths works, but I get the occasional 'glitch' which gives completely 'out of whack' results. I shall report back.

---

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [10 November 2022 13:50 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/5 "2022-11-10T13:50:58Z")

</div>

I have modified the Script from @Colin down here, that works for me.

Only i have a Question: Where do this Script store the Data ? and where is the File stored ? (I have Debian on Raspberry Pi)

```auto
// Given current total energy from Shelly in msg.payload
let runningTotalEnergy = global.get("runningTotalEnergy", "storeInFile") || 0
let previousShellyValue = global.get("previousShellyValue", "storeInFile") || 0
// test whether the Shelly has reset since last input
if (msg.payload < previousShellyValue) {
    // yes it has, just add new value to the total
    runningTotalEnergy += msg.payload
} else {
    // no so add the difference between this and last to the running total
    runningTotalEnergy += msg.payload - previousShellyValue
}
global.set("previousShellyValue", msg.payload, "storeInFile")
global.set("runningTotalEnergy",runningTotalEnergy, "storeInFile")
msg.payload = runningTotalEnergy
return msg;

```

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [10 November 2022 13:55 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/6 "2022-11-10T13:55:14Z")

</div>

EDIT: @Starfoxfs - wrote this as you replied.

I have modified the routine and think (hope) it will not give me the glitches the previous one gave...

```auto
//New routine ************************************************************ */
//if PZEM has been reset or -ve anomalous reading recalc newPower XCPT when @midnight!!

//Fetch current and last stored readings New should always be >> old (Don't check during daily reading reset)
if ((elecNewPower < elecOldPower) && resetTimeFlag === 0 ) {
    //Modify new reading if == 0 or < OldPower
    if (elecNewPower == 0){
        elecNewPower = elecOldPower;
    }
    
    //be careful- don't want to skew readings, so might be best to ignore
    /*
    //mitigate anomalous reading to ripple through routine
    else{
        elecNewPower = elecOldPower + elecNewPower;
    }
    */
}
// *********************************************************************** */

```

You will then be able to calculate the difference between the New Power and Old Power and get the power used between two valid readings.

HTH

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [10 November 2022 14:02 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/7 "2022-11-10T14:02:28Z")

</div>

The variables are stored as Context Data. 'storeInFile' is probably in memory and lost during reset.

See [this thread here](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115) for more details.

---

<div class="post-metadata">

**Author:** ![Starfoxfs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/starfoxfs/32/69119_2.png) [@Starfoxfs](https://discourse.nodered.org/u/Starfoxfs)\
**Post date:** [10 November 2022 15:07 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/8 "2022-11-10T15:07:39Z")

</div>

I have configured it out in Settings.json

Now it´s stored in

> ~/.node-red/context/global/global.json

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [17 November 2022 06:58 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/9 "2022-11-17T06:58:01Z")

</div>

Now modified my code to the less confusing version presented by @Colin. Thank you.

I have learnt some more about Javascript and dumped my routine where I was needlessly trying to trap completely unnecessary possibilities I had probably imagined! (Confused? So was I! :lol)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 November 2022 08:05 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/10 "2022-11-17T08:05:23Z")

</div>

> [@mudwalker](#):
>
> trying to trap completely unnecessary possibilities

It is often a tricky balance to get the right amount of defensive code to guard against incorrect data.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 January 2023 08:05 UTC](https://discourse.nodered.org/t/i-search-for-a-solution-for-power-consumption-fallback/70332/11 "2023-01-16T08:05:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
