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 whether you accept or reject these cookies being set.

Energy calculation and data saving
#1
Dear all,
I manage some energy counters (energy produced by photovoltaic, energy sold to the network) and in the meanwhile I calculate the self-consumption energy. Daily and total. Here below is shown the script I did and I would like to ask you if there's a best way to improve it.

The system works fine but I would like to know if there's a better way to manage this process.

1. Scheduled script to read the value of energy instrument at the beginning of the day (energy counter of instrument cannot be reset). Hence I find this method to obtain daily data.

Code:
-- script run every day at 0:0 am.
-- script to get value from instrument of photovoltaic energy and energy sold.

evenduta = grp.getvalue('3/4/16') -- read from the instrument value of energy sold

grp.write('32/1/10', evenduta) -- write to morning_energy_sold which has address 32/1/10

-- evenduta_mattino = grp.getvalue('32/1/10')

ftv = grp.getvalue('3/4/28') -- read from the instrument value of energy produced by photovoltaic
grp.write('32/1/11', ftv) -- write to morning_energy_photovoltaic which has address 32/1/11

2. Scheduled script to calculate energy product by photovoltaic and self-consumption energy.


Code:
-- script runs every day, every hour at 59 minutes
-- script to calculate the daily energy product by photovoltaic and self-consumption energy. The last script of the day will give the result of the day.

ftv = grp.getvalue('3/4/28') -- energy read by instrument of photovoltaic.
ftv_mattino = grp.getvalue('32/1/11') -- photovoltaic energy read by morning script.

evenduta = grp.getvalue('3/4/16') -- energy read by instrument of energy sold.
evenduta_mattino = grp.getvalue('32/1/10') -- energy-sold read by morning script.

delta_ftv = ftv - ftv_mattino -- difference to calculate daily photovoltaic energy.
delta_evenduta = evenduta - evenduta_mattino -- difference to calculate daily energy sold.

grp.write('32/1/12', delta_ftv)
grp.write('32/1/13', delta_evenduta)

energia_autoconsumata = delta_ftv - delta_evenduta -- difference to calculate self-consumption energy

grp.write('32/1/15', energia_autoconsumata) -- daily value of self-consumption energy

autoconsumo_perc = (energia_autoconsumata / delta_ftv) * 100 -- % value of daily self-consumption energy

grp.write('32/1/14', autoconsumo_perc)


3. Scheduled script to calculate the total self-consumption energy.
Code:
-- script runs all days, at 11 pm at 59 minutes.
-- script to calculate total self-consumption energy.

daily = grp.getvalue('32/1/15') -- read daily self-consumption energy

os.sleep(2)

total = storage.get('autoconsumo_totale') -- get from system self-consumption energy

os.sleep(2)

total = daily + total -- add to total the daily value

os.sleep(2)

grp.write('32/1/16', total) -- write to "Total self-consumption energy" address

storage.set('autoconsumo_totale', total) -- save on system new "Total self-consumption energy"

Thanks.

BR
Reply
#2
Looks good to me. It can also be done without the third script by adding this to the end of the second script that runs every hour at 59 minutes:
Code:
-- run only on the last hour of the day
hour = os.date('*t').hour
if hour == 23 then
  total = energia_autoconsumata + grp.getvalue('32/1/16')
  grp.write('32/1/16', total)
end
Reply
#3
(29.04.2021, 07:57)admin Wrote: Looks good to me. It can also be done without the third script by adding this to the end of the second script that runs every hour at 59 minutes:
Code:
-- run only on the last hour of the day
hour = os.date('*t').hour
if hour == 23 then
  total = energia_autoconsumata + grp.getvalue('32/1/16')
  grp.write('32/1/16', total)
end

Thank you. Further to save the data do you suggest to NOT use the "storage"?

Group address is data retain in any case?

Thanks.
Reply
#4
There's no big difference which method to use. Object values are synced the same way as storage is. Just make sure that the object data type can store the value without overflowing. Use either 4 byte floating point or 8 byte signed integer depending on your data.
Reply


Forum Jump: