# Why am I getting the wrong \*type\* of data in this message?

**URL:** <https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830>\
**Category:** General\
**Created:** [23 April 2023 04:58 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830 "2023-04-23T04:58:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 04:58 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/1 "2023-04-23T04:58:07Z")

</div>

I am TRYING to test some code for weather (rain) detection.

Originally it wasn't this smart, but now I am wanting to improve it.

I've cut down a lot of stuff and tried to cut it to what is THE problem.

If it is raining part of the message exists and it is parsed.  
If it isn't raining that part doesn't exist and a _dummy_ is made. (The value is set to `0`)  
That is then sent on and rounded to 1 decimal place - yeah: deal with the numbers as given - and stored (flow context)  
If the _flow.MODE_ is NOT _B_ then it adds the values.  
That was painful as `+` was being done as _stick to the end of_ rather than ADD.  
This was because the values were _TEXT_ rather than numbers.

I got around that problem and it started to work.  
Then it stopped and now doesn't seem to want to play the game.

No matter what I do I am NOT getting NUMBERS out of the `function` node in `msg.rain_now`  
The top `function` node after the `switch` node.

Ok, I could deal with the entire value given and _tidy it up_ later on for display purposes.  
That is a whole problem unto itself.

So TRYING to get it working:  
Why is it that `msg.rain_now` is TEXT and not NUMBER?

```auto
[{"id":"89c34e754b89497e","type":"inject","z":"05319bbbba06da29","name":"0.818181","props":[{"p":"data.rain[\"1h\"]","v":"0.818181","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":470,"y":810,"wires":[["4da4a97fbc646912"]]},{"id":"37a2bea6f1a3d08f","type":"inject","z":"05319bbbba06da29","name":"0.1616161616","props":[{"p":"data.rain[\"1h\"]","v":"0.1616161616","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":480,"y":860,"wires":[["4da4a97fbc646912"]]},{"id":"4da4a97fbc646912","type":"switch","z":"05319bbbba06da29","name":"Raining?","property":"data.rain[\"1h\"]","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":925,"y":910,"wires":[["bbf430450a163bba","f082cb9f8d1294b0","61d5dac37fcb1773"],["90d76570e9d18b83","52c64f8853124708"]],"l":false},{"id":"61d5dac37fcb1773","type":"function","z":"05319bbbba06da29","name":"Set `rain_now` to value.","func":"let rain_now = parseFloat(msg.data.rain[\"1h\"]).toFixed(1);\nmsg.rain_now = rain_now;\n\nnode.warn(\"Rain now \" + rain_now);\n//msg.something = 0.6;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1005,"y":910,"wires":[["2b5173aa2b316920","0b369a7bec8d4350"]],"l":false},{"id":"52c64f8853124708","type":"function","z":"05319bbbba06da29","name":"No rain detected","func":"msg.rain_now = 0;\nmsg.payload = 0;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1005,"y":950,"wires":[["0ad56c2f26fe2166"]],"l":false},{"id":"2b5173aa2b316920","type":"switch","z":"05319bbbba06da29","name":"==B?","property":"mode","propertyType":"flow","rules":[{"t":"eq","v":"B","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1075,"y":910,"wires":[["13659bc680d6437d"],["0ad56c2f26fe2166"]],"l":false},{"id":"0ad56c2f26fe2166","type":"junction","z":"05319bbbba06da29","x":1090,"y":1010,"wires":[["4f3f2b25c2f8d6d6"]]},{"id":"04eeefaf3ac83cdd","type":"function","z":"05319bbbba06da29","name":"Store rain.","func":"// If `reset` message received.\nif (msg.reset == \"reset\")\n{\n msg.rain_now = flow.get(\"rain\");\n return msg;\n}\n\nlet todays_rain = parseInt(flow.get(\"rain\")) || 0;\n//node.warn(\"Today's rain is \" + todays_rain);\n//node.status({ text: todays_rain });\n\nlet rain_now = parseInt(msg.rain_now);\n\n//todays_rain = todays_rain + rain_now;\n//todays_rain = parseFloat(todays_rain + rain_now).toFixed(1);\ntodays_rain = todays_rain + rain_now;\n\nnode.warn(\"Adding rain totals together\");\nnode.warn(\"rain now \" + rain_now);\nnode.warn(\"Today's rain \" + todays_rain);\n\nflow.set(\"rain\",todays_rain);\n\nnode.status({ text: todays_rain });\n\nmsg.payload = todays_rain;\nmsg.topic = null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1380,"y":1010,"wires":[["ea17db4b610c2bca","f70d474fa4e6a312","0a7ddf199d71ddce","0543926b6e610c6f"]]}]

```

This is a rewrite of what I had and worked.  
Well, it worked in that values seemed to be passed correctly.

But to _simplify_ things, I moved things around and processed the original message to give me a usable message.  
This becomes/became `msg.rain_now` and it a `number` with 1 needed decimal place accuracy.  
From here on it is only one message that needs to be handled and if/when a new message arrives, the _context_ value is added then stored back to the _context_.  
If/when the _ONE ONLY_ message arrives requesting the value, it is taken and then on-sent.

Should be easy, but I am getting `text` rather than `number` values sent.  
As I'm already down this rabbit hole, going back to the older way - which had it's own set of problems - I would prefer to get this working.

So although you are seeing something that doesn't work - why would I ask for help if it did work? - I hope you can see what it is and HOW I want to do it.

Again: Thanks.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [23 April 2023 05:52 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/2 "2023-04-23T05:52:18Z")

</div>

```auto
//with this you'll get a string 
// because parseFloat(msg.data.rain["1h"]) returns a number
// and then you make it a string by .toFixed(1)
let rain_now = parseFloat(msg.data.rain["1h"]).toFixed(1);

node.warn("rain_now is "+ typeof rain_now)

// to get a number
let rain_now_as_number = parseFloat(msg.data.rain["1h"].toFixed(1));
node.warn("rain_now_as_number is " + typeof rain_now_as_number)

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 06:40 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/3 "2023-04-23T06:40:53Z")

</div>

Indulge me this question after I've said that was the solution:

This is what I am seeing - and things are good.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82258c089508a00af7b091bf6e3d8c0c389e187a.png)

I have sent a value from one of the two `inject` nodes.  
`0.81818181`  
That stored `0.8` and then I sent the value again.

That gives me the `1.6` which is shown at the bottom as the `payload`.

I then inject a third `0.81818181` and this happens.

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

`2.4000000000000004`

ARGH!

Ok, it is ONLY a value and it makes no difference at the end of the day.

But I've "tidied up" the values so they are only 1 digit (after decimal) correct.  
So HOW does the `0000000000004` get involved?

I'm guessing `float` has something to do with it.

P.S.

I **CHECKED** the flow.context and it was `1.6` and not.... `1.600000000000000000000` or anything like that.  
The THIRD injection showed `0.8` going through the nodes.  
So.... As much as I admit I suspect `float` doing it: I don't see how.  
There is no _error_ there to start with to invoke the `00000000000004` after the third injection.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [23 April 2023 07:05 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/4 "2023-04-23T07:05:53Z")

</div>

If you're after siencific answer, do google something like "JavaScript floating point precision"

But actually you shouldn't care about it that too much.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 07:12 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/5 "2023-04-23T07:12:48Z")

</div>

I have read things like that.

Yes, I get it that I SHOULDN'T let it get to me.

Alas: It does. Not seriously, but I just don't get it.  
I'll go and read it again to try and put myself at ease.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 07:13 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/6 "2023-04-23T07:13:04Z")

</div>

> [@Trying\_to\_learn](#):
>
> HOW does the `0000000000004` get involved?

It is called floating point rounding error. Float values are stored with a finite number of bits (because computers so far at least, don't have an infinite number of bits) so often numbers are not held or calculated accurately. You said yourself in your other thread that 20/3 is 6.666666666667, but that is not correct. The sixes should go on for ever. Whenever you do a calculation there is the possibility of a 1 bit error. The moral is _don't_ reduce the precision of numbers until you absolutely need to. Keep them as numbers (not strings) to full precision and only round them when you need to display them.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 07:18 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/7 "2023-04-23T07:18:50Z")

</div>

(all)

So one way I could _get around_ this problem is:  
(Please indulge me here)

Get the original payload  
multiply it by `10`.  
convert it to an integer. (gets rid of all after the decimal point)  
do all my _magic stuff_ with that number.  
When displaying it: divide the number by `10` and `.toFixed(1)` to give it a fixed (forced) decimal accuracy.

As that converts it to a string (late with being told that - another example didn't exactly show/declare that when they showed it - another story) thing should then show correctly.

Would that work?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 07:25 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/8 "2023-04-23T07:25:44Z")

</div>

> [@Trying\_to\_learn](#):
>
> convert it to an integer. (gets rid of all after the decimal point)  
> do all my _magic stuff_ with that number.

I haven't looked at your magic stuff, but does it need to be converted to an integer for that to work?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 07:31 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/9 "2023-04-23T07:31:30Z")

</div>

Well, (say) I am getting values like:  
`1.2345`  
and `2.3456`  
I don't want/need anything more than either `1.2` or `2.3`.  
Their total doesn't matter if it is `3.3` or `3.4`

As I am wanting to allow `0.1` _accuracy_ I need to keep the first number after the decimal point.

So if I `*10` the value then `parseInt()` that value: that would then give me (from the above example) `12` or `23` - yes?  
Then I have the right number of digits after the decimal point included in my stuff/magic.

So as long as I allow from the value being ten times bigger, I can allow for that with any downstream maths I want to do.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 07:39 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/10 "2023-04-23T07:39:10Z")

</div>

> [@Trying\_to\_learn](#):
>
> As I am wanting to allow `0.1` _accuracy_ I need to keep the first number after the decimal point.

Don't do that until later. Just add them up (if that is what you are doing) which will give 3.5841, then when you come to display it do the rounding, to give you 3.6

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 07:45 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/11 "2023-04-23T07:45:59Z")

</div>

Yes, I understand that is _THE_ way to do it.  
All the stuff happens _under the bonnet_ and the user only needs to see the formatted stuff.

I am doing this only as an exercise in ....... making my life difficult? 😉  
how to constrain inputs to limits.

So if I am getting _squillion digit_ inputs and only need _limited digit_ inputs: how do I do it.  
Yes, this does directly arc back to what was first said.  
But to learn how to constrain things it seems like a good idea.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 08:12 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/12 "2023-04-23T08:12:13Z")

</div>

> [@Trying\_to\_learn](#):
>
> So if I am getting _squillion digit_ inputs and only need _limited digit_ inputs: how do I do it.

You ignore it till you are about to display it and then used `toFixed(1)` or {{msg.payload | number: 1}} where appropriate.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 08:14 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/13 "2023-04-23T08:14:51Z")

</div>

Yes, we've agreed that is the way to do it.

But I am wanting to learn how to constrain inputs.

_This all may have been brought on because I am learning how to use CAD programs_.  
_Dunno._  
But the word CONSTRAINT is sticking in my vocabulary just now and what it means.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 08:19 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/14 "2023-04-23T08:19:29Z")

</div>

> [@Trying\_to\_learn](#):
>
> But I am wanting to learn how to constrain inputs

What do you mean by constrain inputs? Do you mean operator inputs on the dashboard?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 08:24 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/15 "2023-04-23T08:24:45Z")

</div>

I am not sure what I mean.

But if i am ~~writing a `function` node~~ using a `function` node and have things need to be parsed/checked they are of certain ..... _things_.......

And `subflows` - which I am sure you know I seem to like. 😉

Maybe (probably) I am just hung up on the word `constrain`.  
But as it seems to be just now: I am for some reason stuck on this thing.  
Sorry if it is not _nice_ for others. But I think this group is a good place to learn about structures in languages than any other.  
So alas I am asking here.  
Also because _we_ all talk javascript.  
So it is a base line that can be used to establish things.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 08:48 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/16 "2023-04-23T08:48:36Z")

</div>

Can you give a real example of where you need to constrain something to fixed digits other than when it is being displayed?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 08:51 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/17 "2023-04-23T08:51:56Z")

</div>

Alas not really.

I shall have to resign myself to the theory that I am just hung up on it.

This scenario probably triggered/tripped me over the threshold of needing to ask the question.  
Badly formed and poorly asked.

I was just wanting to _tidy up_ some stuff and make the flow happen smoother than it was.  
Believe me: It was a lot worse than it is now.

And while doing it the problem landed in my lap on how to do it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 08:59 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/18 "2023-04-23T08:59:42Z")

</div>

If you have not got a real example then how is it a problem.

But if you do have a number and want to take it to the nearest value with fixed precision, but keep it as a number, then the easiest way is probably  
`const rounded = (Math.round(value * 10))/10`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 09:06 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/19 "2023-04-23T09:06:15Z")

</div>

It was more a question because I was trying something to _simplify_ something else.

Yes, I make my own life harder than it needs to be. This is a good day.

That little _trick_ you posted may be of interest.  
My working solution was (for this example posted above) was to do this:  
Going into the part of the flow.

```auto
let rain_now = parseInt(msg.data.rain["1h"]*10);
msg.rain_now = rain_now;

return msg;

```

Coming out I just reversed it.

I'll have to look more at/into what `Math.round()` does.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2023 09:09 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/20 "2023-04-23T09:09:32Z")

</div>

> [@Trying\_to\_learn](#):
>
> `let rain_now = parseInt(msg.data.rain["1h"]*10);`

I think that probably converts it to a string and back again, which isn't very efficient.

[Next page](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830.md?page=2)
