# Problem with previousState in rule for battery alarm

**URL:** <https://community.openhab.org/t/problem-with-previousstate-in-rule-for-battery-alarm/70553>\
**Category:** Off-Topic\
**Tags:** rule, battery, icloud\
**Created:** [March 18, 2019, 8:57pm UTC](https://community.openhab.org/t/problem-with-previousstate-in-rule-for-battery-alarm/70553 "2019-03-18T20:57:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [March 18, 2019, 8:57pm UTC](https://community.openhab.org/t/problem-with-previousstate-in-rule-for-battery-alarm/70553/1 "2019-03-18T20:57:18Z")

</div>

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!

```csv
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:

```csv
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

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [March 18, 2019, 9:19pm UTC](https://community.openhab.org/t/problem-with-previousstate-in-rule-for-battery-alarm/70553/2 "2019-03-18T21:19:47Z")

</div>

> [@e36Alex](#):
>
> IPadVonFamilieIPad\_Batterieladung.previousState.state

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

```php
if ( (IPadVonFamilieIPad_Batterieladung.state as Number) >= 25
   && (previousState as Number) > (IPadVonFamilieIPad_Batterieladung.state as Number)
    ) {

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [March 18, 2019, 10:14pm UTC](https://community.openhab.org/t/problem-with-previousstate-in-rule-for-battery-alarm/70553/4 "2019-03-18T22:14:31Z")

</div>

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