OH 1.7 Persistence - Log On/Off Time

If you want to track the duration of a switch state, you can do that with rules. Here is an example.

Items:

Switch DemoSwitch
Number DemoSwitch_On_Duration   "duration total"
Number DemoSwitch_On_Asc   "duration ascending" 

Persistence:

Strategies {
	everyMinute : "0 * * * * ?"
}

Items {
	DemoSwitch : strategy = everyChange
	DemoSwitch_On_Duration,DemoSwitch_On_Asc : strategy = everyMinute, restoreOnStartup
}

Rules:

rule "Switch time ascending"
when
	Time cron "0 * * * * ?" 
then
    if( DemoSwitch.state == ON ) {
        var DateTime m =  new DateTime(DemoSwitch.lastUpdate )
	var diff = now.millis - m.millis;
	DemoSwitch_On_Asc.postUpdate( diff );
	logInfo("Track Ascending Duration", "demoSwitch changed last after  " + diff  )	
    }
    else {
       DemoSwitch_On_Asc.postUpdate( 0 );
    }
end

rule "Switch On time total"
when
	Item DemoSwitch received command
then
    if( receivedCommand==OFF ) {
        var DateTime m =  new DateTime(DemoSwitch.lastUpdate )
	var diff = now.millis - m.millis;
	DemoSwitch_On_Duration.postUpdate( diff );
	logInfo("Track On Duration", "demoSwitch on duration was  " + diff  )	
    }
    else {
    	logInfo("Track On Duration", "demoSwitch is now on "  )
    	
    }
end

The “DemoSwitch_On_As” number will lead to an ascending graph, the longer a switch stays on, starting/hovering over zero when the switch is off. The DemoSwitch_On_Duration will track the duration a switch was on.