Part 1: I’m planning to use Kalman filter (*) to filter signals from a temperature and a level sensor. Is there anything like that implemented already in openHAB? I did my homework and I’m 99% sure there is no such thing.
Part 2: If i want to implement it as an internal function of openHAB - is there a library built-it functions where I could add it?
Have a look at state profiles. There is no Kalman filter, but you could probably write one based on such a profile: Transformations / Profiles | openHAB
Filters, like Kalman, low pass, high pass, etc. are part of digital signal processing. In order to work they need some preconditions. Usually this will be:
Constant sampling rate
Sampling rate high enough to cover the highest signal components (Shannons theorem)
State
The model of the Things/Items currently do no support this out of the box. Therefore I suggest you model this with so called proxy items and rules implementing your filter on the original item.
Can profiles have state? Without it, it will be difficult to implement transfer functions for filter components. But I guess the answer is yes for the StateProfile.
See this low pass filter implementation:
It needs at least the last value (state) and the new value to apply the transfer function.
Can StateProfiles sample the input thing independent from state changes to achieve a constant sampling rate?
A profile is an intermediary between a Channel (which interfaces with an external information source or sink) and an Item (which interfaces with the user via the UI). They transform incoming values received from the Channel and pass a transformed outgoing value to the Item. Examples of typical (existing) profile transformations are average of last n values; minimum; maximum; diff between last two values; ignore inputs with delta less than than some limit; etc. etc. So to answer your question – no the profile does not have a state; (the only “states” are the prior incoming values from the Channel and the resulting transformed outgoing value to the Item).
I don’t want to disturb the discussion with my ignorance, but I think Kalman filter might be OK (not perfect, but OK) without the constant sampling rate and openHAB proxy items seem to be promising. Definitely I need to understand the implementation of the Low Pass filter.
There are PID and PWM add-ons which, based on my ancient half forgotten knowledge, kinda sorta works like a Kalman filter at a high level. So a full add-on might be something worth looking at as an approach.
It also might work as a persistence action. Riemann sums were just added to persistence and if this could be encapsulated as a persistence call that might work too. You definitely have control over the sample rate with persistence. Using the in memory persistence would be a good choice for implementing something like this.
The SCRIPT Transformation Profile is able to access the cache which could be used to store more data than a standard profile would normally be able to. If nothing else it could be a quick way to prototype and prove whether the profile approach overall is viable.
In rules languages like Python, jRuby, and JS there is access to third party libraries. You may not need to implement anything at all and just use a library that has already been implemented. jRuby also has an easy way to create profiles so you are not limited to just rules.