[SOLVED] Easiest way to calculate total runtime?

Hi,
I am trying to sum up the total runtime of my machine and show it on my sitemap (and maybe use it for notification purposes).
I’ve checked some posts but they seem all very complicated and I could not manage to get one to work so far.

My code looks like this:

var Number Epoch_Time = 0
var Number Machine_Runtime = 0



rule "Calculate Machine Runtime"
when
    Item Machine_Switch changed
then
    Epoch_Time = now.millis
    if (Machine_Switch.state == OFF) Machine_Stop_Time.postUpdate(Epoch_Time)
    if (Machine_Switch.state == ON) {
            Machine_Start_Time.postUpdate(Epoch_Time)
            Machine_Stop_Time.postUpdate(NULL)
        }
    if ((Machine_Start_Time.state > 0) && (Machine_Stop_Time.state) > 0) {
        Machine_Runtime = ((Machine_Stop_Time.state as Number) - (Machine_Start_Time.state as Number)) / 60000
        Machine_Runtime_Minutes.postUpdate(Machine_Runtime)
        logInfo("Machine", "Machine Runtime - " + Machine_Runtime)
        var Number totalruntime = (Machine_Runtime_Minutes_Total.state as Number)+(Machine_Runtime)
        Machine_Runtime_Minutes_Total.postUpdate(totalruntime)
        }
end

Items:

Number Machine_Start_Time "Washing Machine Stop Time [%d]" // Stores Epoch timestamp

Number Machine_Stop_Time "Washing Machine Stop Time [%d]" //Stores Epoch timestamp

Number Machine_Runtme_Minutes "Washing Machine Runtime Minutes [%d]" // Stores runtime minutes

Number Machine_Runtime_Minutes_Total "Washing Machine Runtime Minutes [%d]"//Stores total runtime 

Right now, I get the error
Rule 'Calculate Machine Runtime': Could not cast NULL to java.lang.Number; line 21, column 36, length 45

EDIT:
Managed to fix my own problem.
Seems like the rule can’t handle the inital state of the Minutes_total item (NULL), so replacing it with 0 works. Either do it manually or like me:

if(Machine_Runtime_Minutes_Total.state == NULL) Machine_Runtime_Minutes_Total.postUpdate("0")

Fixed it.
Managed to fix my own problem.
Seems like the rule can’t handle the inital state of the Minutes_total item (NULL), so replacing it with 0 works. Either do it manually or like me:

if(Machine_Runtime_Minutes_Total.state == NULL) Machine_Runtime_Minutes_Total.postUpdate("0")
1 Like