Hello
I am trying to get a historical value of a temperature element throughout the life of my OH (which since the last reinstallation is not much) I have tried with the rules that I publish below and I have no result, I also have them added to the persistence configuration file
`rule “Update max and min historicas temperatures exterior”
when
Item Temperatura changed or
Time cron “0 0 0 * * ?” or
System started
then
thank you
ps-I have searched and I have not seen anything on the subject, something quite strange, I understand that perhaps I have not searched well, sorry for my English
if you look in your openhab.log you might see error messages.
.historicState() fetches from persistence your Item’s state at a past moment in time (if it can !).
It follows that historicState() needs you to tell it which of the large number of past moments in time that you mean. In other words, you have to give it a target datetime that you want data from.
Example var oldRecord = myItem.historicState(now.minusMonths(6))
It’s not really clear what you have in mind? Are you trying to get the average over a long period, or something? You might look at the other persistence methods.
If it is “getting the oldest data”, that is harder than it looks - historicState() can only give the state at a given moment. In reality, that is usually the database record from just before the moment you asked for. But there is not an easy way to ask for the very oldest record, apart from trying past dates and seeing what works.
Thank you very much what speed in answering like this
I use the rrd4j persistence system
I try to keep a history of the highest and lowest temperature of the entire time the system has been operating
".historicState () fetches from persistence your Item’s state at a past moment in time (if it can!).
It follows that historicState () needs you to tell it which of the large number of past moments in time that you mean. In other words, you have to give it a target datetime that you want data from.
Example
var oldRecord = myItem.historicState (now.minusMonths (6)) "
var oldRecord = youPutYourItemNameHere.historicState(now.minusMonths(6))
if (oldRecord !== null) {
logInfo("test", "The data was " + oldRecord.state.toString)
logInfo("test", "Datestamped " + String::format("%1$td/%1$tm/%1$tY", oldRecord.timestamp)
} else {
logInfo("test", "There was no persisted data for that time")
}
rule “Update max and min historical exterior temperatures”
when
Item Temperature changed or
Time cron “0 0 0 * *?” or
System started
then
var oldRecord = Temperature.historicState (now.minusMonths (6))
if (oldRecord! == null) {
logInfo (“test”, “The data was” + oldRecord.state.toString)
logInfo (“test”, “Datestamped” + String :: format ("% 1 $ td /% 1 $ tm /% 1 $ tY", oldRecord.timestamp))
}
else {
logInfo (“test”, “There was no persisted data for that time”)
}
end
and the only thing I get in the log when the temperature value changes is this
2020-09-02 18: 22: 19.348 [INFO] [.eclipse.smarthome.model.script.test] - There was no persisted data for that time
How do you try to keep highest and lowest readings? A single max over the whole time or the daily/weekly / etc max saved in a database?
The code shown so far doesn’t do anything to that concern.
You need the .maximumSince and.minimumSince.
Note that rrd4j (in the default config) does store averages if you are looking further then 8 hours into the past.
@opus your help is correct I already see the minimum temperature and double changing one part and I also see the maximum temperature, thank you very much
@5iver I’m sorry to tell you that when modifying “(now.minusDays (15)) by (now.DateTime (0))” I get this error in the log
2020-09-02 21: 31: 47.270 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Update max and min historical exterior temperatures’: The name ‘DateTime’ cannot be resolved to an item or type; line 19, column 43, length 11
I will look at the link you put me, unfortunately my English is quite bad and it will cost me time to see the document
follows me not valid it does not use the old values, I am trying to change the number of days and every time I make a change for some reason it changes the value for a higher one
I’m not sure what you were saying . Here is a bit more detail on what I was suggesting. This rule is untested and assumes that your Temperature Item is not utilizing UoM. I am also not sure what your persistence will do with a very old date, but JDBC can handle it.
rule "Update max and min historical exterior temperatures"
when
Item Temperature changed
or
Time cron "0 0 0 * * ?"
then
val current_state = Temperature.state
if (current_state == NULL) {
return
)
val minimum_value = Temperature.minimumSince(new DateTime(0)).state
val maximum_value = Temperature.maximumSince(new DateTime(0)).state
if (current_state < minimum_value) {
Temp_Min_hist.postUpdate(current_state.toString)
logInfo ("test", "The current value '{}' was less than the previous minimum '{}', so it was updated", current_state, minimum_value)
return
} else if (current_state > maximum_value) {
Temp_Max_hist.postUpdate(current_state.toString)
logInfo ("test", "The current value '{}' was greater than the previous maximum '{}', so it was updated", current_state, maximum_value)
return
}
logInfo ("test", "The current value '{}' did not require updating the previous minimum '{}' or maximum '{}'", current_state, minimum_value, maximum_value)
end