Logic Machine Forum
Calculated montly kwh from relay status - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Calculated montly kwh from relay status (/showthread.php?tid=1802)



Calculated montly kwh from relay status - Kai-Roger - 19.12.2018

Hi.

I want to make a simple and cost efficiant way to have an montly overview of kwh for usage of underfloor heatingcables.
How much kW the heatingcable is delivering is easily measured with instruments and the measured kW is accurate enough for most uses.

My plan is to count the time while the relay for the heating cable is active, and multiply this time with the cables kW for every hour of active relay.
Then i want to extract the kWh for each previous month, and add it to a trend.
This will when time goes by show the last 2 years of heating cable kWh usage.

Does anybody have a script with something similar that can help me on my way?

BR
Kai-Roger


RE: Calculated montly kwh from relay status - buuuudzik - 19.12.2018

You could use something like below. This is for event-based script connected to the status of your heating load. It automatically generates data of your consumption in kWh. You can also add the cost parameter to have it in same place.

All current calculations are available in storage. Value is refreshed on every change or update of status.
Code:
// Event based script
// Connect with load status

status = event.getvalue()
now = os.time()

-- power in kW
power = 2

-- price per kWh
price = 0.5
currency = '$'

energyCounter = storage.get('energy_counter', {price = price, currency = currency, consumers = {}})

-- load should have "status", "power", "lastChange", "counter"
load = energyCounter.consumers['heating-cables']

if not load then
    load = {status = status, power = 2000, lastChange = now, counter = 0}
end

function updateConsumption()
    -- consumed kWh value of last not calculated period
    local delta = (now - load.lastChange) / 3600 * load.power
    
    if load.status then
        -- add current consumption to the counter
        load.counter = load.counter + delta
        load.status = status
        load.lastChange = now
    end
    
    energyCounter.consumers['heating-cables'] = load
    storage.set('energy_counter', energyCounter)
end


updateConsumption()


And below example is how to use data in your monthly script:
Code:
energyCounter = storage.get('energy_counter', {price = price, currency = currency, consumers = {}})

-- load should have "status", "power", "lastChange", "counter"
load = energyCounter.consumers['heating-cables']

counter = load.counter // in kWh
log(counter)



RE: Calculated montly kwh from relay status - Kai-Roger - 19.12.2018

Thanks. I will try to understand the code better tomorrow (: