How to initialise variables

var GasCount = 0.0

rule "Gas System Calc"
when
	Item ReedSwitch1 changed from 1 to 0
then
	GasCount = GasCount + 0.1
	GasCounter.postUpdate(GasCount)		
end

Issues:

  1. I want that GasCount’s initial value is 0.0 rather than null.
    How to? The initial condition did not work.

  2. I want to reset the value to 0.0 at midnight.
    I failed to make a 2nd rule work within the same rule file…

Thanks.

Hi again,

If you want to use variables inside a rule, the syntax is

val Number GasCount = 0.0

… where GasCount is a temp variable of a Number type (not an item)

Anyway, if I understand correctly your would like to reset the value of item GasCount (defined in an .items file) to 0.0 when the systems starts up and at midnight? If so you could try the following rules.

rule "System restart"
when
        System started
then
        // initialize your item GasCound here
        GasCount.postUpdate(0.0)
end

// ***************************************

rule "Gas System Reset"

when
        // at 00:00 every day
        Time cron "0 0 0 1/1 * ? *"
then

        // reset your item GasCound here
        GasCount.postUpdate(0.0)

      // Create a log entry in openhab.log to keep track
        logInfo("Rule - Gas System Reset","GasCount set to 0.0")
end

Both rules may be in the same file if you prefer

Not exactly.

GasCount is the calculated value - the variable.
GasCounter is the label to which I give it the value of GasCount.

I do not mind so much that GasCounter is null at first.
But I mind that I cannot pre-define GasCount.

Either way - your explanation helped a lot.

Thanks! :slight_smile:

var Number GasCount = 0.0

rule "Gas System Calc"
when
	Item ReedSwitch1 changed from 1 to 0
then
	GasCount = GasCount + 0.1
	GasCounter.postUpdate(GasCount)		
end

rule "System restart"
when
        System started
then
		GasCount = 0.0
        // initialize your item GasCound here
		GasCounter.postUpdate(GasCount)	
end

// ***************************************

rule "Gas System Reset"

when
        // at 00:00 every day
        Time cron "0 0 0 1/1 * ? *"
then
		GasCount = 0.0
        // reset your item GasCound here
		GasCounter.postUpdate(GasCount)	

      // Create a log entry in openhab.log to keep track
        logInfo("Rule - Gas System Reset","GasCount set to 0.0")
end

Is there a way I can save a variable to file and load it on the next system restart?

Thanks.