Problems using historic state

Hi,

I am trying to set up a rule which turns the fan in the bathroom on when having an increase in humidity of at least 5% within the last 5 minutes. Unfortunately, I am battleing with issues for three evenings now.

My Code (one of the various attempts) is:

rule “Fan activate”
when Item CurrentHum_LaCrosse_Arbeitszimmer changed
then
if (((CurrentHum_LaCrosse_Arbeitszimmer.state as DecimalType).floatValue - (CurrentHum_LaCrosse_Arbeitszimmer.historicState(now.minusMinutes(5)) as DecimalType).floatValue)>5){
Fan_Bathroom.sendCommand(ON)
Thread::sleep(5000)
Fan_Bathroom.sendCommand(OFF)
}
end

I also tried “as Number” without the .floatType and some other variants.

The Error I am getting when the humidity changes is:
2019-02-28 22:31:59.234 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Fan activate’: Could not cast org.openhab.core.persistence.internal.QueryablePersistenceServiceDelegate$1@7c9b98 to org.eclipse.smarthome.core.library.types.DecimalType; line 8, column 77, length 83

(line8 column 77 is the start of the historic item request)


I am running openhabian-pi
latest updates installed
using influxdb as persistence service (data is stored an viewable in grafana) - influx is set as standard persistence service.

do you have an idea what could be wrong?

Thanks in advance

Christian

historicState returns a HistoricItem. This has both the timestamp of the state and the state itself. You need to use

CurrentHum_LaCrosse_Arbeitszimmer.historicState(now.minusMinutes(5)).state as Number

I recommend Number of DecimalType. You do not need to call floatValue to compare it to 5.

Consider using a Timer instead of a long sleep like this. Long sleeps can cause problems.