I discover it yesterday when I was asking myself of how to filter value coming from a ultrasonic level sensor I’m using for my fuel tank.
I’ve done some test, and have several interrogation / question about using it.
first to give you an overview of the previous situation, this is a snapshot of my previous unfiltered values.
As you can see, there is a lot of noise : value that are completely outside of range.
But as you can also see, the average of values are correct.
The jump on 26 around 8:00 is because we add 500 L to the tank.
What I try so far, is to filter the input value using a condition on DELTA_PERCENT.
This make thing smoother, but I’m not sure if it’s the best solution in my case.
Is using $AVERAGE, or $MEDIAN can be a better solution in my case.
If so, I don’t see sample of how to use it. Perhaps something like comparing my current incoming value with last 10 avg or median. Something like:
Second question is about mismatchState. If I understand properly, the default is to not update if the conditions are not met. Would it be an option to have a special mismatchState like mismatchState=”$AVERAGE” that will update the state with the average when condition are not meat.
Also, how can I handle specific case like tank fill up, that will change the value with great increment (I think if I move to using an average or median window, it should work as it) ?
@lo92fr When I added all these improvements to the existing State Filter profile, I didn’t have a concrete need for it and didn’t have any real use case.
Now given your requirements gave me a concrete example to ponder and I think the current State Filter as it is is insufficient to achieve what you really want.
I think ideally the filter should be expressed in something like this:
ABS($CURRENT - $MEDIAN(10)) < 5
But the State Filter as it is doesn’t support such an expression.
Perhaps I (or someone else) need to work on the State Filter to enrich its expression capability in the future. Its power at the moment is that it performs the statistical calculations for you without lengthy code required (e.g. in javascript). The same functionality can be done in JS / jruby but it’s going to be a lot more complex to implement especially if you aren’t into coding.
I’m thinking for backwards compatibility, leave the current conditions as is, and implement a new expression for the more advanced syntax.
I’m currently working with Jinja in another thing (yaml model enhancements), and perhaps Jinja could be a candidate to implement this since it supports evaluating expressions out of the box, we just need to supply it with the actual data, or implement the custom functions such as median(num_of_samples) or ItemName (to represent its state).
thanks for your answer.
As I’m already on 5.2-Snap, I we take a look to time-weighted-average-profile.
I’m also developer, so of course coding something directly in Javascript is another options.
I see some code like this earlier this week, I will also give it a try.