# Injection missing at certain time

**URL:** <https://discourse.nodered.org/t/injection-missing-at-certain-time/21918>\
**Category:** General\
**Created:** [18 February 2020 12:24 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918 "2020-02-18T12:24:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [18 February 2020 12:24 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/1 "2020-02-18T12:24:56Z")

</div>

I've been trying to understand why my program is buggy but I'm not sure if this behavior is normal.

I have a Flow acquiring generated energy during the day. Inside a flow, I use an injection node to reset the total accumulated energy inside a function. The example flow is [here](https://flows.nodered.org/node/node-red-contrib-watt2kwh).

The problem is: I set the inject to 00:00. It works well, but sometimes, it doesn't. First time in the morning I check and sometimes I have to manually inject to reset counting.

I've been searching and found [node-red-contrib--cron-pkjq](https://flows.nodered.org/node/node-red-contrib--cron-pkjq), but I'm not sure if this is the correct approach.

Is there a redundant approach or I'm doing something wrong?

Edit: Let me just quote inject node

> **Note:**"interval between times" and "at a specific time" will use cron.  
> "interval" should be less than 596 hours.  
> See info box for details.

It is supposed to work.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 February 2020 13:30 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/2 "2020-02-18T13:30:36Z")

</div>

> [@thalesmaoa](#):
>
> Inside a function, I use an injection node

(1) ?? there must be a semantics issue here. The `function` node and `inject` node are two different nodes and you can not put an inject node inside a function node.

Now if that is not what you meant, could you explain it again?

(2) the example flow you point to is part of a a node's description but there are two flows -

---

<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:** [18 February 2020 13:57 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/3 "2020-02-18T13:57:15Z")

</div>

If you have an inject node that you think is not performing the inject, add a debug node showing the inject node output and set it to log to the console, then you can look in the log in the morning to see whether it is the inject node that is at fault or some other problem.

---

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [18 February 2020 14:31 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/4 "2020-02-18T14:31:17Z")

</div>

> [@zenofmud](#):
>
> (1) ?? there must be a semantics issue here.

I edited the original post. Thank you.

> [@zenofmud](#):
>
> (2) the example flow you point to is part of a a node's description but there are two flows -

Here is a picture.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e98f81a7370480c509413a84a2840802b24cd64e.png)

@Colin Indeed I already did that. I just put a time inject 2 minutes ahead and it works. The problem is that it seems to fail sometimes.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da86c3392af8107a038d1814b796e7dd53b6afc4.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3ce872704a714df58a90a53a64e4a4fb1c945cc2.png)

Is there a way to check, inside the flow if the injects was received?

EDIT: I will put 2 inject nodes for redundancy.

---

<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:** [18 February 2020 14:35 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/5 "2020-02-18T14:35:17Z")

</div>

Better to wait till it fails then look to see whether it was actually the inject node that failed or whether there is a flaw somewhere else.

Post the code of the Store kwh node so we can have a look.

---

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [18 February 2020 14:37 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/6 "2020-02-18T14:37:33Z")

</div>

Ok. I will follow your suggestion:

```auto
// Save payload
var inputVal = msg.payload;

// Create output variable
var outputVal;

// If receive a reset, save the last value as daily value
if (inputVal == "reset"){
    flow.set("DailyInv_energyVal", flow.get("Inv_energyVal")||0 ); 
}else{
    // Continually store the total energy to the flow
    flow.set("Inv_energyVal", inputVal);
    outputVal = inputVal - flow.get("DailyInv_energyVal")||0;
    
    msg = {payload: outputVal, "inputVal": inputVal, "Inv_energyVal": flow.get("Inv_energyVal"), "DailyInv_energyVal": flow.get("DailyInv_energyVal")};
    return msg;
}

```

---

<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:** [18 February 2020 14:57 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/7 "2020-02-18T14:57:19Z")

</div>

Is there a reason you have used flow context rather than node context? Node context is safer if you only want to access the value inside that function node. Then there is no possibility of something else accidentally writing to it.  
Other than that I can't see any reason why it might not reset.  
Hopefully when it fails the log may tell you something useful.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 February 2020 16:37 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/8 "2020-02-18T16:37:11Z")

</div>

Your pictures show 2 flows - each with a reset going into a function... If they are both resetting at the "same" time and writing to the same flow variable name then as Colin (sort of) suggests then the order those things happen could be an issue if one resets before the other and then sometimes (for some reason) the other resets first.

---

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [18 February 2020 17:21 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/9 "2020-02-18T17:21:41Z")

</div>

> [@Colin](#):
>
> Node context is safer if you only want to access the value inside that function node.

Just did follow the example presented by the package writer. I will look into it.  
@dceejay Thank you! I will check that carefully as well.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [18 February 2020 17:28 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/10 "2020-02-18T17:28:18Z")

</div>

I'm rushing at the moment, but a quick look at your function node code does not do what I think you want it to do.  
Firstly, the readings from the `watt2kwh` node should form a running total, starting at zero at midnight, then every energy feed reading should be added to the context value, right up to midnight before being set to zero again.  
In the example flow that I included in the `watt2kwh` contrib node, the function code was;

```auto
var inputVal = msg.payload;
if (inputVal == "reset"){
 flow.set("energyVal",0); 
 } else {
var savedVal = flow.get('energyVal')||0;
savedVal += inputVal;
flow.set("energyVal",savedVal);
msg.payload = savedVal;
return msg;
}

```

So as you can see, `savedVal += inputVal;` continually adds the values together so we have a running total, whilst `flow.set("energyVal",0);` resets the value back to zero.

In your flow, you have `flow.set("DailyInv_energyVal", flow.get("Inv_energyVal")||0 );` which at midnight saves one context value to another context, but where is `Inv_energyVal` reset back to zero?

---

<div class="post-metadata">

**Author:** ![thalesmaoa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thalesmaoa/32/17067_2.png) [@thalesmaoa](https://discourse.nodered.org/u/thalesmaoa)\
**Post date:** [18 February 2020 18:03 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/11 "2020-02-18T18:03:17Z")

</div>

Hello @Paul-Reed, I have this variable that comes from the inverter that stores the total inverter energy since the day it was first power on.

I was concerned about power loss or even communication fault. If that happens, I lost info and variables and finally the total energy generated. Thus I added two functions.  
First one is the one you've created (just copy and paste). By the way, nice job. Really handy.  
The other one just retrieve the total energy from the inverter and store the last one from the last day (that is the expected behaviour from my function). With that, it is expected to get only daily energy.

```auto
outputVal = inputVal - flow.get("DailyInv_energyVal")||0;

```

If I get that working correctly, I can either manually reset to a value from a day before or store in a file.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [18 February 2020 19:38 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/12 "2020-02-18T19:38:50Z")

</div>

> [@thalesmaoa](#):
>
> I have this variable that comes from the inverter that stores the total inverter energy since the day it was first power on

...hmm, so you are not using the `watt2kwh` at all in this flow, and the energy accumulation is done by the inverter...

In that case, similar to what Colin has said, I would add some logging, to see what is happening. You could always add some `node.log` statements in your function node, like this;

```auto
// Save payload
var inputVal = msg.payload;

// Create output variable
var outputVal;

// If receive a reset, save the last value as daily value
if (inputVal == "reset"){
    flow.set("DailyInv_energyVal", flow.get("Inv_energyVal")||0 ); 

// Add some logging
       node.log("Reset msg received");
       node.log("Inv_energy = " + context.get(Inv_energyVal));
       node.log("DailyInv_energy = " + context.get(DailyInv_energyVal));
// End of logging

}else{
    // Continually store the total energy to the flow
    flow.set("Inv_energyVal", inputVal);
    outputVal = inputVal - flow.get("DailyInv_energyVal")||0;
    
    msg = {payload: outputVal, "inputVal": inputVal, "Inv_energyVal": flow.get("Inv_energyVal"), "DailyInv_energyVal": flow.get("DailyInv_energyVal")};
    return msg;
}

```

Then tomorrow, check your node-RED log, and you will see datestamped statements showing if a reset occured, and what the new context values are at that point in time.  
Something like...

```auto
pi@raspberrypi:~ $ node-red-log
18 Feb 19:39:49 - [info] [function:Divertor node 10] Reset msg received
18 Feb 19:39:49 - [info] [function:Divertor node 10] Inv_energy = 1582054789186
18 Feb 19:39:49 - [info] [function:Divertor node 10] DailyInv_energy = 1582054789186

```

---

<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:** [18 April 2020 19:38 UTC](https://discourse.nodered.org/t/injection-missing-at-certain-time/21918/13 "2020-04-18T19:38:52Z")

</div>

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