By this I mean are you doing some sort of calculation that requires only two decimal points? Or do you just want to see only two decimal places in your sitemap or logs?
This is important because a little know fact about CPUs is they are really terrible at floating point math and even worse, it is impossible to even represent some numbers. Oh, it can get really close which leaves you with a value like -0.840000152587890812 but it almost never can get exact.
The proper way to deal with this is to simple live with the numbers having lots and lots fo decimal places (i.e. don’t round the number) until the very last moment, usually when the number is presented to a human.
In your sitemap you would use:
"My label [%.2f]" which will only print the two decimal points.
While we are on this note, this is the same reason why you should never try to use == with floating point numbers. They will almost never be equal.
I’ll add some more info:
In my openHAB setup, I’ve a lot of Xiaomi temperature sensors. Once a while some of them goes offline (it’s the connection with the gateway, not with openHAB). They only come online again after connecting them again to the gateway.
So I want to receive a notification/mail when this happens.
I thought to check the delta (deltaSince) of the temperatures onze a day: when one of them is 0, something is not correct (I have persistence on for every 5 minutes, so updatedSince is not an option).
I’m now displaying the delta of one of my sensor which is offline since 30 hours (afterwards, if all works, I’ll apply the full rule to a whole group):
Probably the easiest would be to use the expire binding to set the Item to NULL or some other noticable value (e.g. -1) after the Item hasn’t been updated for a time. It is best to set the expire time to 2x the update time so if the Xiaomi updates every 10 minutes set the expire binding to 20 minutes.
You can set a rule that triggers on changes to this item and first thing in the rule check whether it is e the expired value and alert or whatever as appropriate.
There is also Design Patterns: Generic Is Alive which is a more comprehensive solution but it is a little more complicated than you need right here and I need to rewrite that DP so if you want to take that approach, wait until next week when I have a chance to rewrite it.
I also thought about the expire binding, but when I write the values every 5 minutes to my db, it receives an update, even if the value is the same.
Or is there a difference between writing a value which can be the same as the previous one and an update?
They are completely separate. Updates can be saved to the database but saving to the database does not cause updated to the item. And I’m pretty sure that NULL doesn’t get saved to the database so that that into consideration for what you set the item to with expire.
I don’t think you can use OR with a Number type. What would you expect it to be?\
But, if we assume it works, you probably need to use NULL instead of UNDEF.
As written, again assuming it works, Grp_Xiaomi_sensor_check will be 0 if there are no UNDEF Items and UNDEF if there is one or more UNDEF Items. But since UNDEF really isn’t a valid Item state I’m not sure what it would do.
I first tried to give my sensor a unrealistic value (‘999’), I used MAX for the group, so when the group is 999, I know there’s something wrong. But I don’t want that it also writes this value in my db (my graphs are f*cked up now )
You could use a proxy Item and a Rule instead of the Group to keep track of whether a sensor is offline or reporting a bad value or something. But over all I suspect you can’t do this with just a Group.