Average of 1 item during 'sun'

Hello,
I’m using following rule to calculate the average of 1 item (of the last 24 hours).
The value is the current production of a solar system.

rule "Average se15000_Prod"
when
    Item se15000_Prod received update
then
    se15000_ProdAverage.postUpdate(se15000_Prod.averageSince(now.minusHours(24)))
end

I would like to do this now only during sunrise and sunset, and a reset of the day. In other words, I only want to see the average of the day. But of course, during night, this will be 0, so would be nice to exclude this…

Any tips?

You will need to look at:

Put the time of day in an item: Time_Of_Day with the value “NIGHT” after sunset and before sunrise
The you will need a proxy item:

Number se15000_Prod_Proxy

And your rule:

rule "Average se15000_Prod with Night"
when
    Item se15000_Prod received update
then
   if (Time_Of_Day.state != "NIGHT") {
        se15000_Prod_Proxy.postUpdate(se_15000_Prod.state)
    } else {
        se15000_Prod_Proxy.postUpdate(0)
    }
    se15000_ProdAverage.postUpdate(se15000_Prod_Proxy.averageSince(now.minusHours(24)))
end

I don’t think this is exactly what OP is asking for. The zeros of night period will skew the average down. Maybe it would work if we don’t post anything when it’s NIGHT. This would also assume that we are using just an every update strategy and not everyMinute (i.e. no rrd4j).

I’m pretty sure the averageSince just retrieves all the values and averages them together without doing anything fancy with the amount of time or normalization or anything like that.

1 Like

Yes, I didn’t think about the value being drawn down. So no updating the value and we’ll have to assume that the value gets updated on a more or less regular interval.

rule "Average se15000_Prod with Night"
when
    Item se15000_Prod received update
then
   if (Time_Of_Day.state != "NIGHT") {
        se15000_Prod_Proxy.postUpdate(se_15000_Prod.state)
    }
    se15000_ProdAverage.postUpdate(se15000_Prod_Proxy.averageSince(now.minusHours(24)))
end
1 Like

Can I reset se15000_Prod_Proxy as well? I would like to see the average only of today.
Something like:

rule "Reset Average value"when
    Time cron "0 0 0 ? * * *"
then
    se15000_Prod_Proxy.postUpdate("0")
end

Or will the minusHours(24) always take values of yesterday in account?

It will take all readings from the last 24 houres, starting from NOW. For example: when doing that request on Monday 10 am, it will take all reading from Sunday 10 am until Monday 9:59 am!

Damned.
So I can’t use the se15000_Prod_Proxy.averageSince(now.minusHours(24)) for this then?

Is there an alternative, like for example just ‘se15000_Prod_Proxy.average’? :blush:

You can use averageSince(Insert Time to start)

To be specific

se15000_Prod_Proxy.averageSince(now.withTimeAtStartOfDay)

which will give you the average since midnight on the current day.

1 Like

I tried to get this working from sunrise (instead of midnight).
Tried your example with TimeOfDay and so.
But didn’t succeed in it. :disappointed_relieved:

This would give me the exact production, since before sunrise, the value is ‘0’…

Any hint what i could try?

It’s is hard to saw workout the code you already tried.

There big think I’d you have to convert the DateTimeType to a DateTime that can be passed to averageSince. See https://docs.openhab.org/configuration/rules-dsl.html#datetime-item