Help modifying my code

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.

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

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

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

Share the message (using image) explaining where its coming from.

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


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

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];

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

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 !

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

1 Like

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