Hi guys I have on a Tasmota device a Tuyasend command which from 1605 to 1660.
I need to separate the 16 from the other 2 values (temperature) with a space or a comma. Is there a simple command or function node to make it 16,05 or 16 05?
subtract 1600?
divide by 100.
In a change node with JSONata
$join($split($string($number($$.payload)/100), "."), ",")
or in a function
msg.payload = String(Number(msg.payload)/100).split(".").join(",");
If it is always a four digit number and you want to split it in the middle and add a comma you could convert it to a string, then use two substring statements to grab the first two characters and the last two characters and then combine them with a comma
msg.payload = char1_2 + ‘,’ + char3_4
So if the original value is 1645 you change it to a string ‘1645’ then use the substring commands to break it up to ‘16’ and the ‘45’ (remember the substring command uses ) as the first character) then combine them.
Use a function node to write the code.
And I pretty sure that someone might tell you how to use JSONata to do it, but if you do it yourself you will learn something new today.
(You can look up the JavaScript commands at w3schools.com)
[edit: see, while I was typing you got the JSONata solution]
Perfect, this works perfectly, thank you very much!
Edit: @E1cid
I found that 1610, 1620 and 1630 are each missing the zero. Same with function node.
Thanks, I'll take a look at your post as well.
Similar to what Paul mentioned, I would approach this as simple string manipulation rather than using arithmetic (to avoid losing leading zeros and such).
If you know your input is always 4 digits, then this regex replace command will work in either a function
node...
"1620".replace(/(\d\d)(\d\d)/, '$1,$2') //yields the string '16,20'
or even in a change
node:
Of course, you will have to adjust your input a bit, if it's not a 4-digit string in msg.payload
It doesnt work because slider outputs a number.
You can manipulate that is several ways but its just easier to do it in a function node...
function code...
msg.payload = msg.payload.toString().replace(/(\d\d)(\d\d)/, '$1,$2')
return msg;
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.