# Counter for pulses

**URL:** <https://community.openhab.org/t/counter-for-pulses/37751>\
**Category:** Scripts & Rules\
**Created:** [December 25, 2017, 11:07am UTC](https://community.openhab.org/t/counter-for-pulses/37751 "2017-12-25T11:07:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviborg](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@aviborg](https://community.openhab.org/u/aviborg)\
**Post date:** [December 25, 2017, 11:07am UTC](https://community.openhab.org/t/counter-for-pulses/37751/1 "2017-12-25T11:07:21Z")

</div>

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:

```csv
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:

```php
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![mhilbush](https://community.openhab.org/user_avatar/community.openhab.org/mhilbush/32/56108_2.png) [@mhilbush](https://community.openhab.org/u/mhilbush)\
**Post date:** [December 25, 2017, 12:01pm UTC](https://community.openhab.org/t/counter-for-pulses/37751/2 "2017-12-25T12:01:00Z")

</div>

Try this

```auto
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 &gt; 0)
	//{
		heatingValvePosition.postUpdate((heatingValvePosition.state as Number) - 1)
	//}
end

```

---

<div class="post-metadata">

**Author:** ![aviborg](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@aviborg](https://community.openhab.org/u/aviborg)\
**Post date:** [December 25, 2017, 12:08pm UTC](https://community.openhab.org/t/counter-for-pulses/37751/3 "2017-12-25T12:08:18Z")

</div>

Seems to work, at least no error messages!

Thanks a lot!
