Fix EM320-TH decoder to handle negative temperatures #281
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The EM320-TH decoder treats temperature values as unsigned integers, causing incorrect readings for sub-zero temperatures. A value like -5°C (encoded as 0xFFCE in two's complement) was decoded as a large positive number.
Changes
parseTemperature(byteLow, byteHigh)function to handle signed 16-bit temperature valuesi→i+4for temp bytes after 4-byte timestamp)Applied to all 5 integrations: CHIRPSTACK, TTN, LORIOT, TTI, HTTP
Implementation
Before:
parseBytesToInt(input, i, 2, false) / 10After:
parseTemperature(input[i], input[i+1])Original prompt
I got a request from a customer:
"During our follow-up testing, we found that the current EM320-TH decoder does not correctly handle temperatures below 0°C. To resolve this issue, the decoder on the platform needs to be updated.
I’ve attached the updated decoder content for your reference. Could you please ask your technical team to assist with applying this update?"
Here is the attached decoder they sent:
var data = decodeToJson(payload);
var deviceName = data.deviceName;
var deviceType = "EM320-TH";
var groupName = null; // If groupName is not null - created device will be added to the entity group with such name.
var customerName = null; // If customerName is not null - created devices will be assigned to customer with such name.
// use assetName and assetType instead of deviceName and deviceType
// to automatically create assets instead of devices.
// var assetName = 'Asset A';
// var assetType = 'building';
// If you want to parse incoming data somehow, you can add your code to this function.
// input: bytes
// expected output:
// {
// "attributes": {"attributeKey": "attributeValue"},
// "telemetry": [{"ts": 1...1, "values": {"telemetryKey":"telemetryValue"}, {"ts": 1...2, "values": {"telemetryKey":"telemetryValue"}}]
// }
function decodePayload(input) {
var output = {
attributes: {},
telemetry: []
};
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.