[SOLVED] Rules - Avg temperature

Hi All,
I’m trying to set a rule for calculating the average of a set of temperature sensors, but I’m struggling in making the math, Here is the rule

rule “Rule_AAA”
when
Item ThermoSoggiorno_T changed
or
Item ThermoBagnoPT_T changed
then
var t = (ThermoSoggiorno_T.state + ThermoBagnoPT_T.state)
TemperaturaPianoTerra.postUpdate(t)
end

I get this error

An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null

I know the avg temperature may be calculated also defining a group, but I’d like to work as I would like to make more sofisticated maths in the rules.
Anyone can help?

thanks

Ahh, just ignore… I didnt notice you didn´t want to use group.

Sorry!

Thank you Kim. But I would like, at a later stage, to build more sofisticated math like predictive indicators, and I really don’t understand why I’m not able to make a simple sum between a couple of “double” type numbers. I certainly miss some knowledge about the item state object structure.
Any help?

Thanks again

I’m far more comfortable with the wiring side of things, but as far as I understand it…

That error message is suggesting that one of your donor values hasn’t been updated with a value yet?

(I see it immediately after a start up, if sensors haven’t been polled.)

There are lots of comments on here about building in resilient against null values (or at least factoring them into rules).

I hope that helps you.

Try this rule

rule “Rule_AAA”
when
Item ThermoSoggiorno_T changed
or
Item ThermoBagnoPT_T changed
then
var t = (ThermoSoggiorno_T.state as Number + ThermoBagnoPT_T.state as Number)
TemperaturaPianoTerra.postUpdate(t)
end

Hi Stuart,
Thanks but there is no dubt, the sensors are updated (otherwise the event wouldn’t fire). Anyway I see the value in the log.

I also tryed a cast to DecimalType but it doesn’t work (I get a couldn’t cast error)

Any other suggestions?

Easy enough :slight_smile:

rule "Rule_AAA"
when
    Item ThermoSoggiorno_T changed
or
    Item ThermoBagnoPT_T changed
then
    if(!(ThermoSoggiorno_T.state instanceof Number )) return; // check if ThermoSoggiorno_T.state is of type number
    if(!(ThermoBagnoPT_T.state instanceof Number )) return; // check if ThermoBagnoPT_T.state is of type number
    // both items have a number value, so calculate...
    var t = (ThermoSoggiorno_T.state as Number) + (ThermoBagnoPT_T.state as Number) // beware the brackets!
    TemperaturaPianoTerra.postUpdate(t)
end

Maybe you want to see if the rule was cancelled early…

rule "Rule_AAA"
when
    Item ThermoSoggiorno_T changed
or
    Item ThermoBagnoPT_T changed
then
    if(!(ThermoSoggiorno_T.state instanceof Number )) { // check if ThermoSoggiorno_T.state is of type number
        logWarn("aaa","ThermoSoggiorno_T has no valid value!")
        return; 
    }
    if(!(ThermoBagnoPT_T.state instanceof Number )) { // check if ThermoBagnoPT_T.state is of type number
        logWarn("aaa","ThermoBagnoPT_T has no valid value!")
        return; 
    }
    // both items have a number value, so calculate...

    var t = (ThermoSoggiorno_T.state as Number) + (ThermoBagnoPT_T.state as Number) // beware the brackets!
    TemperaturaPianoTerra.postUpdate(t)
end

Thank you Udo. But this way I avoid the error but don’t get the math done. Unfortunately this error is always fired not just in some cases.

The rule will run every time the Item changes, you can use cron to only run at a certain time.
What error are you getting?

So your rule is good but cannot work with bad values. Are your mystery Items actually Number types? (i.e. may we see your Item definitions)

Of course you can use a default value if no value is set, but it’s simply impossible to do math without valid values, so what’s the point?

Hi H102, this solved!! Now I get the avg temperature… but in °K !! How can I convert back to °C ?
thanks a lot!!!

tempC = tempK - 273.15

Does your temp item have units defined like [%.1f *C] the * should be the deg symbol.

Or as Vincent suggest, use the calculation in the rule before .postUpdate

rule “Rule_AAA”
when
Item ThermoSoggiorno_T changed
or
Item ThermoBagnoPT_T changed
then
var k = (ThermoSoggiorno_T.state as Number + ThermoBagnoPT_T.state as Number)
var t = k - 273.15
TemperaturaPianoTerra.postUpdate(t)
end

1 Like

Remember that the average is not the sum but the sum divided by tqo in the case:

var k = (ThermoSoggiorno_T.state as Number + ThermoBagnoPT_T.state as Number) / 2
1 Like

Yeah i was noting that in all the examples as well, I thought just to get it working they were just working with the sum…