NodeRed round to whole number

Hi,

I am trying to use a function node to round a number to a whole number, for example a temperature of 22.3 degrees, needs to round to 223, so in essence removing the decimal.

I have tried change and function nodes, but can only remove the decimal place and the numbers that follow, i end up with 22 degrees, rather than 223.

Thanks.

Multiply with 10.

msg.payload = msg.payload *10

There is probably a node that can do it also but function node is practical for a lot of thing.

Edit:

What is the value when it’s an integer?
Does it say 22 or 22.0?

1 Like

Convert it to a string, remove the decimal place and set it back to an integer.

msg.payload = parseInt(msg.payload.toString().replace(".",""))

1 Like

This worked - thanks!

This method may cause errors if there is the possibility of 2 decimal places.

For example:

22.3 * 10 = 223
22.35 * 10 = 223.5

You can use replace to remove the decimal

$number($replace(($entity().state), '.', "", 1))	
2 Likes

Thanks, i noticed this. I do however have another problem. I am trying to convert Mph to meters per second, as follows, but this requires an additional calcuation.

I have tried to convert Mph to MPS, and then remove the decimal, however this does not work.

Here is my current flow:

[{"id":"f71f5801e1d5e28d","type":"inject","z":"a3a21d2e40b021ad","name":"Injects 1.2 MPH","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.2","payloadType":"num","x":100,"y":120,"wires":[["d22ad07560735065"]]}]

So you see, 1.2 miles per hour, in meters per second should be 0.53, however when i try and remove the decimal, i get 1519 which is obviously not correct.

I am obviously missing something here…

Sure it’s not 5.3 m/s?

I haven’t imported your “flow” but looking at the string, it seems you only exported the inject.

Sorry it seems google convert is wrong.

When I google it I get 0.53 m/s

The sensor is giving our a value of 002.12 miles per hour, which i need to convert to meters per second, without a decimal place.

For example, 1mph is 0.44704 meters per second. The final outcome needs to be 044704 (or better still, 045).

Thanks.

It’s going to be hard to send a number that starts with 0 without a decimal. As soon as it’s cast to a number it drops the leading zero.

[{"id":"baf409c9.fa41f","type":"inject","z":"f80b6c338afd5483","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"002.12","payloadType":"num","x":850,"y":760,"wires":[["33fffafac0324657"]]},{"id":"33fffafac0324657","type":"change","z":"f80b6c338afd5483","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round((payload / 2.237), 3)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$string(payload)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$replace(payload, '.', \"\")","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$parseInteger(payload, '0###')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":840,"wires":[["0130417f024d8c56"]]},{"id":"0130417f024d8c56","type":"debug","z":"f80b6c338afd5483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1390,"y":620,"wires":[]}]

Is there any way i can prevent numbers higher than 3 digits?

The range needs to be anything from 0-600.

Thanks.

Math.min(msg.payload, 600)

This will take the minimum value of the payload or 600.

So if the payload is 300 the value will be 300, if its 609 it’ll be 600

Here is my setup.

[{"id":"a3a21d2e40b021ad","type":"tab","label":"Temp Data","disabled":false,"info":"","env":[]},{"id":"c3a1f05e884b85c0","type":"function","z":"a3a21d2e40b021ad","name":"MPH > MPS","func":"msg.payload = msg.payload / 2.237\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":100,"wires":[["4c3a5cc7f43e0519","1657d66d3ffd95d0"]]},{"id":"27970f52a4908ad1","type":"function","z":"a3a21d2e40b021ad","name":"Remove Decimal","func":"msg.payload = parseInt(msg.payload.toString().replace(\".\",\"\"))\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":200,"wires":[["4c3a5cc7f43e0519"]]},{"id":"4c3a5cc7f43e0519","type":"debug","z":"a3a21d2e40b021ad","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":100,"wires":[]},{"id":"0eaf5607dfe56699","type":"inject","z":"a3a21d2e40b021ad","name":"Injects 1.2 MPH","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1.2","payloadType":"num","x":120,"y":80,"wires":[["c3a1f05e884b85c0"]]},{"id":"1657d66d3ffd95d0","type":"change","z":"a3a21d2e40b021ad","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(payload, 2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":200,"wires":[["27970f52a4908ad1"]]}]

If i inject 1.2 miles per hour, with my current setup, i get 54 metres per second, which is incorrect, it should be 0.53, which means for my application i need to send a value of 053.

I am sending data to an external weather server, which requires data in MPS and my wind sensor is outputting miles per hour.

That’s because you’re rounding the payload to two decimal places.
The payload being 0.5364327223960661 gets round up to 0.54

$round(payload, 2)

Try this:

[
    {
        "id": "a3a21d2e40b021ad",
        "type": "tab",
        "label": "Temp Data",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c3a1f05e884b85c0",
        "type": "function",
        "z": "a3a21d2e40b021ad",
        "name": "MPH > MPS",
        "func": "msg.payload = msg.payload / 2.237\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 100,
        "wires": [
            [
                "4c3a5cc7f43e0519",
                "27970f52a4908ad1"
            ]
        ]
    },
    {
        "id": "27970f52a4908ad1",
        "type": "function",
        "z": "a3a21d2e40b021ad",
        "name": "Remove Decimal",
        "func": "var d = Math.pow(10,2)\nvar dec = (parseInt(msg.payload*d)/d).toFixed(2)\n\nmsg.payload = parseInt(dec.toString().replace(\".\",\"\"))\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 200,
        "wires": [
            [
                "4c3a5cc7f43e0519"
            ]
        ]
    },
    {
        "id": "4c3a5cc7f43e0519",
        "type": "debug",
        "z": "a3a21d2e40b021ad",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 100,
        "wires": []
    },
    {
        "id": "0eaf5607dfe56699",
        "type": "inject",
        "z": "a3a21d2e40b021ad",
        "name": "Injects 1.2 MPH",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1.2",
        "payloadType": "num",
        "x": 120,
        "y": 80,
        "wires": [
            [
                "c3a1f05e884b85c0"
            ]
        ]
    }
]

if you want 0.536 to become 0.53 then you need to either round differently or truncate the value.
Meaning you essentially take the four first characters.

This is what i need to acheive.

If the value is 0.432, i want the final value to be 043

If the value is 1.234, the final value needs to be 123

Can you send this as a string? and not a number

Thanks,

This works on numbers greater than 1 - if i inject 0.1mph, the value rounds to 4 meters per second, whereas it should be 004

Yes, can be sent as a string - it does however end up in a sensor thats use in a RESTful command.

then this should work

[{"id":"baf409c9.fa41f","type":"inject","z":"f80b6c338afd5483","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"002.12","payloadType":"num","x":850,"y":760,"wires":[["33fffafac0324657"]]},{"id":"33fffafac0324657","type":"change","z":"f80b6c338afd5483","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round((payload / 2.237), 2)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$string(payload)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$replace(payload, '.', \"\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":760,"wires":[["0130417f024d8c56"]]},{"id":"0130417f024d8c56","type":"debug","z":"f80b6c338afd5483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1390,"y":620,"wires":[]}]
1 Like