# TypeError: Cannot read properties of undefined (reading 'Total\_in')"

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165>\
**Category:** General\
**Tags:** database, function-node\
**Created:** [9 September 2023 19:24 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165 "2023-09-09T19:24:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobi31](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobi31/32/82700_2.png) [@Tobi31](https://discourse.nodered.org/u/Tobi31)\
**Post date:** [9 September 2023 19:24 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165/1 "2023-09-09T19:24:46Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5fd432416c90085358c6fc1af56d644ee317b5c.png)

and the function:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/76b41019e4d81242e0b0f096a6cdc4e0e8024d6b.png)

Does anyone have a solution for this?

Regards,  
Tobias

---

<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:** [9 September 2023 21:47 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165/2 "2023-09-09T21:47:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tobi31](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobi31/32/82700_2.png) [@Tobi31](https://discourse.nodered.org/u/Tobi31)\
**Post date:** [9 September 2023 22:38 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165/3 "2023-09-09T22:38:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [9 September 2023 23:18 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165/4 "2023-09-09T23:18:44Z")

</div>

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 `````)

````auto
``` 
   code goes here 
```

````

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [8 November 2023 23:19 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-total-in/81165/5 "2023-11-08T23:19:00Z")

</div>

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