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 that you accept these cookies being set.

Extracting Values from JSON data - Amber Electric API
#1
Hi

I'd like to pull the current price from my electricity supplier (Amber Electric). They offer variable pricing (updaters every 30 mins) so there's an opportunity to automate the power down of energy hungry devices when the prices increase.

I've assembled most of the script but am having some trouble extracting one of the last required value from the JSON.

Attached is the beginning of the script and an example of the data that is available via the API.

Can anyone provide some example code to retrieve the wholesaleKWHPrice for the current period (the current period is the last period in the data set with type ACTUAL).  This will involve selecting the value based on another value rather than a specific data point in the JSON.

Many thanks in advance

Kind Regards
James








 
Code:
local https = require('ssl.https')
  local ltn12 = require('ltn12')
  local json = require('json')
  local cBody = '{"postcode": "3000"}'
  local cReq = {}
 
  result1 = https.request({
    url = 'https://api.amberelectric.com.au/prices/listprices',
    method = 'POST',
    headers = {
      ['content-length'] = #cBody,
      ['content-type'] = 'application/json'
    },
    source = ltn12.source.string(cBody),
    sink = ltn12.sink.table(cReq)
  })
 
  if (cReq) then
    mydata = "[" .. table.concat(cReq) .. "]"

--log(mydata)
   
    totalfixedKWHPrice = json.decode(mydata)[1].data.staticPrices.E1.totalfixedKWHPrice
    log(totalfixedKWHPrice)
 
    lossFactor = json.decode(mydata)[1].data.staticPrices.E1.lossFactor
    log(lossFactor)
 
    -- Retrieve the current price - the latest period with type ACTUAL
    --wholesaleKWHPrice = json.decode(mydata)[1].data.variablePricesAndRenewables.[period].wholesaleKWHPrice
    --log(wholesaleKWHPrice)
 
    currentPrice = totalfixedKWHPrice + lossFactor * wholesaleKWHPrice
    log(currentPrice)
   

  end





Here is an example of the data returned via the API

Code:
{
  "serviceResponseType":1,
  "data":{
      "currentNEMtime":"2020-12-26T20:53:14",
      "postcode":"3000",
      "networkProvider":"CitiPower",
      "staticPrices":{
        "E1":{
            "dataAvailable":true,
            "networkDailyPrice":"28.63043692",
            "basicMeterDailyPrice":"0",
            "additionalSmartMeterDailyPrice":"22",
            "amberDailyPrice":"32.87671233",
            "totalDailyPrice":"83.50714925",
            "networkKWHPrice":"7.766",
            "marketKWHPrice":"1.718",
            "greenKWHPrice":"4.2471",
            "carbonNeutralKWHPrice":"0.11",
            "lossFactor":"1.04063232",
            "offsetKWHPrice":"0.11",
            "totalfixedKWHPrice":"9.59400",
            "totalBlackPeakFixedKWHPrice":"NaN",
            "totalBlackShoulderFixedKWHPrice":"NaN",
            "totalBlackOffpeakFixedKWHPrice":"NaN"
        },
        "E2":{
            "dataAvailable":true,
            "networkDailyPrice":"0",
            "basicMeterDailyPrice":"0",
            "additionalSmartMeterDailyPrice":"0",
            "amberDailyPrice":"0",
            "totalDailyPrice":"0",
            "networkKWHPrice":"2.453",
            "marketKWHPrice":"1.718",
            "greenKWHPrice":"4.2471",
            "carbonNeutralKWHPrice":"0.11",
            "lossFactor":"1.04063232",
            "offsetKWHPrice":"0.11",
            "totalfixedKWHPrice":"4.28100",
            "totalBlackPeakFixedKWHPrice":"NaN",
            "totalBlackShoulderFixedKWHPrice":"NaN",
            "totalBlackOffpeakFixedKWHPrice":"NaN"
        },
        "B1":{
            "dataAvailable":true,
            "networkDailyPrice":"0",
            "basicMeterDailyPrice":"0",
            "additionalSmartMeterDailyPrice":"0",
            "amberDailyPrice":"0",
            "totalDailyPrice":"0",
            "networkKWHPrice":"0",
            "marketKWHPrice":"0",
            "greenKWHPrice":"0",
            "carbonNeutralKWHPrice":"0",
            "lossFactor":"-1.04063232",
            "offsetKWHPrice":"0",
            "totalfixedKWHPrice":"0.00000",
            "totalBlackPeakFixedKWHPrice":"NaN",
            "totalBlackShoulderFixedKWHPrice":"NaN",
            "totalBlackOffpeakFixedKWHPrice":"NaN"
        }
      },
      "variablePricesAndRenewables":[
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"678.32",
            "operationalDemand":"3975.27",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.8269000000000006",
            "region":"VIC1",
            "period":"2020-12-25T21:00:00",
            "renewablesPercentage":"0.17063495058197306",
            "periodSource":"30MIN",
            "percentileRank":"0.5585585585585585"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"769.17",
            "operationalDemand":"3932.25",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9864000000000006",
            "region":"VIC1",
            "period":"2020-12-25T21:30:00",
            "renewablesPercentage":"0.19560556933053594",
            "periodSource":"30MIN",
            "percentileRank":"0.7117117117117117"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"775.71",
            "operationalDemand":"3881.86",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.7741000000000007",
            "region":"VIC1",
            "period":"2020-12-25T22:00:00",
            "renewablesPercentage":"0.19982946319547845",
            "periodSource":"30MIN",
            "percentileRank":"0.5495495495495496"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"755.05",
            "operationalDemand":"3847.55",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.0084",
            "region":"VIC1",
            "period":"2020-12-25T22:30:00",
            "renewablesPercentage":"0.196241764239581",
            "periodSource":"30MIN",
            "percentileRank":"0.7207207207207207"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"713.71",
            "operationalDemand":"3812.18",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9842",
            "region":"VIC1",
            "period":"2020-12-25T23:00:00",
            "renewablesPercentage":"0.18721833701451665",
            "periodSource":"30MIN",
            "percentileRank":"0.7027027027027027"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"698.63",
            "operationalDemand":"4021.32",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.6563",
            "region":"VIC1",
            "period":"2020-12-25T23:30:00",
            "renewablesPercentage":"0.17373151104612414",
            "periodSource":"30MIN",
            "percentileRank":"0.8288288288288288"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"646.3",
            "operationalDemand":"3993.64",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.588100000000001",
            "region":"VIC1",
            "period":"2020-12-26T00:00:00",
            "renewablesPercentage":"0.16183231337827145",
            "periodSource":"30MIN",
            "percentileRank":"0.8198198198198198"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"616.52",
            "operationalDemand":"3868.19",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.5232",
            "region":"VIC1",
            "period":"2020-12-26T00:30:00",
            "renewablesPercentage":"0.15938203655973465",
            "periodSource":"30MIN",
            "percentileRank":"0.8108108108108109"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"633.5",
            "operationalDemand":"3773.97",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.258100000000001",
            "region":"VIC1",
            "period":"2020-12-26T01:00:00",
            "renewablesPercentage":"0.1678603698492569",
            "periodSource":"30MIN",
            "percentileRank":"0.7837837837837838"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"593.02",
            "operationalDemand":"3718.44",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.0623000000000005",
            "region":"VIC1",
            "period":"2020-12-26T01:30:00",
            "renewablesPercentage":"0.15948085756392466",
            "periodSource":"30MIN",
            "percentileRank":"0.7387387387387387"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"511.02",
            "operationalDemand":"3590.14",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9413000000000005",
            "region":"VIC1",
            "period":"2020-12-26T02:00:00",
            "renewablesPercentage":"0.14233985304194266",
            "periodSource":"30MIN",
            "percentileRank":"0.6396396396396397"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"475.24",
            "operationalDemand":"3538.07",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9644000000000004",
            "region":"VIC1",
            "period":"2020-12-26T02:30:00",
            "renewablesPercentage":"0.13432181952307332",
            "periodSource":"30MIN",
            "percentileRank":"0.6756756756756757"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"454.21",
            "operationalDemand":"3482.85",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9435000000000002",
            "region":"VIC1",
            "period":"2020-12-26T03:00:00",
            "renewablesPercentage":"0.13041331093788133",
            "periodSource":"30MIN",
            "percentileRank":"0.6486486486486487"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"409.91",
            "operationalDemand":"3452.72",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9358000000000004",
            "region":"VIC1",
            "period":"2020-12-26T03:30:00",
            "renewablesPercentage":"0.11872089251372832",
            "periodSource":"30MIN",
            "percentileRank":"0.6306306306306306"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"360.19",
            "operationalDemand":"3445.2",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9083000000000006",
            "region":"VIC1",
            "period":"2020-12-26T04:00:00",
            "renewablesPercentage":"0.10454835713456404",
            "periodSource":"30MIN",
            "percentileRank":"0.6216216216216216"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"332.96",
            "operationalDemand":"3514.7",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.0579",
            "region":"VIC1",
            "period":"2020-12-26T04:30:00",
            "renewablesPercentage":"0.09473354767120949",
            "periodSource":"30MIN",
            "percentileRank":"0.7297297297297297"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"310.68",
            "operationalDemand":"3529.65",
            "rooftopSolar":"0.065",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.1811",
            "region":"VIC1",
            "period":"2020-12-26T05:00:00",
            "renewablesPercentage":"0.08803685283372736",
            "periodSource":"30MIN",
            "percentileRank":"0.7747747747747747"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"326.24",
            "operationalDemand":"3589.17",
            "rooftopSolar":"18.005",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.2658000000000005",
            "region":"VIC1",
            "period":"2020-12-26T05:30:00",
            "renewablesPercentage":"0.09543340702904628",
            "periodSource":"30MIN",
            "percentileRank":"0.7927927927927928"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"373.18",
            "operationalDemand":"3631.27",
            "rooftopSolar":"85.093",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.0755",
            "region":"VIC1",
            "period":"2020-12-26T06:00:00",
            "renewablesPercentage":"0.1233122275730331",
            "periodSource":"30MIN",
            "percentileRank":"0.7477477477477478"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"453.27",
            "operationalDemand":"3655.17",
            "rooftopSolar":"211.13",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.5486000000000004",
            "region":"VIC1",
            "period":"2020-12-26T06:30:00",
            "renewablesPercentage":"0.17184388174740706",
            "periodSource":"30MIN",
            "percentileRank":"0.5315315315315315"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"549.53",
            "operationalDemand":"3597.66",
            "rooftopSolar":"397.335",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.0338000000000003",
            "region":"VIC1",
            "period":"2020-12-26T07:00:00",
            "renewablesPercentage":"0.23701281228136706",
            "periodSource":"30MIN",
            "percentileRank":"0.5135135135135135"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"575.41",
            "operationalDemand":"3465.6",
            "rooftopSolar":"615.659",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"1.9162000000000003",
            "region":"VIC1",
            "period":"2020-12-26T07:30:00",
            "renewablesPercentage":"0.291838621366593",
            "periodSource":"30MIN",
            "percentileRank":"0.44144144144144143"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"562.95",
            "operationalDemand":"3403.54",
            "rooftopSolar":"844.22",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"1.6588",
            "region":"VIC1",
            "period":"2020-12-26T08:00:00",
            "renewablesPercentage":"0.3312734241105901",
            "periodSource":"30MIN",
            "percentileRank":"0.43243243243243246"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"572.87",
            "operationalDemand":"3328.05",
            "rooftopSolar":"1062.277",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"2.5652",
            "region":"VIC1",
            "period":"2020-12-26T08:30:00",
            "renewablesPercentage":"0.3724431004797592",
            "periodSource":"30MIN",
            "percentileRank":"0.4594594594594595"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"571.54",
            "operationalDemand":"3220.45",
            "rooftopSolar":"1256.372",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"1.4069",
            "region":"VIC1",
            "period":"2020-12-26T09:00:00",
            "renewablesPercentage":"0.40830571329393933",
            "periodSource":"30MIN",
            "percentileRank":"0.3783783783783784"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"612.53",
            "operationalDemand":"3118.4",
            "rooftopSolar":"1420.145",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.9867000000000001",
            "region":"VIC1",
            "period":"2020-12-26T09:30:00",
            "renewablesPercentage":"0.4478693061322516",
            "periodSource":"30MIN",
            "percentileRank":"0.24324324324324326"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"661.28",
            "operationalDemand":"2983.81",
            "rooftopSolar":"1565.511",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.9845",
            "region":"VIC1",
            "period":"2020-12-26T10:00:00",
            "renewablesPercentage":"0.489477660512415",
            "periodSource":"30MIN",
            "percentileRank":"0.1891891891891892"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"709.44",
            "operationalDemand":"2945.39",
            "rooftopSolar":"1645.939",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.9845",
            "region":"VIC1",
            "period":"2020-12-26T10:30:00",
            "renewablesPercentage":"0.513005929220058",
            "periodSource":"30MIN",
            "percentileRank":"0.1981981981981982"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"731.66",
            "operationalDemand":"2914.81",
            "rooftopSolar":"1700.024",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.077",
            "region":"VIC1",
            "period":"2020-12-26T11:00:00",
            "renewablesPercentage":"0.5269277291447536",
            "periodSource":"30MIN",
            "percentileRank":"0.15315315315315314"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"747.14",
            "operationalDemand":"2937.72",
            "rooftopSolar":"1740.195",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.24200000000000005",
            "region":"VIC1",
            "period":"2020-12-26T11:30:00",
            "renewablesPercentage":"0.5317187251157834",
            "periodSource":"30MIN",
            "percentileRank":"0.17117117117117117"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"792.24",
            "operationalDemand":"2882.21",
            "rooftopSolar":"1760.767",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.056100000000000004",
            "region":"VIC1",
            "period":"2020-12-26T12:00:00",
            "renewablesPercentage":"0.5498642358124971",
            "periodSource":"30MIN",
            "percentileRank":"0.14414414414414414"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"818.8",
            "operationalDemand":"2881.59",
            "rooftopSolar":"1780.291",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.1639",
            "region":"VIC1",
            "period":"2020-12-26T12:30:00",
            "renewablesPercentage":"0.5575198079916668",
            "periodSource":"30MIN",
            "percentileRank":"0.16216216216216217"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"807.65",
            "operationalDemand":"2883.57",
            "rooftopSolar":"1781.577",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.5676000000000001",
            "region":"VIC1",
            "period":"2020-12-26T13:00:00",
            "renewablesPercentage":"0.5550150938437738",
            "periodSource":"30MIN",
            "percentileRank":"0.18018018018018017"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"829.58",
            "operationalDemand":"2909.54",
            "rooftopSolar":"1762.097",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.9845",
            "region":"VIC1",
            "period":"2020-12-26T13:30:00",
            "renewablesPercentage":"0.554768489075671",
            "periodSource":"30MIN",
            "percentileRank":"0.2072072072072072"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"809.83",
            "operationalDemand":"3001.71",
            "rooftopSolar":"1712.111",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"1.0879",
            "region":"VIC1",
            "period":"2020-12-26T14:00:00",
            "renewablesPercentage":"0.535009920826438",
            "periodSource":"30MIN",
            "percentileRank":"0.35135135135135137"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"808.58",
            "operationalDemand":"3121.4",
            "rooftopSolar":"1623.585",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"0.9867000000000001",
            "region":"VIC1",
            "period":"2020-12-26T14:30:00",
            "renewablesPercentage":"0.512575909091388",
            "periodSource":"30MIN",
            "percentileRank":"0.25225225225225223"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"795.38",
            "operationalDemand":"3296.11",
            "rooftopSolar":"1518.091",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"2.6235000000000004",
            "region":"VIC1",
            "period":"2020-12-26T15:00:00",
            "renewablesPercentage":"0.48055139367882643",
            "periodSource":"30MIN",
            "percentileRank":"0.46846846846846846"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"714.54",
            "operationalDemand":"3476.09",
            "rooftopSolar":"1412.3",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.6938000000000004",
            "region":"VIC1",
            "period":"2020-12-26T15:30:00",
            "renewablesPercentage":"0.43507985246676306",
            "periodSource":"30MIN",
            "percentileRank":"0.5405405405405406"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"626.04",
            "operationalDemand":"3741.15",
            "rooftopSolar":"1257.755",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"3.9567",
            "region":"VIC1",
            "period":"2020-12-26T16:00:00",
            "renewablesPercentage":"0.3768415282946965",
            "periodSource":"30MIN",
            "percentileRank":"0.6666666666666666"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"598.78",
            "operationalDemand":"3974.83",
            "rooftopSolar":"1099.333",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.705800000000001",
            "region":"VIC1",
            "period":"2020-12-26T16:30:00",
            "renewablesPercentage":"0.3346587407617769",
            "periodSource":"30MIN",
            "percentileRank":"0.8468468468468469"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"524.44",
            "operationalDemand":"4247.24",
            "rooftopSolar":"904.874",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"4.7585999999999995",
            "region":"VIC1",
            "period":"2020-12-26T17:00:00",
            "renewablesPercentage":"0.27742282100124344",
            "periodSource":"30MIN",
            "percentileRank":"0.8558558558558559"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"480.06",
            "operationalDemand":"4549.44",
            "rooftopSolar":"685.632",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"5.2173",
            "region":"VIC1",
            "period":"2020-12-26T17:30:00",
            "renewablesPercentage":"0.22266971686349302",
            "periodSource":"30MIN",
            "percentileRank":"0.9099099099099099"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"420.61",
            "operationalDemand":"4782.4",
            "rooftopSolar":"469.151",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"5.8971",
            "region":"VIC1",
            "period":"2020-12-26T18:00:00",
            "renewablesPercentage":"0.1694282317738131",
            "periodSource":"30MIN",
            "percentileRank":"0.963963963963964"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"315.05",
            "operationalDemand":"4946.62",
            "rooftopSolar":"267.46",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"5.61",
            "region":"VIC1",
            "period":"2020-12-26T18:30:00",
            "renewablesPercentage":"0.11171865410580582",
            "periodSource":"30MIN",
            "percentileRank":"0.9459459459459459"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"221.97",
            "operationalDemand":"5020.6",
            "rooftopSolar":"125.135",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"6.171000000000001",
            "region":"VIC1",
            "period":"2020-12-26T19:00:00",
            "renewablesPercentage":"0.06745489225543096",
            "periodSource":"30MIN",
            "percentileRank":"0.9819819819819819"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"115.4",
            "operationalDemand":"4993.48",
            "rooftopSolar":"34.049",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"6.6275",
            "region":"VIC1",
            "period":"2020-12-26T19:30:00",
            "renewablesPercentage":"0.029726133852236363",
            "periodSource":"30MIN",
            "percentileRank":"0.990990990990991"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"106.79",
            "operationalDemand":"4933.87",
            "rooftopSolar":"1.424",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"6.036800000000001",
            "region":"VIC1",
            "period":"2020-12-26T20:00:00",
            "renewablesPercentage":"0.021926555945805865",
            "periodSource":"30MIN",
            "percentileRank":"0.972972972972973"
        },
        {
            "periodType":"ACTUAL",
            "semiScheduledGeneration":"123.89",
            "operationalDemand":"4942.17",
            "rooftopSolar":"0",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"5.7255",
            "region":"VIC1",
            "period":"2020-12-26T20:30:00",
            "renewablesPercentage":"0.025067935744824642",
            "periodSource":"30MIN",
            "percentileRank":"0.954954954954955"
        },
        {
            "period":"2020-12-26T21:00:00",
            "periodType":"ACTUAL",
            "periodSource":"5MIN",
            "latestPeriod":"2020-12-26T20:55:00",
            "region":"VIC1",
            "rooftopSolar":"0",
            "usage":"28947.40800",
            "operationalDemand":"28947.40800",
            "wholesaleKWHPrice":"5.55772",
            "renewablesPercentage":"0.03197",
            "percentileRank":"0.9279279279279279"
        },
        {
            "periodType":"FORECAST",
            "semiScheduledGeneration":"137.817",
            "operationalDemand":"4721.4",
            "rooftopSolar":"0",
            "forecastedAt":"2020-12-26T20:30:00",
            "forecastedAt+period":"2020-12-26T20:30:00+2020-12-26T21:30:00",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"5.534966800000001",
            "period":"2020-12-26T21:30:00",
            "region":"VIC1",
            "wholesaleKWHPriceRange":{
              "max":"50.04597",
              "min":"4.08006"
            },
            "renewablesPercentage":"0.029189858940144878",
            "periodSource":"30MIN",
            "percentileRank":"0.918918918918919"
        },
        {
            "periodType":"FORECAST",
            "semiScheduledGeneration":"154.562",
            "operationalDemand":"4549.38",
            "rooftopSolar":"0",
            "forecastedAt":"2020-12-26T20:30:00",
            "forecastedAt+period":"2020-12-26T20:30:00+2020-12-26T22:00:00",
            "createdAt":"2020-12-26T20:52:41",
            "wholesaleKWHPrice":"5.5633369",
            "period":"2020-12-26T22:00:00",
            "region":"VIC1",
            "wholesaleKWHPriceRange":{
              "max":"36.71082",
              "min":"4.21840"
            },
            "renewablesPercentage":"0.0339742997946973",
            "periodSource":"30MIN",
            "percentileRank":"0.9369369369369369"
        },
       
Reply
#2
Hi,

Try this:
Code:
local https = require('ssl.https')
local ltn12 = require('ltn12')
local json = require('json')
local cBody = '{"postcode": "3000"}'
local cReq = {}

result1 = https.request({
  url = 'https://api.amberelectric.com.au/prices/listprices',
  method = 'POST',
  headers = {
    ['content-length'] = #cBody,
    ['content-type'] = 'application/json'
  },
  source = ltn12.source.string(cBody),
  sink = ltn12.sink.table(cReq)
})

if (cReq) then
  mydata =  json.pdecode(table.concat(cReq))
  log(mydata)

  totalfixedKWHPrice = mydata.data.staticPrices.E1.totalfixedKWHPrice
  log(totalfixedKWHPrice)

  lossFactor = mydata.data.staticPrices.E1.lossFactor
  log(lossFactor)

  -- Retrieve the current price - the latest period with type ACTUAL
  wholesaleKWHPrice = mydata.data.variablePricesAndRenewables[#mydata.data.variablePricesAndRenewables].wholesaleKWHPrice
  log(wholesaleKWHPrice)

  currentPrice = totalfixedKWHPrice + lossFactor * wholesaleKWHPrice
  log(currentPrice)

end
#mydata.data.variablePricesAndRenewables gives you the length of the subset variablePricesAndRenewables and you pick up the latest field of the sub set variablePricesAndRenewables
Reply
#3
Hi Erwin,

The last value of the variablePricesAndRenewables subset isn't always the value we require - we need the period for the half hour increment which is the last one tagged as periodType":"ACTUAL"

.. this will change depending on the time of day.  The variablePricesAndRenewables subset contains half hour increments for the day.  The point where the tags change from ACTUAL to FORECAST changes based on the time of day that we pull the data.

Is there a way to scan the JSON data values within the variablePricesAndRenewables subset to find the last one tagged with periodType":"ACTUAL" rather than the last one in the subset?

Kind Regards
James

Hi,

Further to my last message I've added the following code to return the last period of type ACTUAL


Code:
for i,k in ipairs(mydata.data.variablePricesAndRenewables) do
      if (k.periodType == 'ACTUAL') then
        wholesaleKWHPrice = k.wholesaleKWHPrice
      end
    end

Thanks for your help
Reply


Forum Jump: