Posts: 195
Threads: 53
Joined: Mar 2021
Reputation:
2
Hi guys,
I need your help with calculating today’s, weekly, monthly, and yearly energy values.
I was not able to manage it using the Trends library:
https://kb.logicmachine.net/libraries/trends/
Your help would be greatly appreciated.
What I need is: - One script that runs every hour, calculates today’s energy consumption, and writes it to an object.
- Another script that calculates the monthly energy consumption and write it into object . It should sum the daily energy values, run 1 per day, and reset at the beginning of each new month.
- Another script that calculates the yearly energy consumption. It should accumulate the values over the year and reset at the beginning of each new year.
I already have a trend for the total consumed active energy in kWh.
I would really appreciate your help in advance.
Posts: 8722
Threads: 48
Joined: Jun 2015
Reputation:
498
Create scheduled scripts that run at required intervals. For monthly/yearly mode the current day data won't be included into calculation because the data has not been aggregated yet.
Daily:
Code: require('trends')
name = 'energy'
dates = {}
dates['start'] = os.date('*t')
dates['end'] = os.date('*t')
dates['end'].day = dates['end'].day + 1
sum = 0
values = trends.fetch(name, dates)
for _, value in ipairs(values) do
sum = sum + (value or 0)
end
grp.checkupdate('0/0/1', sum)
Monthly:
Code: require('trends')
name = 'energy'
dates = {}
dates['start'] = os.date('*t')
dates['start'].day = 1
dates['end'] = os.date('*t')
sum = 0
values = trends.fetch(name, dates, 86400)
for _, value in ipairs(values) do
sum = sum + (value or 0)
end
grp.checkupdate('0/0/2', sum)
Yearly:
Code: require('trends')
name = 'energy'
dates = {}
dates['start'] = os.date('*t')
dates['start'].month = 1
dates['start'].day = 1
dates['end'] = os.date('*t')
sum = 0
values = trends.fetch(name, dates, 86400)
for _, value in ipairs(values) do
sum = sum + (value or 0)
end
grp.checkupdate('0/0/3', sum)
Posts: 195
Threads: 53
Joined: Mar 2021
Reputation:
2
Thank you for the great support and the clear solution. As always, very helpful!
Posts: 195
Threads: 53
Joined: Mar 2021
Reputation:
2
Hi again,
I will need to apply this for around 40 meters, with values for today, yesterday, this month, last month, this year, and last year.
Do you think there is any practical limit I should be careful not to exceed in terms of the number of scripts, trend queries, or objects to update?
Posts: 8722
Threads: 48
Joined: Jun 2015
Reputation:
498
Instead of making one script per trend you can use a single script for multiple trends:
Code: mapping = {
{ trend = 'energy A', output = '0/0/1' },
{ trend = 'energy B', output = '0/0/2' },
{ trend = 'energy C', output = '0/0/3' },
}
require('trends')
dates = {}
dates['start'] = os.date('*t')
dates['end'] = os.date('*t')
dates['end'].day = dates['end'].day + 1
for _, map in ipairs(mapping) do
sum = 0
values = trends.fetch(map.trend, dates)
for _, value in ipairs(values) do
sum = sum + (value or 0)
end
grp.checkupdate(map.output, sum)
os.sleep(1)
end
Posts: 195
Threads: 53
Joined: Mar 2021
Reputation:
2
(10.04.2026, 13:29)admin Wrote: Instead of making one script per trend you can use a single script for multiple trends:
Code: mapping = {
{ trend = 'energy A', output = '0/0/1' },
{ trend = 'energy B', output = '0/0/2' },
{ trend = 'energy C', output = '0/0/3' },
}
require('trends')
dates = {}
dates['start'] = os.date('*t')
dates['end'] = os.date('*t')
dates['end'].day = dates['end'].day + 1
for _, map in ipairs(mapping) do
sum = 0
values = trends.fetch(map.trend, dates)
for _, value in ipairs(values) do
sum = sum + (value or 0)
end
grp.checkupdate(map.output, sum)
os.sleep(1)
end
One trend per meter , so it will be 40
Posts: 8722
Threads: 48
Joined: Jun 2015
Reputation:
498
It should be ok since you don't need to run the script very often.
|