Calculate time on and energy use for an item/switch rule

Hello

I manage to use this code for a TV, which is plugged in a strip.
Now I want to find a solution for all 3 sockets from the strip. Can this be done with members? Or should i create 2 times more the rule?

my rule:

import java.lang.Math

var org.joda.time.DateTime whenStarted = null

rule “LGTV started”
when
Item TellurPowerStripTellurPower1 received command ON
then
whenStarted = now
end

rule “LGTV stopped”
when
Item TellurPowerStripTellurPower1 received command OFF
then
if(whenStarted != null) {
// do the same as the if(TellurPowerStripTellurPower1.state == ON && whenStarted != null) in the increment rule
}
whenStarted = null
end

rule “Increment LGTV Runtimes”
when
Time cron “* */1 * * * ?”
then
if(TellurPowerStripTellurPower1.state == ON && whenStarted != null){
logInfo(“GenUptime”, “LGTV: Getting now”)
val long nowMsec = now.millis

    logInfo("GenUptime", "LGTV: Getting whenStarted")
    val long wsMsec = whenStarted.millis

    logInfo("GenUptime", "LGTV: Resetting whenStarted")
    whenStarted = now

    logInfo("GenUptime", "LGTV: Calculating time difference") 
    val long timeSinceLastUpdate = nowMsec - wsMsec

    logInfo("GenUptime", "LGTV: Getting total Uptime in msec")
    //val long oldVal = TellurPowerStripTellurPower1RuntimeMSec.state
    val long oldVal = if(TellurPowerStripTellurPower1RuntimeMSec.state === NULL) 0 else (TellurPowerStripTellurPower1RuntimeMSec.state as Number).longValue
    
    //logInfo("GenUptime", "Adding current time to total")
    logInfo("GenUpTime", "LGTV: Adding current time to total: timeSinceLastUpdate = " + timeSinceLastUpdate + " oldVal = " + oldVal)
    val long totalMsec = oldVal + timeSinceLastUpdate // calculate total runtime
    
    logInfo("GenUptime", "LGTV: Posting totalMsec to Item")
    TellurPowerStripTellurPower1RuntimeMSec.postUpdate(totalMsec) // post the full runtime

    logInfo("GenUptime", "LGTV: Calculating hours")
    //val long hours = Math::round(totalMsec/1000/60/60)
    val double hours = totalMsec/1000.0/60.0/60.0
    logInfo("GenUptime", "LGTV: Posting hours")
    TellurPowerStripTellurPower1RuntimeHours.postUpdate(hours)

    logInfo("GenUptime", "LGTV: Calculate energy consumption")
    var float LGTVPower = 130             // Enter total power consumption [in Watts]
    val double LGTVEnergyConsumption = hours * LGTVPower / 1000                  // Energy consumed [kWh] Energy = Power * Time
    LGTVEnergy.postUpdate(LGTVEnergyConsumption)
    }
else if(TellurPowerStripTellurPower1.state == ON && whenStarted == null){
    logInfo("GenUptime", "LGTV: TV is ON but 'LGTV started' rule never executed")
    whenStarted = now
}
else {
    logInfo("GenUptime", "LGTV is not running")
}

end

items:
Switch TellurPowerStripTellurPower1 “On/off switch” (gTellur) [“Outlet”] {channel=“mqtt:topic:46425acf:Tellur_Strip:Tellur_Power1”}
Switch TellurPowerStripTellurPower2 “On/off switch” (gTellur) [“Outlet”] {channel=“mqtt:topic:46425acf:Tellur_Strip:Tellur_Power2”}
Switch TellurPowerStripTellurPower3 “On/off switch” (gTellur) [“Outlet”] {channel=“mqtt:topic:46425acf:Tellur_Strip:Tellur_Power3”}
Number LGTVEnergy “TV Socket1 Energy Consumption [%.2f kWh]” (gEnergyTellur)
Number TellurPowerStripTellurPower1RuntimeHours “LGTV Runtime Hours [%.2f Hours]”
Number TellurPowerStripTellurPower1RuntimeMSec "LGTV Runtime "

Are you sure you want a rule which triggers once a second? Three times? Really? If using persistence, you can get the runtime through the persistence itself (lastChange(true) if I remember it right), of course without update once a second.