From a38042213f9655b45bef27b2dd5ff47584dfaee2 Mon Sep 17 00:00:00 2001 From: Adrian Giesbrecht Date: Fri, 5 Aug 2022 15:07:10 -0400 Subject: [PATCH] try parseFloat mqtt payload in order to be able to format number with significant digits in value output control --- server/runtime/devices/mqtt/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/server/runtime/devices/mqtt/index.js b/server/runtime/devices/mqtt/index.js index 37417ecc8..252a4b14e 100644 --- a/server/runtime/devices/mqtt/index.js +++ b/server/runtime/devices/mqtt/index.js @@ -342,7 +342,18 @@ function MQTTclient(_data, _logger, _events) { for (var i = 0; i < topicsMap[topicAddr].length; i++) { var id = topicsMap[topicAddr][i].id; var oldvalue = data.tags[id].value; - data.tags[id].value = msg.toString(); + let val = parseFloat(msg.toString()); + if (Number.isNaN(val)) { + // maybe boolean + val = Number(value); + // maybe string + if (Number.isNaN(val)) { + val = value; + } + } else { + val = parseFloat(val.toFixed(5)); + } + data.tags[id].value = val; data.tags[id].timestamp = new Date().getTime(); data.tags[id].changed = true; if (data.tags[id].type === 'json' && data.tags[id].options && data.tags[id].options.subs && data.tags[id].memaddress) { @@ -574,4 +585,4 @@ module.exports = { create: function (data, logger, events) { return new MQTTclient(data, logger, events); } -} \ No newline at end of file +}