Persistence: sumSince / influxdb / sumSince

i try to count how many times a pump turned on. the pump is configured as switch. sumSince returns 0, in case i change the item to a temperature (Number), sumSince works correct…

any hints ?

thanks in advance

How many times a day?
Do you need to keep this value?

For a daily count, create an item and 2 rules:

Number pumpCount

Rules:

rule "Pump ON"
when
    pump changed to ON
then
    val count = pumpCount.state as Number
    pumpCount.postUpdate(count + 1)
end

rule "Reset count"
when
    Time "2 0 0 ? * * *" // 2 seconds after midnight everyday
then
    pumpCount.postUpdate(0)
end
1 Like

does it mean i can’t use sumSince for Switch items (the state persits to influxdb) ?

i don´t want to add additional item just for count

No, because a Switch item gets stored as a String in influxDB, either ON of OFF
And you need a Number item to use sumSince

Why not? I have just as many, if not more “Virtual” items used for just this kind of purpose, counting, triggers, timers…

Not on my installation, i don’t knwo exactly, but i use a map for translating value from device…

also with no binding an map, value in influxdb is numeric and not a string

Then you should be able to use sumSince

how can i debug, why its not working ? the request for query the items is sent to influxdb, i can read that in infliuxdb logs. the function it self is working anyhow, but returning “0”

can somebody help ? do i have to dive in the persistence binding ?

What rule code have you got so far?

// Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule “pump_zisterne_on”
when
Time cron “0 0/1 * * * ?”
then
var sum = pump_zisterne.sumSince(now.minusHours(24),‘influxdb’)
logInfo(“rules”,"Zisterne: " + sum.toString)
end

Try:
Remove the imports they are not needed in OH2

rule "pump_zisterne_on"
when
    Time cron "0 0/1 * * * ?"
then
    var sum = pump_zisterne.sumSince(now.minusHours(24), "influxdb")
    logInfo("rules", "Zisterne: " + sum.toString)
end