Help with group avg

Hi all.
I have eight miflora sensors and can easily group them and get avg temperature etc.
However, sometimes the update on one or more of the sensors fails. This means the avg is of several out of sync values with the current ones.
Can anyone suggest a way to avg group members only if they are up to date?
Cheers!

1 Like

One idea is to use a filter rule:

Thanks @sihui
I had a look at the link you suggest and I’m not sure how I would use this idea for eight sensors.
If I make a rule that discards nonsense and doesn’t update the second value, then the old value would still be in place (rrd4j persistence).
Thus, the avg will use the old value.
Any ideas how to exclude some sensors from avg if they are out-of-date?
Cheers!

See Design Pattern: Working with Groups in Rules

val sensors = SensorGroup.members.filter[ s | s.lastUpdate !== null ].filter[ s | s.lastUpdate.after(now.minusSeconds(10) ]
val sum = sensors.map[ state as Number ].reduce [ s, v | s = s + v ]
val avg = sum / sensors.size

All of the members of SensorGroup must be saved to persistence.

A better alternative, imho, is to use the Expire binding you set the sensor Item to NULL if it hasn’t received an update for too long. Then the first line above becomes:

val sensors = SensorGroup.members.filter[ s | s.lastUpdate != NULL ]

and it doesn’t require persistence. And the NULL is a good indicator to both your Rules and where you are displaying these sensors that it has not been updated so cannot be used.

In fact, I’m not certain but the AVG function may work with the NULL states on the Item so you won’t even need the rule. I’m not positive about that though.

It does, the AVG function ignores NULL state items

Yes the AVG function ignores NULL state items. However, recently I realized that when one of my sensors went down, it was showing “nan” on ESPEASY web interface, but openhab (OH2.5.12) showed the last valid sensor reading and the item state was stuck to that value (so it was not NULL), even though it had not updated for over 12 hours. Subsequently, this completely threw off the Average value of all the group items. I haven’t found a work around this yet. Any suggestions, tips or insight would be appreciated.

A few bindings offer direct means to deal with failed inputs. There are a number of generic techniques, too. Neither has much to do with Group AVG, you should start a new thread for thespecific problem.

1 Like

Thank you. I have opened a new thread. I appreciate your response. Regards.