Energy meter rules

Hi,
I’m looking for rules counting the number of pulses from the energy meter. I found a few but none works on openhab 2.5. Since version 2.3 there is a problem with the error “Could not cast NULL to org.eclipse.smarthome.core.library.types.DecimalType”.
Do you know any working rules for openhab 2.5?
I tried to use one but there is the mentioned error for the selected line.

var impulsy = 0

rule "counter"
when 
Item GPIO_BUTTON changed to OPEN
then
**var last_change = GPIOdlugosc.state as DecimalType**
var totalTime = ((now.millis - last_change) / 1000)
impulsy = impulsy + 1
if (totalTime > 2) {
var newValue = (GPIOlicznik.state as DecimalType).intValue()
var newValuemies = (GPIOliczmies.state as DecimalType).intValue() 
var totalEnergy = (1440 * impulsy)/ totalTime
newValue = newValue + impulsy
newValuemies = newValuemies + impulsy
GPIOlicznik.postUpdate(newValue)
GPIOliczmies.postUpdate(newValuemies)
GPIOdlugosc.postUpdate(now.millis)
PoborMocy.postUpdate(totalEnergy)
KWHdzis.postUpdate(newValue * 0.0004)
KWHtenmiesiac.postUpdate(newValuemies * 0.0004)
impulsy = 0
}
end

rule "daily reset"
when
Time cron "0 0 0 * * ?"
then
KWHdzienny.postUpdate(KWHdzis.state as DecimalType)
KWHdzis.postUpdate(0)
GPIOlicznik.postUpdate(0)
end

rule "monthly reset"
when
Time cron "1 0 0 1 * ?"
then
KWHmiesieczny.postUpdate(KWHtenmiesiac.state as DecimalType)
KWHtenmiesiac.postUpdate(0)
GPIOliczmies.postUpdate(0)
end

Well, as the error states, GPIOdlugosc is not a DecimalType. It’s of type NULL. That Item has never had a state update since openHAB started. No rule can work if the Item doesn’t actually have a state.