Generator hours counter

Couldn’t agree more… I am tracking my energy use, and using both approaches. Must say that having the meter indexes at the begining of the month and throughout has bailed me out a number of times when there has been lost data… as you can still tell how much was used during the outage.

Hi @sipvoip, could you post the final code for the counter?

import java.lang.Math
import org.openhab.model.script.actions.*
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock

var org.joda.time.DateTime whenStarted = null
var Timer generator_status_timer = null
var Timer generator_load_timer = null
var Lock lock = new ReentrantLock()

rule "Generator Started"
when
	Item Generator_Auto received command ON or
	Item Generator_Override received command ON
then	
	logInfo("Testing", "Generator Started - Locking")
	lock.lock()
	try {
		sendCommand(Generator_Start, ON)
		Thread::sleep(45000) // Let Generator spool up
			if (Generator_Status.state == CLOSED) {
				sendCommand(Generator_Failed, OFF)
				sendCommand(Generator_Cooling, OFF)
				whenStarted = now // Start Clock
				generator_load_timer = createTimer(now.plusSeconds(15))
					[ 
						sendMail("8323303810@mms.att.net", "Generator", "Generator Started, load at " + Buy_Total_Watts.state + " watts")
					]
			} else {
				sendCommand(Generator_Failed, ON)
				sendCommand(Generator_Override, OFF)
				sendMail("8323303810@mms.att.net", "Generator", "Generator Failed to Start!!")
			}
	}
	finally {
		lock.unlock()
		logInfo("Testing", "Generator Started - Unlocked")
	}
end


rule "Generator Stopped"
when
	Item Generator_Auto received command OFF or
	Item Generator_Override received command OFF
then	
	lock.lock()
	try {
		sendCommand(Generator_Start, OFF)
		postUpdate(Generator_Auto, OFF)
		postUpdate(Generator_Override, OFF)
		postUpdate(Generator_Cooling, ON)
		Thread::sleep(90000) // Let Generator cool down
		postUpdate(Generator_Cooling, OFF)
    		whenStarted = null
	}
	finally {
		lock.unlock()
	}
end


rule "Check Generator and Increment Runtimes"
when
    	Time cron "0 0/1 * * * ?"
then	
	logInfo("Testing", "Check Generator and Increment Runtimes - Locking")
	lock.lock()
	try {
		// Check if Generator is running already
		if (Generator_Status.state == CLOSED && whenStarted == null) whenStarted = now 
		if (Generator_Status.state == CLOSED && Generator_Auto.state == OFF && Generator_Override.state == OFF && Generator_Cooling.state == OFF) {
			postUpdate(Generator_Override, ON)
			whenStarted = now
			sendMail("8323303810@mms.att.net", "Generator", "Generator Started Manually")
		}

		// Check if Generator failed
		if (Generator_Status.state == OPEN && Generator_Auto.state == ON) {
			sendCommand(Generator_Auto, OFF)
			sendMail("8323303810@mms.att.net", "Generator", "Generator Failed!!")
		}
	
		// Increment Runtimes
		if(Generator_Status.state == CLOSED) {
			val long nowMSec = now.millis
			val long wsMSec = whenStarted.millis
			whenStarted = now	
        		val long timeSinceLastUpdate = nowMSec - wsMSec
			val long oldVal = (Generator_Runtime_MSec.state as Number).longValue
			val long totalMSec = oldVal + timeSinceLastUpdate // calculate total runtime
        		Generator_Runtime_MSec.postUpdate(totalMSec) // post the full runtime
			val double hours = totalMSec/1000.0/60.0/60.0
        		Generator_Runtime_Hours.postUpdate(hours)

			// Caculate runtime for today		
			val long todayOldVal = if(Today_Generator_Runtime_MSec.state == Uninitialized) 0 else (Today_Generator_Runtime_MSec.state as Number).longValue
			val long todayTotalMSec = todayOldVal + timeSinceLastUpdate // calculate total runtime
			Today_Generator_Runtime_MSec.postUpdate(todayTotalMSec) // post the full runtime
			val double todayHours = todayTotalMSec/1000.0/60.0/60.0
        		Today_Generator_Runtime_Hours.postUpdate(todayHours)
		}
	}
	finally {
		lock.unlock()
		logInfo("Testing", "Check Generator and Increment Runtimes - Unlocked")
	}
end


rule "Today Generator Runtime Reset"
when
	Time cron "50 59 23 * * ?"
then
	Today_Generator_Runtime_MSec.postUpdate(0)
end
4 Likes

Very nice thread ! What is your hardware? I mean what kind is your generator used? How to connect it to OpenHab? @sipvoip

Generac 20kw and set it up for 2 wire start and then control it via a relay off a pokeys 57 board.

how can you connect them to openhab? what was the setup and configuration?

I’m looking at porting your rule to control a 30HP industrial compressor
of all these variable I’d like help understanding what one are connected to physical switches and sensors and what are virtual switches and counters as well as all the items that need to be in the items file

generator_status_timer				//Declared but never used
generator_load_timer					Rule_Number				//Delay timer for load reading
Buy_Total_Watts.state					Item_Number_Physical	//Power reading from other device
Generator_Auto			"ON/OFF"
Generator_Override		"ON/OFF"
Generator_Start			"ON/OFF"
Generator_Status		"OPENED/CLOSED"	Item_Contact_Physical
Generator_Failed		"ON/OFF"		Item_Switch_Virtual		//genset Failed Indicator
Generator_Cooling		"ON/OFF"		Item_Switch_Virtual		//Delayed mode indicator to allow genset to cool before restart
whenStarted				"null/TIME"
Generator_Runtime_MSec					Item_Number_Virtual		// used to store intermediate values which get rounded
Generator_Runtime_Hours	"%.1f Hours"	Item_Number_Virtual		// used on sitemap
Today_Generator_Runtime_MSec
todayTotalMSec
todayOldVal
timeSinceLastUpdate
todayHours
Today_Generator_Runtime_Hours

Thanks,