Simple rule to get average temp. return postUpdate(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null

Hi i’m missing something while trying to create my first rule (a really simple one to be onest) to calculate maximum, minimum and average temperature of past 24 hours.
I firstly created 3 new items on PAPERUI (I’m on openhabian): Temp_Ext_MAX, Temp_Ext_MIN and TEMP_Ext_AVG, that i declared as Number and added to rrdj4.persist (for persistence) with : strategy = everyMInute

Later i created this rule:

rule "Grafici Temperatura esterna" when Item UnitaEsternaTemperatura received update then var Number tempA = UnitaEsternaTemperatura.maximumSince(now.toDateMidnight) var Number tempB = UnitaEsternaTemperatura.minimumSince(now.toDateMidnight) var Number tempC = UnitaEsternaTemperatura.averageSince(now.toDateMidnight) Temp_Ext_MAX.postUpdate(tempA) Temp_Ext_MIN.postUpdate(tempB) Temp_Ext_AVG.postUpdate(tempC) end

UnitaEsternaTemperatura is a Numeric item connected to my daikin pump which return me the external temperature.
When i try this rule the log report me the following error, which i’m not able to debug :frowning: : Rule 'Grafici Temperatura esterna': An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null

If i try the following:

rule "Report to log" when Item UnitaEsternaTemperatura received update then var Number tempC = UnitaEsternaTemperatura.averageSince(now.toDateMidnight).toString logInfo("Average temperature is:", tempC) end
All work as expected, so i really don’t understand what i’m missing :frowning:
Thanks for your kind help!

Hope that is a typo in the posting. It should be everyMinute.

toDateMidnight is deprecated. You should use now.withTimeAtStartOfDay. But that isn’t an error.

But there is an error. maximumSince and minimumSince return a HistoricItem, meaning you get an actual Item object, not just a State. So you need to call .state:

        var Number tempA = UnitaEsternaTemperatura.maximumSince(now.toDateMidnight).state
        var Number tempB = UnitaEsternaTemperatura.minimumSince(now.toDateMidnight).state
        var Number tempC = UnitaEsternaTemperatura.averageSince(now.toDateMidnight)

Unfortunately, this error can mean a lot of different things. At a high level, it means it cannot cast or parse something passed to postUpdate into a form that is usable for applying to that Item’s state. In this particular case, it cannot cast tempA, which is of type HistoricItem, to a Number.

Thanks and sorry for late answer but the panel was on read only mode for some times.
Your suggestion (add .state at the end of each line) did the trick :smiley:
Sorry for the noob question but unless sitemaps, persistence and transformation, rules seems to have a little bit harder learning curve, especially if someone doesn’t have any coding experience like me. Also the wiki seems to be a little bit confusing on that point :frowning: (i found there the now.toDateMidnight string).

Thanks again!!!

If you are running OH 2.x you should be using the docs. The wiki was written for OH 1.x and with the exception of the binding documents they have not been kept up to date.

Thanks i’ll take a look also there !!