Jsonata maths

1,197 views
Skip to first unread message

Garry Hayne

unread,
Feb 16, 2018, 7:19:46 AM2/16/18
to Node-RED
How can I evaluate ((msg.payload[1] + (256 * msg.payload[0])) / 1.2) in Jsonata in a change node?

Many thanks, Garry

AIOT MAKER

unread,
Feb 16, 2018, 9:10:35 AM2/16/18
to Node-RED
Hi Garry,

It should be straightforward.









Flow:



[{"id":"44686803.af5598","type":"debug","z":"8576c92a.fc8388","name":"","active":true,"console":"false","complete":"false","x":650,"y":220,"wires":[]},{"id":"92b8ef59.dcf75","type":"inject","z":"8576c92a.fc8388","name":"","topic":"","payload":"[5,3,9,12,6]","payloadType":"json","repeat":"","crontab":"","once":false,"x":220,"y":220,"wires":[["de60f2b7.b45e2","1c0e913f.457e7f"]]},{"id":"de60f2b7.b45e2","type":"change","z":"8576c92a.fc8388","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t   (\t       payload[1] + (256 * payload[0])\t   \t   ) / 1.2\t\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":220,"wires":[["44686803.af5598"]]},{"id":"1c0e913f.457e7f","type":"debug","z":"8576c92a.fc8388","name":"","active":true,"console":"false","complete":"false","x":350,"y":300,"wires":[]}]

Garry Hayne

unread,
Feb 16, 2018, 9:38:32 AM2/16/18
to Node-RED
Hi,

Sorry, I should have said that the payload is a raw buffer containing two bytes, I am getting:
"Invalid JSONata expression: The right side of the "*" operator must evaluate to a number"

Garry

AIOT MAKER

unread,
Feb 16, 2018, 10:08:53 AM2/16/18
to Node-RED
Perhaps if you share your flow or at least the original data set it will be easier for someone to suggest a solution. Kind Regards.

Nick O'Leary

unread,
Feb 16, 2018, 10:32:26 AM2/16/18
to Node-RED Mailing List
HI Garry,

JSONata can only do maths on Number types and provides the $number function to help convert types - it does, however, have some limitations.

    ($number(payload[1]) + (256 *$number(payload[0]))) / 1.2

The above expression will work if you are passing it an array of strings - such as ["10", "20"].

However it appears to not work on Buffer types - as Buffer is not a JSON type. I will talk to the JSONata project about Buffer support, but that doesn't help you right now.


So, if your data is in a Buffer, you'll need to use a Function to do the work:


msg.payload = (parseInt(msg.payload[1]) + (256*parseInt(msg.payload[0])))/1.2;
return msg;


On 16 February 2018 at 15:08, AIOT MAKER <aiot....@gmail.com> wrote:
Perhaps if you share your flow or at least the original data set it will be easier for someone to suggest a solution. Kind Regards.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/3449e008-8f43-4559-b931-717d64788ef9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Garry Hayne

unread,
Feb 16, 2018, 10:42:50 AM2/16/18
to Node-RED
Hi Nick,

I already had this working in a function node with : .lux = ((msg.payload[1] + (256 * msg.payload[0])) / 1.2)
without parseInt().
I was trying to move it to a change node to get into Jasonata a bit more!

Garry


On Friday, 16 February 2018 15:32:26 UTC, Nick O'Leary wrote:
HI Garry,

JSONata can only do maths on Number types and provides the $number function to help convert types - it does, however, have some limitations.

    ($number(payload[1]) + (256 *$number(payload[0]))) / 1.2

The above expression will work if you are passing it an array of strings - such as ["10", "20"].

However it appears to not work on Buffer types - as Buffer is not a JSON type. I will talk to the JSONata project about Buffer support, but that doesn't help you right now.


So, if your data is in a Buffer, you'll need to use a Function to do the work:


msg.payload = (parseInt(msg.payload[1]) + (256*parseInt(msg.payload[0])))/1.2;
return msg;

On 16 February 2018 at 15:08, AIOT MAKER <aiot....@gmail.com> wrote:
Perhaps if you share your flow or at least the original data set it will be easier for someone to suggest a solution. Kind Regards.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nick O'Leary

unread,
Feb 16, 2018, 10:44:28 AM2/16/18
to Node-RED Mailing List
I was trying to move it to a change node to get into Jasonata a bit more!

An honourable endeavour! It is unfortunate you've hit a case where JSONata can't help at the moment.

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Garry Hayne

unread,
Feb 16, 2018, 11:02:58 AM2/16/18
to Node-RED
Nick,
This was the raw data:


I put ((msg.payload.1 + (256 * msg.payload.0)) / 1.2) into a change node Jasonata expression and it worked!!!!!

Garry



Reply all
Reply to author
Forward
0 new messages