Check if switch was ON during last 2 minutes

Hi Folks,

I have a simple rain sensor, which changes a switch item to ON when it’s wet and back to OFF when it’s dry. Because of integrated heating it can happen, that the state changes quite often when the rain is light.

To awoid the frequent change on the sitemap, I need a rule which tells me if state was ON at least once in the last 2 minutes and change a proxy rain sensor switch ON then. Else to OFF.

Persistance is availible.

Thanks for your ideas

If just using Rules DSL you could use the `expire1 binding and set/reset a 2 minute timer when the switch is tumed on.
If the Timer is still running it has been ;less than 2 minutes.

<item>.changedSince(AbstractInstant)
Checks if the State of the Item has (ever) changed since a certain point in time

Try it like this … (NOT TESTED BY ME)

rule "Rain Sensor"
when
	Time cron "0 */2 * * * ?"
then
	if (PROXY_RAIN_SENOR.state == OFF) {
		if (RAIN_SENSOR.changedSince(now.minusMinutes(2))) {
			PROXY_RAIN_SENOR.sendCommand(ON)
		} 
	} else if (PROXY_RAIN_SENOR.state == ON) {
		if (!RAIN_SENSOR.changedSince(now.minusMinutes(2))) {
			PROXY_RAIN_SENOR.sendCommand(OFF)
		} 
	} else {
		//if the state of PROXY_RAIN_SENOR is NULL or UNDEF
		if (RAIN_SENSOR.state == ON) {
			PROXY_RAIN_SENOR.sendCommand(ON)
		} else {
				PROXY_RAIN_SENOR.sendCommand(OFF)
		}
	}
end

If I read my first code again, it will not work that way. But with the code you already have a base.

Perhaps this?

rule "Rain Sensor"
when
	Item RAIN_SENSOR changed
then
	if (PROXY_RAIN_SENOR.state == OFF && RAIN_SENSOR.state == ON) {
		PROXY_RAIN_SENOR.sendCommand(ON)
	} else if (PROXY_RAIN_SENOR.state == ON && RAIN_SENSOR.state == OFF) {
		if (!RAIN_SENSOR.changedSince(now.minusMinutes(2))) {
			PROXY_RAIN_SENOR.sendCommand(OFF)
		}
	} else {
			if (RAIN_SENSOR.state == ON) {
				PROXY_RAIN_SENOR.sendCommand(ON)
			} else {
					PROXY_RAIN_SENOR.sendCommand(OFF)
				}
		}
end

Hi Kevin,

thanks for your support. What’s exactly wrong with your first idea? I’ve tested it and it seems to fit my needs. I’ve just added a trigger by RainSensor.state to get rain information immediately.

rule "Regensensor verzoegerung"
when
	Time cron "0 */2 * * * ?" or
    Item RainSensor received update
then
	if (RainSensorProxy.state == OPEN) {
		if (RainSensor.changedSince(now.minusMinutes(2))) {
			RainSensorProxy.postUpdate(CLOSED)
		} 
	} else if (RainSensorProxy.state == CLOSED) {
		if (!RainSensor.changedSince(now.minusMinutes(2))) {
			RainSensorProxy.postUpdate(OPEN)
		} 
	} else {
		//if the state of RainSensorProxy is NULL or UNDEF
		if (RainSensor.state == ON) {
			RainSensorProxy.postUpdate(OPEN)
		} else {
				RainSensorProxy.postUpdate(CLOSED)
		}
	}
end

:no_mouth: after couple of rainy days your second idea solved my problem. Thank you
:upside_down_face:

1 Like