historicState sumSince

I am trying to compute the rain fall total over the past 5 days.
Precip_Total gets updated from weatherUnderground and whenever it changes I want to recompute the 5 day total.

I have this rule as a test (zone3 is a motion sensor)

rule "Recent Temps"
when
    Item zone3 changed to OPEN
then
    if (Precip_Total.historicState(now().minusDays(5)) != null) {
      logInfo("Motion", "5 Days Ok")
    }
    if (Precip_Total.historicState(now().minusDays(4)) != null) {
      logInfo("Motion", "4 Days Ok")
    }
    if (Precip_Total.historicState(now().minusDays(3)) != null) {
      logInfo("Motion", "3 Days Ok")
    }
    if (Precip_Total.historicState(now().minusDays(2)) != null) {
      logInfo("Motion", "2 Days Ok")
    }
    if (Precip_Total.historicState(now().minusDays(1)) != null) {
      logInfo("Motion", "1 Days Ok")
    }
    if (Precip_Total.historicState(now().minusHours(5)) != null) {
      logInfo("Motion", "5 Hour Ok")
    }
    if (Precip_Total.historicState(now().minusHours(4)) != null) {
      logInfo("Motion", "4 Hour Ok")
    }
    if (Precip_Total.historicState(now().minusHours(3)) != null) {
      logInfo("Motion", "3 Hour Ok")
    }
    if (Precip_Total.historicState(now().minusHours(2)) != null) {
      logInfo("Motion", "2 Hour Ok")
    }
    if (Precip_Total.historicState(now().minusHours(1)) != null) {
      logInfo("Motion", "1 Hour Ok")
    }
    logInfo("Motion", "Anything good?")
end

The only output I get is “Anything good?”

Here is my mysql table for this item

+---------------------+-------+
| Time                | Value |
+---------------------+-------+
| 2016-05-31 20:06:00 |     0 |
| 2016-06-01 20:06:00 |     0 |
| 2016-06-02 20:06:00 |     0 |
| 2016-06-03 20:06:00 |     0 |
| 2016-06-04 20:06:00 |     0 |
| 2016-06-05 20:06:00 |  2.64 |
| 2016-06-06 20:06:00 |  2.64 |
+---------------------+-------+
7 rows in set (0.00 sec)

The entry from 6/6 was added by openhab, I then shutdown and added the other manually

If I just do a sumSince(now.minusDays(5)) I get 0

Any suggestions?

Did you setup the default persistence to mysql? If not, you have to use

Precip_Total.historicState(now().minusHours(2),"mysql").state

Be aware of the .state at the end, as historicState will return an object with timestamp, hashcode, name, state…

Just looking over my logs… cockpit error… I have Precip_Total_Inches set as persistent… tried it with the correct item and it works fine…

I do have mysql set as the default persistence, but the extra argument is nice to see

tom

Still not out of the woods, I can’t seem to fins my syntax error…

rule "Recent Rainfall"
when
    Item Precip_Total_Inches changed or
    Item zone3 changed to OPEN
then
    if (Precip_Total_Inches.historicState(now().minusDays(5)) != null) {
      var Number rainTotal = Precip_Total_Inches.sumSince(now().minusDays(5))
      logInfo("Motion", "History " + rainTotal)
      logInfo("Motion", "Today " + Precip_Total_Inches)
      rainTotal = rainTotal +  Precip_Total_Inches.state as DecimalType
      logInfo("Motion", "Update new total " + rainTotal)
      postUpdate(Precip_Total_5Days, rainTotal)
    } else logInfo("Motion", "Update new average NULL")
end

Produces output in log

2016-06-07 11:14:00.994 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'weather.rules'
2016-06-07 11:14:04.619 [INFO ] [rg.openhab.model.script.Motion] - History 2.7599999999999997868371792719699442386627197265625
2016-06-07 11:14:04.884 [INFO ] [rg.openhab.model.script.Motion] - Today Precip_Total_Inches (Type=NumberItem, State=1.85)
2016-06-07 11:14:05.164 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Recent Rainfall': Cannot cast org.openhab.core.library.types.DecimalType to void

Just what does it think is a void?

I am assuming the error in on rainTotal = rainTotal + etc…

tom

I forgot I needed to IMPORT the DecimalTypes definition…

import org.openhab.core.library.types.DecimalType