Problem with previousState in rule for battery alarm

Hello,

i got a little problem with previousState in a rule for a battery alarm.

I want a broadcast-message at every change of the battery level of my iPad, if its 25% or lower.

To avoid messages during charging, i want the message just, if the previousState ist greater than the actual state.
I created the following rule, but i doesn’t fire.

Woult be great if you could help me to get it running.

Thanks Alex!

rule "Battery_Low_iPad"
when
    Item IPadVonFamilieIPad_Batterieladung changed
then
    if (IPadVonFamilieIPad_Batterieladung.state <=25 && IPadVonFamilieIPad_Batterieladung.previousState.state > IPadVonFamilieIPad_Batterieladung.state ) {
    sendBroadcastNotification("Das iPad muss geladen werden, der Akku hat nur noch "+IPadVonFamilieIPad_Batterieladung.state+" %!")
    }
end

i also tried it with the if-statements in two brackets:

rule "Battery_Low_iPad"
when
    Item IPadVonFamilieIPad_Batterieladung changed
then
    if ((IPadVonFamilieIPad_Batterieladung.state <=25) && (IPadVonFamilieIPad_Batterieladung.previousState.state > IPadVonFamilieIPad_Batterieladung.state )) {
    sendBroadcastNotification("Das iPad muss geladen werden, der Akku hat nur noch "+IPadVonFamilieIPad_Batterieladung.state+" %!")
    }
end

Unless you have suitable persistence configured on this Item, you won’t get .previousState

Fortunately your rule is triggered off Item change, so you can use the implicit variable previousState which gets populated automatically.

Number Item state comparisons can be picky and it’s usually best to ensure you get them as Number variables.

Try

if ( (IPadVonFamilieIPad_Batterieladung.state as Number) >= 25
   && (previousState as Number) > (IPadVonFamilieIPad_Batterieladung.state as Number)
    ) {
1 Like

setting the state to a number works fine, thank you!