Turn payload numbers into integer

Hi everyone,

I probably have a really simple request - here is the use case. I receive temperature values ​​from my sensors, which I can call up through Alexa when needed. The problem now is, that these values have two decimal places, which makes it harder to understand when transmitted through Alexa speak. Therefore I´d like to truncate the payload of temperature values to integers that I can then use to create the message text. Anyone know if there is a simple solution for that?

Hi .. you could use javascript's parseInt() in a function node or in a Change Node use the $round method of a Jsonata expression.

Example Flow :

[{"id":"f38d0c47e9c4c7e5","type":"inject","z":"f6e161f3e6088354","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23.567","payloadType":"num","x":550,"y":500,"wires":[["74b61bddae0cd3d0","58ebd0ca3367f9d2"]]},{"id":"74b61bddae0cd3d0","type":"function","z":"f6e161f3e6088354","name":"parseInt","func":"msg.payload = parseInt(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":460,"wires":[["60668ef870bd270b"]]},{"id":"60668ef870bd270b","type":"debug","z":"f6e161f3e6088354","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":460,"wires":[]},{"id":"58ebd0ca3367f9d2","type":"change","z":"f6e161f3e6088354","name":"jsonata round","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(payload, 0)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":560,"wires":[["e1bbbd3db019d854"]]},{"id":"e1bbbd3db019d854","type":"debug","z":"f6e161f3e6088354","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":560,"wires":[]}]

There are multiple ways msg.payload.toFixed() will return a string with the value set to interger
here are a couple of other ways

[{"id":"d91c7fc3.fce0c8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.23","payloadType":"num","x":100,"y":1480,"wires":[["aa0748cd.3d8a9","54b59e1e.722fa","7eb45134.6f573"]]},{"id":"aa0748cd.3d8a9","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":1480,"wires":[["896fbba5.052b98"]]},{"id":"54b59e1e.722fa","type":"function","z":"c74669a0.6a34f8","name":"","func":"msg.payload = parseInt(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":1520,"wires":[["896fbba5.052b98"]]},{"id":"7eb45134.6f573","type":"function","z":"c74669a0.6a34f8","name":"","func":"msg.payload =msg.payload.toFixed()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":1560,"wires":[["896fbba5.052b98"]]},{"id":"896fbba5.052b98","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":1640,"wires":[]}]

it all depends if you want a string return or a number.

Thanks much @E1cid and @UnborN for the quick solution :slight_smile:

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