Timedependent output from string - ClausP - 29.11.2022
After a lot of help from Admin, im ended up with this piece of code:
Code: url = 'https://api.energidataservice.dk/dataset/Elspotprices?offset=0&filter=%7B%22PriceArea%22:%22dk1%22%7D&sort=HourDK%20DESC&timezone=dk&limit=24'
resp = require('ssl.https').request(url)
data = require('json').decode(resp).records
max = 0 --nulstiller max værdi
min = math.huge --nulstiller min værdi med uendeligt stort tal
sum = 0 --nulstiller sum værdi
--Skriver time:minutter i "tid".
-- %a abbreviated weekday name (e.g., Wed)
-- %A full weekday name (e.g., Wednesday)
-- %b abbreviated month name (e.g., Sep)
-- %B full month name (e.g., September)
-- %c date and time (e.g., 09/16/98 23:48:10)
-- %d day of the month (16) [01-31]
-- %H hour, using a 24-hour clock (23) [00-23]
-- %I hour, using a 12-hour clock (11) [01-12]
-- %M minute (48) [00-59]
-- %m month (09) [01-12]
-- %p either "am" or "pm" (pm)
-- %S second (10) [00-61]
-- %w weekday (3) [0-6 = Sunday-Saturday]
-- %x date (e.g., 09/16/98)
-- %X time (e.g., 23:48:10)
-- %Y full year (1998)
-- %y two-digit year (98) [00-99]
-- %% the character `%´
tidtimer = os.date("%H")
tidminutter = os.date("%M")
for i, item in ipairs(data) do
price = item.SpotPriceDKK/1000 --tildeler ordet price værdien af feltet SpotPriceDKK og deler med 1000 for at gå fra GW til KW.
max = math.max(max, price)
min = math.min(min, price)
sum = sum + price
grp.checkupdate('13/4/' ..i, price)
item.hour = tonumber(item.HourDK:match('T(%d+)'))
--Gemmer prisen fra igår i datalager under Spotpris1...23
if tidtimer == 00 and tidminutter == 00 then
storage.set('Spotpris' ..i, price)
end
end
avg = sum / #data
grp.checkupdate('13/4/25', avg)
grp.checkupdate('13/4/26', max)
grp.checkupdate('13/4/27', min)
table.sort(data, function(a, b)
return a.SpotPriceDKK < b.SpotPriceDKK
end)
list = {}
for i = 1, 4 do
item = data[ i ]
list[ #list + 1 ] = string.format('%d = %.2f', item.hour, item.SpotPriceDKK)
end
hours = table.concat(list, '; ')
grp.checkupdate('13/4/28', hours)
-- viser angivede enheder i loggen
--log(avg, max, min, tidtimer, tidminutter, hours)
The code returns a 250 byte string at groupe address 13/4/28. Now i would like the script, or potentioaly a new script, to turn on a boolean group address at the specific timestamps from the groupe 13/4/28.
Eg. the script returns: string: 1 = 1523.93; 0 = 1559.11; 3 = 1559.93; 4 = 1561.04 wich means that i the hour between 01:00-01:59 is the cheapest hour, and so on.
When the electricity is cheap i want the script to set an output
Hope everything makes sense
RE: Timedependent output from string - admin - 29.11.2022
Modify the script that fetches the prices so it saves the list of cheapest hours into storage:
Code: list = {}
price_hours = {}
for i = 1, 4 do
item = data[ i ]
list[ i ] = string.format('%d = %.2f', item.hour, item.SpotPriceDKK)
price_hours[ i ] = item.hour
end
storage.set('price_hours', price_hours)
hours = table.concat(list, '; ')
grp.checkupdate('13/4/28', hours)
Then create a scheduled script that runs every hour at 0 minutes:
Code: currhour = os.date('*t').hour
hours = storage.get('price_hours', {})
found = false
for _, hour in ipairs(hours) do
if currhour == hour then
found = true
end
end
grp.checkwrite('1/1/1', found)
|