# Convert part of a string to float and integer

**URL:** <https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935>\
**Category:** General\
**Created:** [5 January 2020 13:17 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935 "2020-01-05T13:17:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkyNet97](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@SkyNet97](https://discourse.nodered.org/u/SkyNet97)\
**Post date:** [5 January 2020 13:17 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935/1 "2020-01-05T13:17:50Z")

</div>

Hi guys, hope someone can give me a few hints.

I have 1xAM2320 temp and hum% sensor, and a soil moisture sensor connected to an arduino which in turn is connected to a Rpi.

My problem is that Im getting string messages, where part of the string needs to be converted to two floats for temperature and humidity%, and one integer for the soil mosture.

Once I have clean floats and integer, I will pipe it through to an influxdb/grafana setup.  
Please see my flow:

 ![2020-01-05 14_17_16-Window](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/44a5a07005e995f931d50d7bd843f724bf3448f6.png)

```auto
[{"id":"9b7b9696.f5d378","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"4343aeb7.bc99e","type":"serial in","z":"9b7b9696.f5d378","name":"arduino","serial":"af1e5ab1.636568","x":210,"y":360,"wires":[["10a84488.fd1f9b"]]},{"id":"10a84488.fd1f9b","type":"function","z":"9b7b9696.f5d378","name":"Buffer to String","func":"msg.payload = msg.payload.toString('hex');\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":360,"wires":[["f90cdb39.cb89c8","c2dabb6c.e0fff8"]]},{"id":"f90cdb39.cb89c8","type":"debug","z":"9b7b9696.f5d378","name":"STRENG","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":560,"y":220,"wires":[]},{"id":"cc46a99c.e1b098","type":"debug","z":"9b7b9696.f5d378","name":"TEMP","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":850,"y":180,"wires":[]},{"id":"c2dabb6c.e0fff8","type":"switch","z":"9b7b9696.f5d378","name":"switch splitter","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"temp","vt":"str"},{"t":"cont","v":"hum","vt":"str"},{"t":"cont","v":"smo","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":600,"y":360,"wires":[["cc46a99c.e1b098"],["7e283bef.1f63f4"],["d0e3c730.363738"]]},{"id":"7e283bef.1f63f4","type":"debug","z":"9b7b9696.f5d378","name":"HUM","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":850,"y":300,"wires":[]},{"id":"d0e3c730.363738","type":"debug","z":"9b7b9696.f5d378","name":"SMO","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":830,"y":420,"wires":[]},{"id":"af1e5ab1.636568","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

```

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [5 January 2020 14:24 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935/2 "2020-01-05T14:24:23Z")

</div>

Can you show what's coming out the Arduino node?

---

<div class="post-metadata">

**Author:** ![SkyNet97](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@SkyNet97](https://discourse.nodered.org/u/SkyNet97)\
**Post date:** [5 January 2020 14:43 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935/3 "2020-01-05T14:43:56Z")

</div>

Yes of course :-

 ![2020-01-05 15_43_23-Window](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6269e1d4a83d282f79cc6c44986ccadcd65fedd1.png) )

I found kind of a solution, where I use some split and change nodes. Not very elegant. Very open to suggestions.

```auto
[{"id":"ad41ef9.339ba1","type":"debug","z":"9b7b9696.f5d378","name":"ARDUINOOUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":350,"y":460,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [5 January 2020 14:48 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935/4 "2020-01-05T14:48:19Z")

</div>

Check this out to see if it works for you (I did not try your latest flow yet).

```auto
[{"id":"8bb63d14.26a9b","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"fd398c56.46158","type":"debug","z":"8bb63d14.26a9b","name":"TEMP","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":810,"y":320,"wires":[]},{"id":"3a450264.9841de","type":"switch","z":"8bb63d14.26a9b","name":"switch splitter","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"temp","vt":"str"},{"t":"hask","v":"hum","vt":"str"},{"t":"hask","v":"smo","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":600,"y":360,"wires":[["fd398c56.46158"],["f113c86c.ed3808"],["ce69960a.b00988"]]},{"id":"f113c86c.ed3808","type":"debug","z":"8bb63d14.26a9b","name":"HUM","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":810,"y":360,"wires":[]},{"id":"ce69960a.b00988","type":"debug","z":"8bb63d14.26a9b","name":"SMO","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":810,"y":400,"wires":[]},{"id":"e32deab.66adc18","type":"inject","z":"8bb63d14.26a9b","name":"","topic":"","payload":"hum: 48.80","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":340,"wires":[["432870b2.085a3"]]},{"id":"aec72c6b.7a8d4","type":"inject","z":"8bb63d14.26a9b","name":"","topic":"","payload":"smo: 0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":400,"wires":[["432870b2.085a3"]]},{"id":"432870b2.085a3","type":"function","z":"8bb63d14.26a9b","name":"Parse and trim","func":"let pay = msg.payload.split(\":\");\nmsg.payload = {[pay[0]] : parseFloat(pay[1].trim())};\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":360,"wires":[["3a450264.9841de"]]}]

```

---

<div class="post-metadata">

**Author:** ![SkyNet97](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@SkyNet97](https://discourse.nodered.org/u/SkyNet97)\
**Post date:** [5 January 2020 14:57 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935/5 "2020-01-05T14:57:22Z")

</div>

thank you, a lot more elegant and I avoid filtering away values that are 0 on the soil moisture.

---

<div class="post-metadata">

**Author:** ![regina2898](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/regina2898/32/23746_2.png) [@regina2898](https://discourse.nodered.org/u/regina2898)\
**Post date:** [29 May 2020 16:09 UTC](https://discourse.nodered.org/t/convert-part-of-a-string-to-float-and-integer/19935/6 "2020-05-29T16:09:58Z")

</div>

Hi, can you show your Arduino code? Thanks a lot
