$$$ Power pricing $$$

Discussion in 'C-Bus Automation Controllers' started by philthedill, Jan 23, 2021.

  1. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    Team, there is an electricity retailer (amberelectric.com.au) that sells power at ~ wholesale + distribution + $15 fixed/month. This is appealing but only as long as you can load shed when required... (eg: pool pump, air con, clothes dryer, hws). Ideally, this will be achieved by receiving a price signal into SHAC and then programming accordingly. Amberelectric have a great help desk and I have received the attached information - a .pdf but am struggling with the next step. I am hoping it distils down to extracting the latest forecast variable rate "wholesalekWHPrice" - which I have highlighted in yellow and using this to update a SHAC user variable. I am thinking that this can be done using a scheduled script operating every 30 minutes. Can anyone help? with thanks....

    PS - I could not upload the .json but this link should get to it.. https://mail.google.com/mail/u/0?ui...977457&th=1772885fab80de71&view=att&disp=safe
     

    Attached Files:

    philthedill, Jan 23, 2021
    #1
    1. Advertisements

  2. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    This looks pretty straightforward,
    I was unable to get the .Jason as per link, but

    Have you tried a sending a http post command from postman or other platform, as per attached 210122 Amberelectric email.PDF, I tried it, it returned a error 502 internal server error... so not sure if I am missing something?

    That would be the first step, once the http post command works, the rest is easy...
     
    Pie Boy, Jan 25, 2021
    #2
    1. Advertisements

  3. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    great work Pie Boy. I got it to work via Postman as you can see here. You might have missed putting the postcode into the body of the request. The piece of data I now need to extract is the numerical value at line 709 which is - "1.0109" in this example. I need to write this to a CBusUserParameter every 30 minutes and then do my load management from there. thanks again
    upload_2021-1-26_9-5-14.png
     
    philthedill, Jan 25, 2021
    #3
  4. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    Cool, yeah i had double quotes on my body message, removed them and then all good, rookie mistake....
    Try this, this works for me to receive the complete post response body message and decode the json
    you can then access the bits you need within the message table like this
    log(message.data.staticPrices)
    log(message.data.variablePricesAndRenewables)
    are you after specifically the wholesaleKWHPrice value? we will probably need to use some other parameters in the table to find this value, probably a small loop that looks for something inside all the table index then returns a match on the index of the piece we need to look at for the data

    eg if we can nail down something specific in the table index that only exists in this piece we can work with that


    Code:
    function post_request(url, payload)
     require('json')
      ltn12 = require('ltn12')
      http = require ("ssl.https")
    
      response_body = {}
    
      res, err = http.request{
        url = url,
        method = 'POST',
        headers =
        {
          ["Accept"] = "*/*",
          ["Content-Type"] = "application/json",
          ["Content-Length"] = #payload
        },
          source = ltn12.source.string(payload),
        sink = ltn12.sink.table(response_body)
     
      }
    
      if response_body then
       resp = table.concat(response_body)
      resp = json.decode(resp)
        return resp, res, err
      else
        return nil, err
      end
    end
    
    
    
    message, res, err = post_request('https://api.amberelectric.com.au/prices/listprices', json.encode({postcode = "3232"}))
    
    log(res, err)
    log(message)
    
    --log(message.data.staticPrices)
    --log(message.data.variablePricesAndRenewables)
    
    
     
    Last edited: Jan 25, 2021
    Pie Boy, Jan 25, 2021
    #4
    philthedill likes this.
  5. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    great - that works fine. The piece of data I need is in line 709. where to now?
     
    philthedill, Jan 25, 2021
    #5
  6. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    what exactly are you trying to achieve? will the below achieve this?

    here is something to get you started not sure its the most efficient code but without putting to much thought into it here goes, add in below the other code etc
    Code:
    function find_period(msg, period_tofind)  --"2021-01-27T13:30:00"
     
      for index = 1, #msg.data.variablePricesAndRenewables do
      -- find period and return
        if msg.data.variablePricesAndRenewables[index].period == period_tofind then
        log ('Found period', msg.data.variablePricesAndRenewables[index].period, 'at index', index )
          return msg.data.variablePricesAndRenewables[index].period, index
      end
    end
    
    end
    
    local period, index = find_period(message, "2021-01-27T13:30:00")
    
    --now we know the index for the time period we are after, which is individually different from 
    all other entries we can get all the data associated with this index like this
     -- message.data.variablePricesAndRenewables[index].period
    
    -- this is as per the amber doc to calculate the formula
    log(message.data.staticPrices.E1.totalfixedKWHPrice + message.data.staticPrices.E1.lossFactor *
      message.data.variablePricesAndRenewables[index].wholesaleKWHPrice)
    
    
     
    Pie Boy, Jan 26, 2021
    #6
    philthedill likes this.
  7. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    thank you again. I played with a variant of what you propose. I found that if I enter the number [50] instead of index, I consistently get the record I am looking for. Here is what I used to get the import price after getting the .json

    message, res, err = post_request('https://api.amberelectric.com.au/prices/listprices', json.encode({postcode = "3232"}))

    -- Extract Usage price and post to user variable Elec_usage_price (250/23)

    staticiprice = (message.data.staticPrices.E1["totalfixedKWHPrice"])
    variprice = (message.data.staticPrices.E1["lossFactor"])*(message.data.variablePricesAndRenewables[50]["wholesaleKWHPrice"])
    usageprice = staticiprice + variprice

    SetUserParam(0, 23, usageprice)

    so I am in business - your help appreciated.....
     
    philthedill, Jan 27, 2021
    #7
  8. philthedill

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    163
    Likes Received:
    25
    Location:
    New Zealand
    Good you got it working for you,

    Just so you know, the data you are after at index 50 is Unlikely to 100% always be at that index, it is a possibility under some conditions as amber update its data, its index may move around, as far as I can tell in that table message.data.variablePricesAndRenewables the only thing that is individual in each index element is the time stamp, so to be 100% sure if would use the time stamp to find the index, which is what the code above did,

    but hey, whatever works is all good.
     
    Pie Boy, Jan 27, 2021
    #8
  9. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    thanks for the warning - I'll do some more due diligence. I've noticed the row numbers move around a bit as the 30 minute refresh approaches
     
    philthedill, Jan 27, 2021
    #9
  10. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    Amberelectric has now released a new API that uses a "GET request" and is way more accurate than the earlier version (as it is now account specific). I've got it working with postman and on the SHAC with the received data looking like this but I cannot get it to isolate the (2) particular values that I need. the first block for "feed-in" tariff and the second block is "general" tariff related and I need the value of "perKwh" in both cases. Any suggestions?

    Extract buy/sell prices using new Amber API 27.06.2021 11:08:19
    * table:
    [1]
    * table:
    [startTime]
    * string: 2021-06-27T01:00:01Z
    [renewables]
    * number: 16.907
    [perKwh]
    * number: -12.69729
    [endTime]
    * string: 2021-06-27T01:30:00Z
    [spikeStatus]
    * string: none
    [estimate]
    * bool: true
    [spotPerKwh]
    * number: 12.14517
    [channelType]
    * string: feedIn
    [type]
    * string: CurrentInterval
    [date]
    * string: 2021-06-27
    [nemTime]
    * string: 2021-06-27T11:30:00+10:00
    [duration]
    * number: 30
    [2]
    * table:
    [startTime]
    * string: 2021-06-27T01:00:01Z
    [renewables]
    * number: 16.907
    [perKwh]
    * number: 20.06872
    [endTime]
    * string: 2021-06-27T01:30:00Z
    [estimate]
    * bool: true
    [spikeStatus]
    * string: none
    [tariffInformation]
    * table:
    [period]
    * string: offPeak
    [spotPerKwh]
    * number: 12.14517
    [channelType]
    * string: general
    [type]
    * string: CurrentInterval
    >
    At this stage, I have tried
    <
    eprice0 = message.table.[1]["perKwh"] or
    eprice0 = message.data.[1].[perKwh]
    >
    and lots of other permutations and combinations but without success - I am just guessing and need some knowledgable input please.
    cheers
     
    philthedill, Jun 28, 2021
    #10
  11. philthedill

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    104
    Likes Received:
    0
    Location:
    Melbourne
    solved it myself (for once)

    needed some brackets but here's the answer for others

    eprice0 = (message[1]["perKwh"])
     
    philthedill, Jun 28, 2021
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.