# Help modifying my code

**URL:** <https://discourse.nodered.org/t/help-modifying-my-code/58184>\
**Category:** General\
**Created:** [12 February 2022 02:47 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184 "2022-02-12T02:47:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterBC](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@PeterBC](https://discourse.nodered.org/u/PeterBC)\
**Post date:** [12 February 2022 02:47 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/1 "2022-02-12T02:47:38Z")

</div>

First post so be kind please ...

I've managed to adapt some Node-Red code from another person to my application. Its working (monitoring and logging a LFP battery using an Electrodacus BMS). But I am drowning in lots of this and barely keeping my head above water. Decades since I programmed much (in Turbo Pascal !).  
The code correctly logs every 30 secs a bunch of paramters to a log file. I even managed to update that to include the battery temperature.  
It also logs once a day a summary line of mostly calculated values to a different file.  
I want to update that bit to log the temperature at that time.  
This code from the 30sec logfile is working and I added the msg.payload.tempExt bit to it.

```auto
str = msg.payload.soc.toString() + "," +
      Ah.toFixed(1) + "," +
      battV.toFixed(2) + "," +
      **msg.payload.tempExt + "," +**
      msg.payload.cellsMV[0].toFixed(3) + "," +
      msg.payload.cellsMV[1].toFixed(3) + "," +
      msg.payload.cellsMV[2].toFixed(3) + "," +
      msg.payload.cellsMV[3].toFixed(3) + "," +
      msg.payload.cellsMV[4].toFixed(3) + "," +
      msg.payload.cellsMV[5].toFixed(3) + "," +
      msg.payload.cellsMV[6].toFixed(3) + "," +
      msg.payload.cellsMV[7].toFixed(3) + "," +
      msg.payload.flags.delta + "," +
      msg.payload.currentMA.pv1.toFixed(2) + "," +
      msg.payload.currentMA.battery.toFixed(2) + "," + 
      currentLoad.toFixed(2) + "," + 
      msg.payload.currentMA.extLoad.toFixed(2);
msg.payload = new Date().toLocaleString() + "," + str;
return msg

```

This is the daily stats code bit that I'd like to add the temp to also..

msg2.payload = d.toLocaleString() + "," + lowBattv.toFixed(2) + "," + hiBattv.toFixed(2) + "," +  
lowSOC + "," + hiSOC + "," + maxAh.toFixed(1) + "," +  
hiPV.toFixed(1) + "," + hiLoad.toFixed(1) + "," +  
lowCellv.toFixed(3) + "," + hiCellv.toFixed(3) **+ "," + msg.payload.tempExt** ;

I added the last bit to it but what gets written to the file is:  
2/11/2022, 3:40:14 AM,25.00,26.56,94,94,-1.5,0.0,3.5,3.312,3.323,undefined  
Note the 'undefined'.

I suspect this is a trivial issue to fix, can anyone tell the correction from what I've posted?

rgds.. Pete

---

<div class="post-metadata">

**Author:** ![PeterBC](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@PeterBC](https://discourse.nodered.org/u/PeterBC)\
**Post date:** [12 February 2022 02:53 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/2 "2022-02-12T02:53:48Z")

</div>

The ` **msg.payload.tempExt + "," +** ` bit above was my attempt to bold the bit I added the real line doesnt have any \*\* around it.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 February 2022 03:11 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/3 "2022-02-12T03:11:15Z")

</div>

> [@PeterBC](#):
>
> msg.payload.tempExt

Hello Pete ..

`msg.payload.tempExt` most propably doesnt exist in the msg going in that Function node  
so that is why it is `undefined`.

Can you show us a screenshot of your flow to see how things are wired?

Also can you add a Debug node before that Function node to capture a msg going in to see its properties and values ?

How to capture and share a message from the Debug window :  
Hover over the root of the msg and use the Copy value button

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58c5a9592ca1aa48b2d0db4003f7cf57b08ad196.png)

Share the message (using ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b9a78bfe33d1d94f02c27b531e354539161c96d5.png)) explaining where its coming from.

---

<div class="post-metadata">

**Author:** ![PeterBC](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@PeterBC](https://discourse.nodered.org/u/PeterBC)\
**Post date:** [12 February 2022 03:47 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/4 "2022-02-12T03:47:04Z")

</div>

Thanks Andy  
Here is a screen shot of the relevant parts of the flow

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7418b9a22f64adf9a1b3f7c4cc68f179834a4e2.png)  
I'm foggy (on much of it !) about how the Midnight Action trigger should access the normal tempExt msg thats in the main flow every second, but gets logged every 30.  
Struggling to use the debug node bits .. drowning a bit in debug info as its showing the msg every second ...

The full code of the Log Daily Stats node is

```auto
var msg2 = { payload: msg.payload.length };
var lowBattv = flow.get("lowBattv");
var hiBattv = flow.get("hiBattv");
var lowSOC = flow.get("lowSOC");
var hiSOC = flow.get("hiSOC");
var maxAh = flow.get("maxAh");
var hiPV = flow.get("hiPV");
var hiLoad = flow.get("hiLoad");
var hiCellv = flow.get("hiCellv");
var lowCellv = flow.get("lowCellv");

var d = new Date();
d.setDate(d.getDate() - 1);

msg2.payload = d.toLocaleString() + "," + lowBattv.toFixed(2) + "," + hiBattv.toFixed(2) + "," +
    lowSOC + "," + hiSOC + "," + maxAh.toFixed(1) + "," +
    hiPV.toFixed(1) + "," + hiLoad.toFixed(1) + "," +
    lowCellv.toFixed(3) + "," + hiCellv.toFixed(3) + "," + msg.payload.tempExt;
    
// Now reset the values in flow globals to start new highs and lows
flow.set("lowBattv",25);
flow.set("hiBattv",0);
flow.set("lowSOC",101);
flow.set("hiSOC",0);
flow.set("maxAh",0);
flow.set("hiPV",0);
flow.set("hiLoad",0);
flow.set("lowCellv",5);
flow.set("hiCellv",0);

return [msg,msg2];

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 February 2022 04:13 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/5 "2022-02-12T04:13:24Z")

</div>

the 30sec "Make CSV" log works because its getting its values directly from MQTT node

in the "Log and Reset Daily Stats" node we see at the top some variables being defined that get their values from Flow Context. We have to check in the Context window if the person that wrote the flow stores the battery temp there so we can use it.

Click the refresh button in Flow Context to see the values

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

---

<div class="post-metadata">

**Author:** ![PeterBC](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@PeterBC](https://discourse.nodered.org/u/PeterBC)\
**Post date:** [12 February 2022 23:40 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/6 "2022-02-12T23:40:25Z")

</div>

Bingo , I got it .. thanks..  
In the Battery Temp node I added the var and flow.set commands..  
Just wasnt familiar with this flow.set and flow.get bits ..  
Thaks for reading my questions and your responses proded me in the right direction !

```auto
var battTemp
tempC=msg.payload.tempExt;
flow.set("battTemp",tempC)
msg.payload=tempC;
return msg;

```

In the Log and Daily Stats bit I added

var battTemp = flow.get("battTemp")  
and on the line that outputs the daily line added the + battTemp

---

<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:** [26 February 2022 23:41 UTC](https://discourse.nodered.org/t/help-modifying-my-code/58184/7 "2022-02-26T23:41:27Z")

</div>

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