# Need help with a rule calculating difference between 2 numbers

Hi,
I use this rule to calculate absolute humidity:

``````rule "Calculate absolute humidity outside (g h2o / m3 air)"
when
Item BalconyTemperature changed or
Item BalconyHumidity changed
then
var temp = BalconyTemperature.state as DecimalType
var hum = BalconyHumidity.state as DecimalType
var Number c1 = ((17.67*temp.floatValue)/(temp.floatValue+243.5))
var abs = (Math::pow(Math::E,c1.doubleValue)*6.112*2.1674*hum.floatValue) /(273.15+temp.floatValue)
BalconyHumidityAbsolute.postUpdate(abs)
``````

now the item BalconyHumidityAbsolute is a Number, the state looks like this: 22.459291944913208

now i want to compare absolute humidity inside and outside, and trigger something if difference is bigger than X.

``````rule "Balcony Humidity Alert Virtual Switch"
when
Item BalconyHumidityAbsolute changed
then
val currMonth = now.getMonthValue
val currHour  = now.getHour
var Number diff = (LivingHumidityAbsolute.averageSince(now.minusMinutes(20)) - BalconyHumidityAbsolute.averageSince(now.minusMinutes(20)))
if(diff > 0.5 && Presence.state == ON && currMonth > 4 && currMonth < 12 && currHour > 11 && BalconyNice.state == OFF)
{BalconyNice.sendCommand(ON)}
if(BalconyHumidityAbsolute.averageSince(now.minusMinutes(20)) > LivingHumidityAbsolute.averageSince(now.minusMinutes(20)) && Presence.state == ON && currMonth > 4 && currMonth < 12 && currHour > 11 && BalconyNice.state == ON)
{BalconyNice.sendCommand(OFF)}
end
``````

but I guess I haven’t defined the var correctly, as i get this:

``````2023-08-13 18:13:56.133 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'misc-13' failed: An error occurred during the script execution: index=1, size=1 in misc
``````

EDIT: i had a mistake in loginfo, after fixing it, I’m able to calculate the diff like this:

`````` var Number inside = LivingHumidityAbsolute.state
var Number outside = BalconyHumidityAbsolute.state
var Number diff = outside - inside
``````

Can I get this working for BalconyHumidityAbsolute.averageSince(now.minusMinutes(20)) rather then just BalconyHumidityAbsolute.state which works now?

How about doing the same, like:

``````var Number i = BalconyHumidityAbsolute.averageSince(now.minusMinutes(20))
``````

tried it but getting some errors. nevermind, this is good enough (with current states)