Rule + Persistence (rrd4j) previousState not really working out for me

Hi,

First post here so be kind :slight_smile:
I think im missing something vital that is causing my problems and im sure someone here can help me :slight_smile:

I run Openhab2.2 off a RPi and everything is working perfect except for checking item previous states in rules…

On an item changed event im checking the previous state of the item to determin if it has changed.
Rule example:

val String filename = "presence.rules"
rule "Jonas Phone"
when Item JonasPhone changed
then
logInfo(filename, "JonasPhone New State: "+JonasPhone.state+" Old State: " + JonasPhone.previousState.state  )
end

This in my head should print the new value and the last persisted value, but it seems like the persistence service is too fast? And im always returning the current value.

2018-02-25 09:39:34.561 [vent.ItemStateChangedEvent] - JonasPhone changed from OFF to ON 2018-02-25 09:39:34.601 [INFO ] [marthome.model.script.presence.rules] - JonasPhone New State: ON Old State: ON

I have tried using both influxdb and rrd4j as persistence service but they both give the same symptom.

Anyone got some pointers for me?

try

JonasPhone.previousState().state

otherwise, please post your persistence configuration.

Hi! Thank you for taking the time to reply :slight_smile:

I changed to what you mentioned, unfortunately no change. This is my rr4d4j.persist

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
}

Items {
    //DemoSwitch, NoOfLights, Window_GF_Toilet, Heating* : strategy = everyChange, everyMinute, restoreOnStartup
    JonasPhone, AnnelisPhone : strategy = everyChange
    // let's only store temperature values in rrd
    //Temperature*, Weather_Chart* : strategy = everyMinute, restoreOnStartup
}

// vim: syntax=Xtend

Kind Regards
Jonas

hmm… I know not much about rr4dj, perhaps this persistence has some problems with non-numbers? you’re “JonasPhone”-item is a switch, I guess?

just checking the obvious: there’s nothing more in between those two lines?

and just to be sure, could you please post the items-definition of JonasPhone?

Hi,

Im running the network binding to discover devices availabiliy, so there is one more line in the logs where the responsetime changed to UNDEF.

2018-02-25 11:10:35.485 [vent.ItemStateChangedEvent] - JonasPhone changed from OFF to ON
2018-02-25 11:10:35.490 [vent.ItemStateChangedEvent] - JonasPhoneResponseTime changed from UNDEF to 0.0
2018-02-25 11:10:35.526 [INFO ] [marthome.model.script.presence.rules] - JonasPhone New State: ON Old State: ON
Switch JonasPhone { channel="network:pingdevice:jonasphone:online" }

BR Jonas

just for the record: it’s rrd4j.persist, right?

when i setup rr4dj people were saying it cant store non numbers but once setting it up it seems like it changes a on/off to a 1/0 before storing in db

i still dont know how to use the previous states but will get there

rr4d4j.persist is wrong its going to be a typo

Hi,

I set rrd4j to default persistence service in paperui so i would assume so, when i change the file openhab also reloads the file

2018-02-25 11:45:21.482 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'rrd4j.persist'

Seems like its getting the ON / OFF values from the db its just that it seems like its getting the just “updated” value as the last state, which ofcourse will be the same state :slight_smile:

Thank you all for taking the time to look at this thread :slight_smile:

Edit, i also tried with influxdb and had the same experience. Seems like im doing something wrong somwhere…
Edit2, switched back to influxdb to verify i have the same issues there still.

I have weird issues with persistance. Can you try sleeping your rule for 1 min and then log? Mine seems to work only if I’m not too quick. Also I read somewhere you need everyminute with rrd4j I was thinking that the every change isn’t working right.

https://docs.openhab.org/configuration/rules-dsl.html#implicit-variables

I think there is no persistence necessary for the previous state. It’s available as variable when the trigger indicates a status change. So not for received command as there the status change has not been processed. But for your rule it should work like that.

rule "Test previousState"
when
    Item TestPreviousState changed
then
    logInfo("Test previousState", "Item:\"" + triggeringItem.name + "\" state:\"" +  triggeringItem.state + "\" previousState:\"" +  previousState + "\"" )
end

Thank you all for your input on my tiny problem :slight_smile: I now have 2 working solutions going forward, and if i dont have to persist, thats the one im going with :slight_smile:

Works:
logInfo("Test previousState", "Item:\"" + triggeringItem.name + "\" state:\"" +  triggeringItem.state + "\" previousState:\"" +  previousState + "\"" )

Works with influxdb:
logInfo(filename, "JonasPhone New State: "+JonasPhone.state+" Old State: " + JonasPhone.previousState().state  )


2018-02-25 13:13:11.126 [INFO ] [home.model.script.Test previousState] - Item:"JonasPhone" state:"ON" previousState:"OFF"
2018-02-25 13:13:11.132 [INFO ] [marthome.model.script.presence.rules] - JonasPhone New State: ON Old State: OFF```
 
Kind Regards,
Jonas