Hi all, I am trying to calculate the runtime of my greenhouse heater with the final goal of calculating the amount of propane we use. I have looked through a good many posts and the one that is closest is the generator runtime.
I set the rule up as I best I could figure out. I am close I think but throwing an error. I am also confused on what can be posted out the sitemap and what can persist as I will want to graph/calculate/monitor…
Here are my items. I have two of them commented out as I dont think I need them here?
Switch MainHeater "Main Heater" <switch> (persist,gAllHeater) [ "Switchable"]
{ mqtt=">[broker:cmnd/sonoff_7F5C1F/POWER:command:*:default],
<[broker:stat/sonoff_7F5C1F/POWER:state:default]" }
Number MainHeaterRuntimeHours "MainHeater Runtime [%.1f Hours]" <heater>
//Number MainHeater_On_Duration "Heater Duration Total"
//Number MainHeater_On_Asc "Heater Duration Ascending"
Here is my rule:
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 MainHeater_status_timer = null
var Timer MainHeater_load_timer = null
var Lock lock = new ReentrantLock()
rule "Main Heater Started"
when
Item MainHeater received command ON
then
logInfo("Testing", "Main Heater Started - Locking")
lock.lock()
try {
sendCommand(MainHeater, ON)
Thread::sleep(45000) // Let MainHeater spool up
if (MainHeater_Status.state == CLOSED) {
sendCommand(MainHeater_Failed, OFF)
sendCommand(MainHeater_Cooling, OFF)
whenStarted = now // Start Clock
MainHeater_load_timer = createTimer(now.plusSeconds(15))
[
sendMail("myemail@gmail.com", "MainHeater", "Main Heater Started, load at " + Buy_Total_Watts.state + " watts")
]
} else {
sendCommand(MainHeater_Failed, ON)
sendCommand(MainHeater_Override, OFF)
sendMail("myemail@gmail.com", "Main Heater", "Main Heater Failed to Start!!")
}
}
finally {
lock.unlock()
logInfo("Testing", "Main Heater Started - Unlocked")
}
end
rule "Main Heater Stopped"
when
Item MainHeater received command OFF
then
lock.lock()
try {at
sendCommand(MainHeater, OFF)
postUpdate(MainHeater, OFF)
postUpdate(MainHeater_Cooling, ON)
Thread::sleep(90000) // Let Main Heater cool down
postUpdate(MainHeater, OFF)
whenStarted = null
}
finally {
lock.unlock()
}
end
rule "Check Main Heater and Increment Runtimes"
when
Time cron "0 0/1 * * * ?"
then
logInfo("Testing", "Check Main Heater and Increment Runtimes - Locking")
lock.lock()
try {
// Check if MainHeater is running already
if (MainHeater_Status.state == CLOSED && whenStarted == null) whenStarted = now
if (MainHeater_Status.state == CLOSED && MainHeater_Auto.state == OFF && MainHeater_Override.state == OFF && MainHeater_Cooling.state == OFF) {
postUpdate(MainHeater_Override, ON)
whenStarted = now
sendMail("mymail@gmail.com", "Main Heater", "Main Heater Started Manually")
}
// Check if MainHeater failed
if (MainHeater_Status.state == OPEN && MainHeater_Auto.state == ON) {
sendCommand(MainHeater_Auto, OFF)
sendMail("mymail@gmail.com", "Main Heater", "Main Heater Failed!!")
}
// Increment Runtimes
if(MainHeater_Status.state == CLOSED) {
val long nowMSec = now.millis
val long wsMSec = whenStarted.millis
whenStarted = now
val long timeSinceLastUpdate = nowMSec - wsMSec
val long oldVal = (MainHeater_Runtime_MSec.state as Number).longValue
val long totalMSec = oldVal + timeSinceLastUpdate // calculate total runtime
MainHeater_Runtime_MSec.postUpdate(totalMSec) // post the full runtime
val double hours = totalMSec/1000.0/60.0/60.0
MainHeater_Runtime_Hours.postUpdate(hours)
// Caculate runtime for today
val long todayOldVal = if(Today_MainHeater_Runtime_MSec.state == Uninitialized) 0 else (Today_MainHeater_Runtime_MSec.state as Number).longValue
val long todayTotalMSec = todayOldVal + timeSinceLastUpdate // calculate total runtime
Today_MainHeater_Runtime_MSec.postUpdate(todayTotalMSec) // post the full runtime
val double todayHours = todayTotalMSec/1000.0/60.0/60.0
Today_MainHeater_Runtime_Hours.postUpdate(todayHours)
}
}
finally {
lock.unlock()
logInfo("Testing", "Check Main Heater and Increment Runtimes - Unlocked")
}
end
rule "Today Main Heater Runtime Reset"
when
Time cron "50 59 23 * * ?"
then
Today_MainHeater_Runtime_MSec.postUpdate(0)
end
Here is what I put in the sitemap:
Text item=MainHeaterRuntimeHours
Here is my error:
[ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Check Main Heater and Increment Runtimes': The name 'MainHeater_Status' cannot be resolved to an item or type; line 70, column 7, length 17
Hope thats enough info.
thanks!