19.12.2018, 20:49
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.
And below example is how to use data in your monthly script:
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