# Basic adding (sum) of var values/numbers using function node

**URL:** <https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046>\
**Category:** General\
**Tags:** function-node, home-assistant\
**Created:** [14 October 2022 21:36 UTC](https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046 "2022-10-14T21:36:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GitHubxtsnl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/githubxtsnl/32/34607_2.png) [@GitHubxtsnl](https://discourse.nodered.org/u/GitHubxtsnl)\
**Post date:** [14 October 2022 21:36 UTC](https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046/1 "2022-10-14T21:36:22Z")

</div>

I've been testing for hours now and just cannot seem to find a solution. I'm missing something but just do not get it. Please assist (I'm a beginner) with Node-red.

I want to calculate the total costs, so add up of all 4 values:

```auto
const globalHomeAssistant = global.get('homeassistant');
var dag = globalHomeAssistant.homeAssistant.states["sensor.stroomverbruik_maandelijks_dag"].state;
var nacht = globalHomeAssistant.homeAssistant.states["sensor.stroomverbruik_maandelijks_nacht"].state;
var gas = globalHomeAssistant.homeAssistant.states["sensor.monthly_gas_consumption"].state;
var water = globalHomeAssistant.homeAssistant.states["sensor.monthly_water_consumption"].state;

var water1 = Number(water * 0.604 + 46.04).toFixed(2); --this has value 46.33
var stroom1 = Number(dag * 0.14551).toFixed(2); --this has value 2.44
var stroom2 = Number(nacht * 0.13343).toFixed(2); --this has value 6.53
var gas1 = Number(gas * 0.75516).toFixed(2); --this has value 1.32

msg.payload_totale_kosten = (stroom1+stroom2+gas1+water1);

return msg;

```

but this gives me a result of: **2.446.531.3246.33**  
it is not a sum but just all number next to each other. I've tried many things, but none seem to work.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 October 2022 22:00 UTC](https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046/2 "2022-10-14T22:00:25Z")

</div>

Hi  
toFixed() returns a string.  
So `stroom1+stroom2+gas1+water1` concatenates the strings.  
If you require the addition of 2 decimal place numbers use `Math.round(Number(water) * 100) / 100`.

---

<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:** [14 October 2022 22:09 UTC](https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046/3 "2022-10-14T22:09:21Z")

</div>

To add to what @E1cid says, dont reduce precision in the function. Only trim the decimal places when you view the result.

For example, get rid of all the `.toFixed()` functions, get the accurate result of the summation then (and only then) if you want to display the value in a friendly format do you reduce the precision.

If you want to display a value on the dashboard with reduced decimal places, you can use angular filters to make the value appear "nice". (Search the forum).

---

<div class="post-metadata">

**Author:** ![GitHubxtsnl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/githubxtsnl/32/34607_2.png) [@GitHubxtsnl](https://discourse.nodered.org/u/GitHubxtsnl)\
**Post date:** [15 October 2022 08:13 UTC](https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046/4 "2022-10-15T08:13:06Z")

</div>

Thanks all for the tips. I have it working now.

---

<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:** [14 December 2022 08:13 UTC](https://discourse.nodered.org/t/basic-adding-sum-of-var-values-numbers-using-function-node/69046/5 "2022-12-14T08:13:42Z")

</div>

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