Basic question, Is there a way to return only first 3 characterss of a function output?

1,456 views
Skip to first unread message

Dave B

unread,
Dec 30, 2016, 8:07:07 PM12/30/16
to Node-RED
So I have an output from a sensor which is in C and it seems the easist way to convert to F was just to add a function.  In the function equation it outputs the correct data (sort of).  The sensor input is 19 and after the below code I'm getting { "topic": "Temp sensor", "payload": 69.80000000000001) so as you can see my output is quite long.  I'd like to truncate to 3 digits if possible but I'm not sure how I can do that.  Also with that, is there a way to have the gauges display 3 digits instead of just two when the digit to the right of the decimal is zero?  IE 69.0 when the output is 69 so it is a standardized output even if the the # to the right of the decimal is a zero.  



Here is my code
var T =  msg.payload * 1.8 + 32
msg.payload = T;
return msg;



Zenofmud

unread,
Dec 30, 2016, 8:19:33 PM12/30/16
to node...@googlegroups.com
Try rounding like this
T  = Math.round(T * 10) / 10;
--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/6e7d3c6c-08e9-4dc4-81f4-f4d456a25b62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aidan

unread,
Dec 30, 2016, 8:57:14 PM12/30/16
to Node-RED
Try this

[{"id":"1865018d.98acce","type":"inject","z":"32f4188a.074c98","name":"","topic":"","payload":"1.4565466654","payloadType":"num","repeat":"","crontab":"","once":false,"x":600.8957977294922,"y":47.666656494140625,"wires":[["6341d7b9.ffc138"]]},{"id":"cc5787a0.53b088","type":"debug","z":"32f4188a.074c98","name":"","active":true,"console":"false","complete":"payload","x":1000.8959197998047,"y":51.666656494140625,"wires":[]},{"id":"6341d7b9.ffc138","type":"function","z":"32f4188a.074c98","name":"Math.round","func":"msg.payload = Math.round(msg.payload * 10) / 10\nreturn msg;\n\n\n//msg.payload = {\n//out: Math.round(msg.payload * 10) / 10}\n//return msg;","outputs":1,"noerr":0,"x":805.8957977294922,"y":54.888885498046875,"wires":[["cc5787a0.53b088"]]}]

Colin Law

unread,
Dec 31, 2016, 3:25:21 AM12/31/16
to node...@googlegroups.com
On 31 December 2016 at 01:07, Dave B <cfb...@gmail.com> wrote:
> So I have an output from a sensor which is in C and it seems the easist way
> to convert to F was just to add a function. In the function equation it
> outputs the correct data (sort of). The sensor input is 19 and after the
> below code I'm getting { "topic": "Temp sensor", "payload":
> 69.80000000000001) so as you can see my output is quite long.

Strictly speaking the payload is not any longer than if it said
"payload": 69.9. The payload is a number not a string and it takes
exactly the same number of bytes to store the three digit number as
the longer one. Do not worry about the number of digits at this
point. It is when you come to display it that you should consider to
what precision you wish to see it, and format it appropriately.

Colin

Dave C-J

unread,
Dec 31, 2016, 4:11:20 AM12/31/16
to node...@googlegroups.com

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Nicholas O'Leary

unread,
Dec 31, 2016, 4:32:57 AM12/31/16
to Node-RED Mailing List
Dave B,

to pick up on Colin's point, yet another option you have is to leave the data alone, and just configure the gauge to display the value as you want.

The 'Value Format' property of the gauge lets you determine how the value is displayed. You can use angular directives in that field to apply formatting. In this instance there is the 'number' directive that can do exactly what you want: https://docs.angularjs.org/api/ng/filter/number

So if you set the 'Value Format' field to {{ value | number: 2}} you'll get what you want, without having to modify the underlying data.

Nick



To post to this group, send email to node...@googlegroups.com.
Message has been deleted

Dave B

unread,
Jan 1, 2017, 5:25:42 AM1/1/17
to Node-RED
Thanks guys, I am displaying it which is what the problem is.  I'll try these out and get back to you!

Colin Law

unread,
Jan 1, 2017, 5:33:44 AM1/1/17
to node...@googlegroups.com
I don't know what causes that error, but will just note that
re-installing is almost never the best solution when something is
screwed up. It is generally better to try and sort the problem out.
Unless you are talking about Windows of course.

Colin

On 1 January 2017 at 08:08, Dave B <cfb...@gmail.com> wrote:
> Thanks guys, I am displaying it which is what the problem is. I was using a
> DHT11 temp/humidity sensor. Unfortunately I screwed something up last night
> and had to reinstall from scratch and now when I try to use the DHT22/11
> node I get the error below.
>
> [quote]Jan 1 02:04:16 pi Node-RED[399]: bcm2835_init: Unable to open
> /dev/mem: Permission denied
> Jan 1 02:04:16 pi Node-RED[399]: 1 Jan 02:04:16 - [error]
> [rpi-dht22:c5f9f92e.1fc9c8] TypeError: failed to initialize
> [/quote]
>
> Out of curiousity I tried to chmod 777 /dev/mem (I know, this was just a
> test) and got the following:
>
> [quote]Jan 1 02:05:49 pi Node-RED[399]: bcm2835_init: Unable to open
> /dev/mem: Operation not permitted
> Jan 1 02:05:49 pi Node-RED[399]: 1 Jan 02:05:49 - [error]
> [rpi-dht22:c5f9f92e.1fc9c8] TypeError: failed to initialize
> [/quote]
>
> Using the Adafruit scripts I can read the sensor just fine from the pin so
> I'm lost. I got in over my head for a beginner I think.
> Dave
>
> On Friday, December 30, 2016 at 7:07:07 PM UTC-6, Dave B wrote:
>>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/da56fbd5-8fbc-4f60-a9b4-fabe7d2b1dba%40googlegroups.com.

Dave B

unread,
Jan 1, 2017, 5:51:05 AM1/1/17
to Node-RED

Thanks guys!  Paul that seemed to work but it brings me back to the question of standardizing the output.  There is one point where w/ my equation the # is exactly 68 so it just shows 68 not 68.0.  I suppose it's not a huge deal but is there a way I can somehow tell it to report back one digit to the right of the decimal point regardless of it being 0 or not?

If not, it's not a huge deal, I'm just curious.  
BTW this is what I was seeing before:

Dave B

unread,
Jan 1, 2017, 5:55:54 AM1/1/17
to Node-RED
Thanks Colin I finally managed to resolve that issue as well.  I see that when you use the -g switch it puts it in a different directory than not using it so I went in and removed all of the nodes referencing that device and reinstalled.  I'm good to go again.  Still in the very early stages here.  Now i need to see if I can figure out bi directional communication w/ UI nodes so my UI switches follow the actual status of the GPIOs. .

Colin Law

unread,
Jan 1, 2017, 6:20:30 AM1/1/17
to node...@googlegroups.com
Show us the code that is displaying it.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nicholas O'Leary

unread,
Jan 1, 2017, 6:32:21 AM1/1/17
to Node-RED Mailing List
Dave,

I assume you're working through the response you received on this. I would suggest you jump to my response - and set your gauge's Value Format property to  {{ value | number: 2}} and don't do anything else to the data value itself. That does everything you are asking for.

Nick



Dave B

unread,
Jan 3, 2017, 12:24:11 PM1/3/17
to Node-RED
Sorry Nick I was trying to keep track of this thread.  I will try your suggest as well as that's an easier way w/o having to have added math in there.

Dave
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nils Terje Haavi

unread,
Dec 28, 2017, 9:32:35 AM12/28/17
to Node-RED
Easyest way to do this is by the Value field:  {{value | number:2 }}

Dave B

unread,
Dec 28, 2017, 12:05:29 PM12/28/17
to Node-RED

Thanks I had stepped away from this for some time.  I forgot what I did to resolve it but I’m ok now.


Dave

Dave B

unread,
Dec 28, 2017, 12:13:35 PM12/28/17
to Node-RED
FYI, this is what I did:  Think there is a more efficient way?
var Temp =  msg.payload * 1.8 + 32;
Temp  = Math.round(Temp * 10) / 10;
msg.payload = Temp;

Julian Knight

unread,
Dec 29, 2017, 6:38:56 AM12/29/17
to Node-RED
Well you could remove the temp variable but honestly, that makes little difference. So yes, that would seem to be about right.
Reply all
Reply to author
Forward
0 new messages