More previousState states

Hi, i am notifying myself via notificiation, if a certain items battery level is under a specified percentage. Now, as it is cold in germany, the battery percentages drop and rise automatically, when its getting warmer. (Especially for outside mounted devices)
I have a motion sensor on my terrace which sends its percentage level. Due to the weather conditions it switches like
5% > 9% > 6% > 10%
My rule checks, if the new percentage is lower, than the .previousState(true, “influxdb”). But as you see i will get a notification at 5% AND 6%, according to my rule. Is there a way to compare the last (e.g. 20 states) with the newest state? Like .previousState(true, 20, “influxdb”)?
So it only executes, when the newState is lower than all of the last 20 states.

    if (newState < batteryItem.previousState(true, "influxdb")) {

You can write your own complicated code to do that, all records are available using historicState() - and searching from each historicState() timestamp will give you trhe record before.

But have you considered using averageSince() or minimumSince() or suchlike functions?

Thanks for your answer.

I tried .minimumSince(now.minusDays(7))

Now the following problem occurs:
If the battery percentage drops 15 > 10 > 9 > 12 > 9
my rule compares the newState with the minimumSince.state. (newState <= minimumSince)
the minimumSince.state is ALWAYS the newest, lowest state. So my rule would notify me, if it drops to 9% a second time in that period. If i try (newState < minimumSince) my rule is not triggering at all, since the newstate can NEVER be lower than the minimumSince

So i tried .sinceBetween(now.minusDays(7), now.minusSeconds(20)). Which should compare newState with the minimum state, BEFORE it changed to the newest state. But openhab wont let me do this.

Script execution of rule with UID 'Akkustand-1' failed: cannot invoke method public abstract org.openhab.core.types.State org.openhab.core.persistence.HistoricItem.getState() on null in Akkustand

Maybe influxdb is not able to use .minimumBetween?

rule "Notification: Akkustand eines Gerätes niedrig"
    Member of gAkkustand changed
    var Number valuenewstate = Integer::parseInt((newState as DecimalType).toString.split('\\.').get(0)) // newState auf Ziffern vor dem Komma heruntergebrochen
    var Number valuepreviousstate = Integer::parseInt((previousState as DecimalType).toString.split('\\.').get(0)) // previousState auf Ziffern vor dem Komma heruntergebrochen
    //var Number valueminimumsince = Integer::parseInt((triggeringItem.minimumSince(now.minusDays(7)).state as DecimalType).toString.split('\\.').get(0))
    var Number valueminimumsince = Integer::parseInt((triggeringItem.minimumBetween(now.minusSeconds(20), now.minusDays(7)).state as DecimalType).toString.split('\\.').get(0))

    if (triggeringItem.state <= 10 && valuenewstate < valuepreviousstate && valuenewstate < valueminimumsince) {
            // do stuff