This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Calculated montly kwh from relay status
#2
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)
Done is better than perfect
Reply


Messages In This Thread
RE: Calculated montly kwh from relay status - by buuuudzik - 19.12.2018, 20:49

Forum Jump: