TypeError: Cannot read properties of undefined (reading 'Total_in')"

Hi together,

i am trying to extract my mqtt data by a change as well as a function the value Power.Total_in.

This is extracted cleanly, at least according to the debug. Strangely, right after the change in the function comes the following error:

9.9.2023, 21:18:18node: function 1
function : (error) "TypeError: Cannot read properties of undefined (reading 'Total_in')"

Below is the configuration of the change:

image

and the function:

image

Does anyone have a solution for this?

Regards,
Tobias

The change node is setting the payload (and wiping out the Power_In property so when it gets to the function it’s no longer there as it’s now just in the payload

That worked perfectly. Thanks for the fast response. But now my functions not working properly.

I will explain my goal first:

I want to grab the meter reading I receive in the morning and write that value to the database. Then the values should be updated, but a difference should always be calculated to calculate the daily consumption of electricity.

I have developed the following 3 scripts to achieve this.

Script 1:

// Greifen Sie auf msg.payload zu, um den Wert zu extrahieren

var currentCounter = parseFloat(msg.payload);

msg.currentCounter = currentCounter;

return msg;

Script 2:

var currentDate = new Date().toISOString().split('T')[0]; // Das aktuelle Datum ohne Uhrzeit extrahieren

// Überprüfen Sie, ob bereits ein erster Zählerstand für heute im Kontext gespeichert ist
if (context.get('firstCounter_' + currentDate) === undefined) {
// Wenn nicht, speichern Sie den aktuellen Zählerstand als ersten Zählerstand des Tages
context.set('firstCounter_' + currentDate, msg.payload);
}
return msg;

Script 3:

// Greifen Sie auf msg.payload zu, um den Wert zu extrahieren

var currentCounter = parseFloat(msg.payload);

// Speichern Sie den aktuellen Zählerstand in einer Flussvariable (flow context)

var previousCounter = flow.get("previousCounter") || 0;

// Berechnen Sie den Tagesverbrauch

var dailyConsumption = currentCounter - previousCounter;

// Speichern Sie den aktuellen Zählerstand als vorherigen Zählerstand für die nächste Berechnung

flow.set("previousCounter", currentCounter);

// Fügen Sie den Tagesverbrauch zur Nachricht hinzu, wenn gewünscht

msg.dailyConsumption = dailyConsumption;

return msg;

Unfortunately, my plan doesn't seem to be that easy to implement or I think just too hard.

Can you help me here as well? I'm an absolute beginner with NodeRed (since 2 days in use) because I wanted to try to make my PV yield or power consumption visible by means of Grafana.

Regards,
Tobias

It would be much easier to debug if you post the actual flow here -

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

See this post for more details - How to share code or flow json

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