Transformation of persistence values for chart

Hello everybody,

I am using mysql persistence to write temperature values to database. Two of the values are Celsius, one is Kelvin. No problem with the values because I us JavaScript transformation to show them all in Celsius. But when I add them to a group to show a chart, two of them are between 20 and 30, one is at around 300. Is there any way to transform the data before sending them to database or after getting them?

I would transform the values before persisting.
Depending on how you get the data it migth be possible directly when getting the value or you could use a secondary item which gets populated by a rule that is triggered by the update of the initial item. In this case do persist only the secondary item.

1 Like

I would transform the values before persisting.

Probably the only possibility.

Depending on how you get the data

As far as I know it’s not possible to do 2 transformations a time and I get them via JSONPATH: { http="<[robonectBattery:10000:JSONPATH($.batteries.[0].temperature)]" }

I knew about the possibility of a rule, but I don’t like the idea of too much rules watching change of item states all the time. But it seems there’s no other way…

Not within the http binding at least.
However, javascript is a flexible thing. You can write javascript JS transformations that carry out as many operations as you like in one script.

In this case, extract your value from JSON and do maths on it.

Example

Hi rossko, that’s exactly what I’m doing to get correct values on my app and in Basic UI:
Number Robonect_BatteryTemperature "Temperature [JS(temp.js):%s]" <temperature> (myGroup) { http="<[robonectBattery:10000:JSONPATH($.batteries.[0].temperature)]" }

But this does not affect the values that are written to database.

Why would it?

When you apply a transform in the [state format] part of an Item label, it is purely about display. It does not affect the Item state and is deliberately designed not affect Item state.
Look in your events.log for example to see the true value.

Persistence persists the Item state, not whatever you might or might not display.

When you apply a transform at the binding, it works on raw data from some remote device/service before it gets to the Item, and so it changes Item state.

Persistence persists the Item state, not whatever the external data might have been.

I know, but that’s why I don’t have any other idea than using a rule. Except if i could write a JS Script that does JSON interpretation AND recalculation of the value. Is that what you meant?

Yes, that’s what I said, meant, and provided an example of.

Well then, I didn’t understand it, sorry. I’ll give it a try.

It won’t fix your “old” data, unfortunately.

Hi rossko,

this is my solution, if anybody needs it:

(function(jsonString) {
    var data = JSON.parse(jsonString).batteries;
    return data[0].temperature / 10;
})(input)

Um, that doesn’t convert K to C, but I’m sure you know what you want !

Yes, you’re right. I got another temperature Item in K. Just a mix-up. This one is 10 times the value it should be. But I like the solution, it’s not about deviding by 10 or subtracting 273.15.

When I wrote the first message, temperature was approximately 30°C. So 30*10 and 30+273.15 made me mix it up.

That’s unusual. Used to seeing 215 to represent 21.5 in idiot integer protocols like Modbus, but it seems odd to transmit a raw value here when the device goes to the trouble of packaging in JSON, which is quite happy with 21.5

You have to take what you get. Thanks a lot, anyway :wink:

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.