Beginners Rule

Hi,
Spend already some time on this rule which throws always an error:

 import org.openhab.model.script.actions.*

rule "electricity 15 Minutes"
when 
 	Time cron "0 0,15,30,45 0,5,10,15,20,25,30,35,40,45,50,55 * * * ?"
then

    var Number PrevE
    if  (ElectPre15Min =! null ) {
    	PrevE = ElectPre15Min.state
    }
    else {
    	PrevE = ElectCounter.state
    }
	var Number CurE = ElectCounter.state
	var Number DeltaE = (CurE - PrevE)*12
	postUpdate(ElectDelta15Min, DeltaE)
	postUpdate(ElectPre15Min, CurE)
end

rule "gas 15 Minutes"
when 
	Time cron "0 0,5,10,15,20,25,30,35,40,45,50,55 * * * ?"
then

    var Number PrevG
    if  (GasPre15Min.state =! null ) {
    	PrevG = GasPre15Min.state
    }
    else {
    	PrevG = GasCounter.state
    }
	var Number CurG = GasCounter.state
	var Number DeltaG = (CurG - PrevG)*12
	postUpdate(GasDelta15Min, DeltaG)
	postUpdate(GasPre15Min, CurG)
end

// Energy 
Group g_Energy 
Number GasCounter 		"HM Gas Counter  [%.2f m2]" 	<energy> (g_Energy) { channel="homematic:HM-ES-TX-WM:637956a7:MEQ0706399:1#GAS_ENERGY_COUNTER" } 
Number GasCurrent		"HM Gas Current [%.2f m2]" 	<energy> (g_Energy) { channel="homematic:HM-ES-TX-WM:637956a7:MEQ0706399:1#GAS_POWER" }
Number GasPre15Min 		"HM Gas Counter 15 Mins ago" <energy> (g_Energy)
Number GasDelta15Min	"HM Gas Delta 15 Mins"		<energy> (g_Energy)

Number ElectCounter 	"HM Electricity Counter [%.2f Wh]" <energy> (g_Energy) { channel="homematic:HM-ES-TX-WM:637956a7:MEQ0703969:1#ENERGY_COUNTER" } 
Number ElectCurrent 	"HM Electricity Current [%.2f W]" <energy> (g_Energy) { channel="homematic:HM-ES-TX-WM:637956a7:MEQ0703969:1#POWER" }
Number ElectPre15Min 	"HM Electricity Counter 15 Mins ago" <energy> (g_Energy)
Number ElectDelta15Min 	"HM Electricity Delta 15 Mins" <energy> (g_Energy)

Can someone give me hint whats wrong

2017-01-20 15:55:00.828 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule electricity 15 Minutes: An error occured during the script execution: The name '! <XNullLiteralImplCustom>' cannot be resolved to an item or type.

Eroor is thrown at:

    if  (ElectPre15Min =! null ) {

ElectPre15Min is an Openhab Item object. That’s never null, as it gets created when OH loads. Its value might be null before it gets initialized though. What you want is the state -

if (ElectPre15Min.state =! null ) { …

You got that already in the second rule, just an oversight.

Actually its if(ElectPre15Min.state != NULL) as an Item’s state is initialized to all caps NULL to indicate an Undefined state.

As for the cron, the following is a little easier to read:

Time cron "0 */15 * * * ?"

Finally it works with this code:

rule "elect15Minutes"
when 
 	Time cron "0 0/15 * * * ?"
then

    var Number PrevE
    if (ElectPre15Min.state != NULL)   {
    	PrevE = ElectPre15Min.state
    }
    else {
    	PrevE = ElectCounter.state
    }
	var Number CurE = ElectCounter.state
	var Number DeltaE = (CurE - PrevE)*12
	postUpdate(ElectDelta15Min, DeltaE)
	postUpdate(ElectPre15Min, CurE)
	logInfo("elect15Minutes", ElectDelta15Min.state.toString()) 
end