Counter for pulses

Platform: Raspberry Pi with Openhabian

I have a control unit which send pulses as open and close commands to a valve. I would like to count the number of pulses to creat a virtual item heatingValvePosition showing the actual osition of the valve but I cant get the rule to work.

Here is my heating.items file:

Number heatingValveOpen "Valve open [%d]" { channel="modbus:data:endpoint:poller479:valveOpen:number" }
Number heatingValveClose "Valve close [%d]" { channel="modbus:data:endpoint:poller479:valveClose:number" }
Number heatingOutsideTemp "Outside temp [%d]" { channel="modbus:data:endpoint:poller15000:outsideTemp:number" }
Number heatingOutsideTempAccu "Outside temp [%d]" { channel="modbus:data:endpoint:poller900000:outsideTempAccu:number" }
Number heatingFlowTemp "Flow temp [%d]" { channel="modbus:data:endpoint:poller15000:flowTemp:number" }
Number heatingSetTemp "Set temp [%d]" { channel="modbus:data:endpoint:poller600000:setTemp:number" }
Number heatingPumpOn "Pump [%d]" { channel="modbus:data:endpoint:poller750000:pumpOn:number" }

Number heatingValvePosition "Valve position [%d]"

And the rules file:

rule "Startup"
when
    System started
then
    postUpdate(heatingValvePosition,0)
end

rule "Valve opens"
when
	Item heatingValveOpen received update 1
then
	postUpdate(heatingValvePosition,heatingValvePosition+1)
end

rule "Valve close"
when
	Item heatingValveClose received update 1
then
	//if (heatingValvePosition.state > 0)
	//{
		postUpdate(heatingValvePosition, heatingValvePosition-1)
	//}
end

But I get this error:

2017-12-25 12:13:18.721 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Valve close': Unknown variable or command '-'; line 21, column 36, length 22

What’s wrong?

Try this

rule "Startup"
when
    System started
then
    heatingValvePosition.postUpdate(0)
end

rule "Valve opens"
when
	Item heatingValveOpen received update 1
then
	heatingValvePosition.postUpdate((heatingValvePosition.state as Number) + 1)
end

rule "Valve close"
when
	Item heatingValveClose received update 1
then
	//if (heatingValvePosition.state > 0)
	//{
		heatingValvePosition.postUpdate((heatingValvePosition.state as Number) - 1)
	//}
end
1 Like

Seems to work, at least no error messages!

Thanks a lot!