Adding variable values

I have struggled all afternoon with something which shouldn't be hard.
Have had a look around and seen some similar queries but not quite the same.

My problem is I am trying to add two variables together but the answer seems to be like concatenating the two values.

I am on a RasPi
Node-Red is version 1.2.9
Node.js is version 12.21.0

My code is this...

var temp_vala = msg.payload.rows[0].daily_v;
var temp_valb = msg.payload.rows[0].another_v;

var total = temp_vala + temp_valb;

I have tried wrapping the msg.payload with {} and {} and () and '{}' plus more combinations, equally I have tried wrapping the addition with a bunch of possibilities like ([temp_vala] + [temp_valb]) and a whole bunch of combinations of {} and [] and () with and without and I just can't see how to get to the value instead of the string value.

Several of the attempts gave me the concatenated answer instead of an addition of the two raw values.

My values in msg.payload are whole numbers, so I thought it would be really easy.

Sorry this is again a really basic question. Google and YouTube have not been my friend so far.

Possibly your msg.payload.rows[0].daily_v; and msg.payload.rows[0].another_v;
are strings to begin with and so they are not added as Numbers

You can check by adding a Debug node before your function node and checking in debug sidepanel.

Example :

Value1 is String. Value2 is number

If indeed your values are Strings your could convert them to Number by wrapping them around the Javascript Number()

var temp_vala = Number(msg.payload.rows[0].daily_v);
var temp_valb = Number(msg.payload.rows[0].another_v);

var total = temp_vala + temp_valb;

Thank you so much, adding Number before the (msg.payload) fixed the problem in about 2 minutes.
I appreciate your assistance.
They were numbers coming out of a database but for some reason they were being seen as strings.
Problem now solved :slight_smile:

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