Logic Machine Forum
Power price API -> grp. adresses on Wiser - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Power price API -> grp. adresses on Wiser (/showthread.php?tid=4369)



Power price API -> grp. adresses on Wiser - ClausP - 10.11.2022

Hello people! Big Grin
Im all new to all this scripting thing with my Wiser.
I have been looking at the forum, and found an other script that more or less say it does what I want.
Then i've changed some of the parameters and the API.

When I run the script i just get this awnswer in the alert log:

Resident script:12: attempt to index a nil value
stack traceback:

Can anyone help? Tongue 
Code:
require('json')
https = require 'ssl.https'
date = os.date('%Y-%m-%d')



url = 'https://api.energidataservice.dk/dataset/Elspotprices?offset=0&filter=%7B%22PriceArea%22:%22dk1%22%7D&sort=HourDK%20DESC&timezone=dk&limit=24'
status = ssl.https.request(url)

data = json.decode(status)

price1 = data[1].SpotPriceDKK
grp.checkupdate('5/5/1', price1)

price2 = data[2].SpotPriceDKK
grp.checkupdate('5/5/2', price2)

price3 = data[3].SpotPriceDKK
grp.checkupdate('5/5/3', price3)

price4 = data[4].SpotPriceDKK
grp.checkupdate('5/5/4', price4)

price5 = data[5].SpotPriceDKK
grp.checkupdate('5/5/5', price5)

price6 = data[6].SpotPriceDKK
grp.checkupdate('5/5/6', price6)

price7 = data[7].SpotPriceDKK
grp.checkupdate('5/5/7', price7)

price8 = data[8].SpotPriceDKK
grp.checkupdate('5/5/8', price8)

price9 = data[9].SpotPriceDKK
grp.checkupdate('5/5/9', price9)

price10 = data[10].SpotPriceDKK
grp.checkupdate('5/5/10', price10)

price11 = data[11].SpotPriceDKK
grp.checkupdate('5/5/11', price11)

price12 = data[12].SpotPriceDKK
grp.checkupdate('5/5/12', price12)

price13 = data[13].SpotPriceDKK
grp.checkupdate('5/5/13', price13)

price14 = data[14].SpotPriceDKK
grp.checkupdate('5/5/14', price14)

price15 = data[15].SpotPriceDKK
grp.checkupdate('5/5/15', price15)

price16 = data[16].SpotPriceDKK
grp.checkupdate('5/5/16', price16)

price17 = data[17].SpotPriceDKK
grp.checkupdate('5/5/17', price17)

price18 = data[18].SpotPriceDKK
grp.checkupdate('5/5/18', price18)

price19 = data[19].SpotPriceDKK
grp.checkupdate('5/5/19', price19)

price20 = data[20].SpotPriceDKK
grp.checkupdate('5/5/20', price20)

price21 = data[21].SpotPriceDKK
grp.checkupdate('5/5/21', price21)

price22 = data[22].SpotPriceDKK
grp.checkupdate('5/5/22', price22)

price23 = data[23].SpotPriceDKK
grp.checkupdate('5/5/23', price23)

price24 = data[24].SpotPriceDKK
grp.checkupdate('5/5/24', price24)





medelpris = price1+price2+price3+price4+price5+price6+price7+price8+price9+price10+price11+price12+price13+price14+price15+price16+price17+price18+price19+price20+price21+price22+price23+price24
gennemsnit = medelpris / 24
grp.checkupdate('5/5/25', gennemsnit)

maxpris = math.max(price1, price2, price3, price4, price5, price6, price7, price8, price9, price10, price11, price12, price13, price14, price15, price16, price17, price18, price19, price20, price21, price22, price23, price24)
grp.checkupdate('5/5/26', maxpris)

minpris = math.min(price1, price2, price3, price4, price5, price6, price7, price8, price9, price10, price11, price12, price13, price14, price15, price16, price17, price18, price19, price20, price21, price22, price23, price24)
grp.checkupdate('5/5/27', minpris)


log(price1, price2)



RE: Power price API -> grp. adresses on Wiser - admin - 10.11.2022

Try this:
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
min = math.huge
sum = 0

for i, item in ipairs(data) do
  price = item.SpotPriceDKK

  max = math.max(max, price)
  min = math.min(min, price)
  sum = sum + price

  grp.checkupdate('5/5/' .. i, price)
end

avg = sum / #data

grp.checkupdate('5/5/25', avg)
grp.checkupdate('5/5/26', max)
grp.checkupdate('5/5/27', min)



RE: Power price API -> grp. adresses on Wiser - ClausP - 10.11.2022

Thats perfect! Big Grin 
Work like a charm! But again, im totally noob, so what do I do to get all 24 hours of the day? 

I've done this: (changed the "5/5/ .. i," to 5/5/1.
It gives me 836,75000 in a 4 byte float object.


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
min = math.huge
sum = 0

for i, item in ipairs(data) do
  price = item.SpotPriceDKK

  max = math.max(max, price)
  min = math.min(min, price)
  sum = sum + price

  grp.checkupdate('5/5/1', price)
end

avg = sum / #data

grp.checkupdate('5/5/25', avg)
grp.checkupdate('5/5/26', max)
grp.checkupdate('5/5/27', min)



RE: Power price API -> grp. adresses on Wiser - admin - 10.11.2022

Use the script as is without any modifications. It outputs the hourly data to 5/5/1 .. 5/5/24.


RE: Power price API -> grp. adresses on Wiser - ClausP - 10.11.2022

(10.11.2022, 08:49)admin Wrote: Use the script as is without any modifications. It outputs the hourly data to 5/5/1 .. 5/5/24.

Ah okay Smile  Works nicely. The avg, max, min, however doesnt seem to be working?
Would it be possible to isolatae the cheapest 4 hours?

Would you mind spending 5 min explaining what the different lines do?


RE: Power price API -> grp. adresses on Wiser - admin - 10.11.2022

What exactly is not working for min/max/avg? Do you have objects created with correct data type?

This will produce a string with cheapest 4 hours. Set 5/5/28 datatype to 250 byte string.
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
min = math.huge
sum = 0

for i, item in ipairs(data) do
  price = item.SpotPriceDKK

  max = math.max(max, price)
  min = math.min(min, price)
  sum = sum + price

  grp.checkupdate('5/5/' .. i, price)
  item.hour = tonumber(item.HourDK:match('T(%d+)'))
end

avg = sum / #data

grp.checkupdate('5/5/25', avg)
grp.checkupdate('5/5/26', max)
grp.checkupdate('5/5/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('5/5/28', hours)

For general info on Lua read this book: http://www.lua.org/pil/contents.html


RE: Power price API -> grp. adresses on Wiser - ClausP - 10.11.2022

Thanks alot! Big Grin 

I've ended up with this, and it seems to be working. I have tried to make the script move the values over to the storage, once a day, to visualise the right cost at the right time Smile 
Dont mind the danish text at the right side, im just trying to make my way through the code Tongue 
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 == 12 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)