# How can I work with ioBroker-variables in Node-red function?

**URL:** <https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884>\
**Category:** General\
**Created:** [8 December 2021 16:17 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884 "2021-12-08T16:17:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonasBu](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JonasBu](https://discourse.nodered.org/u/JonasBu)\
**Post date:** [8 December 2021 16:17 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/1 "2021-12-08T16:17:03Z")

</div>

Hi  
I want to read sensor-data, store these values in iobroker and later i want to sum the variables in a function-node.

My function code:

```auto
var dcPower = env.get("msg.dc.power");
var wallbox1Power = env.get("msg.wallbox1.power");
var wallbox2Power = env.get("msg.wallbox2.power");
var sum = dcPower;
msg.payload;
return msg;

```

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 December 2021 16:33 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/2 "2021-12-08T16:33:30Z")

</div>

On the bottom flow, is a debug node to determine where the value arrives. Assuming it is msg.payload, use a change node to copy or move this to their own msg property e.g. `msg.val1` `msg.val2`. When the msg arrives at the function node, you will be able to access all the values at the same time from the msg properties you set in the change nodes.

---

<div class="post-metadata">

**Author:** ![JonasBu](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JonasBu](https://discourse.nodered.org/u/JonasBu)\
**Post date:** [8 December 2021 16:54 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/3 "2021-12-08T16:54:16Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/065bff5dde0d6dffb7a9d74e2db4a2439fd95ad6.png)

Thanks for your answer.  
I set the attribute i the "ioBroker get"-node. In the picture you can see that the variables are stored in the function node, but the sum or payload are empty.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 December 2021 17:01 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/4 "2021-12-08T17:01:25Z")

</div>

It looks like the values are already in some other msg property. Use the copy path button to grab the path to the value and use that with `msg.` instead of `env.get` (env.get is for environment vars not for accessing msg properties)

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

* * *

Ps, it may be necessary to use parseInt or parseFloat to coerce the string values into numbers before doing math operations.

---

<div class="post-metadata">

**Author:** ![JonasBu](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JonasBu](https://discourse.nodered.org/u/JonasBu)\
**Post date:** [8 December 2021 17:14 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/5 "2021-12-08T17:14:09Z")

</div>

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

I copied the path as described above, but that changes nothing.  
Are there other options?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 December 2021 17:24 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/6 "2021-12-08T17:24:27Z")

</div>

parseInt needs round (not square) brackets.

> **[parseInt() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)**
>
> The parseInt() function parses a string argument and
> returns an integer of the specified radix (the base in mathematical numeral
> systems).

Also, don't put msg.xxxx in quotes.

---

<div class="post-metadata">

**Author:** ![JonasBu](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JonasBu](https://discourse.nodered.org/u/JonasBu)\
**Post date:** [8 December 2021 17:27 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/7 "2021-12-08T17:27:40Z")

</div>

I tried. Than I got the message:  
"TypeError: Cannot read property 'power' of undefined"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 December 2021 17:37 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/8 "2021-12-08T17:37:35Z")

</div>

Try `msg["dc.power"]`

The property name has a `.` in it (`power` is not a sub property of `dc`)

This is known as square bracket notation

---

<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:** [7 January 2022 17:31 UTC](https://discourse.nodered.org/t/how-can-i-work-with-iobroker-variables-in-node-red-function/54884/9 "2022-01-07T17:31:26Z")

</div>

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