Convert part of a string to float and integer

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:

[{"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"}]

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

Yes of course :-

)

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

[{"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":[]}]

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

[{"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"]]}]

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

1 Like

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