Announcement

Collapse
No announcement yet.

Jon00 Datascraper - Stuck and in need for help with pattern

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Jon00 Datascraper - Stuck and in need for help with pattern

    Hi,
    So I got the DataScraper script to work, and it is a fantastic tool!
    However, it is far more advanced than my current knowledge, and I am stuck. So a little bit of help would be highly appreciated.

    What I have done so far is to fetch the current energy prices from an API. The format is JSON, with 24 sections which represents the energy price for every hour of the day. I managed to create a pattern so that GrabData is a list of all the prices for every hour of a day, like this:

    [Grab1Data]
    0=1.1156
    1=1.08999
    2=1.06685
    3=1.06029

    And so on, all the way to 23=.

    From this, I create three devices: One that show the day average price, one that shows a calculated day low price point, and one that shows a calculated day high price point, relative to the average. (please se my DataScraper.ini below). The goal is to create events that can turn stuff on and off depening on if the energy price is lower or higher than the average.
    I only lack one device: the one that holds the current hour energy price. It obviously have to be updated every hour.

    The data is already grabbed, but I have no clue how to extract it. I can't grab the hourly price from the API, just a full day (24 hours with corresponding price points). I have fiddled around with the [hour] replacement value as a pattern to use the API/JSON as a source, but it doesn't seem to work. I guess I have to use a Regex on the grabbed data in the DataScraperData.ini file(?), but I have absolutely no knowledge of Regex. So the learning curve is steep, and I have no clue how to create a regex that extract the price for the current hour. Or could I use a pattern directly on the JSON? Any suggestions?

    Thanks!​


    Jon00DataScraper.ini:

    [Settings]
    DataLogging=1
    ExpressionLogging=1
    ErrorLogging=1
    IEClearDelay=3
    [Grab1]
    Path=https://www.hvakosterstrommen.no/api/v1/prices/[year]/[month]-[day]_NO2.json
    PostData=
    TextFile=1
    Encoding=
    Username=
    Password=
    Options=
    UserAgent=
    Devicemode=4
    StripHTML=1
    UseIE=0
    SSLMode=*3072
    GroupDevices=0
    Pattern1=JSON> 0.NOK_per_kWh || 1.NOK_per_kWh || 2.NOK_per_kWh ||3.NOK_per_kWh ||4.NOK_per_kWh ||5.NOK_per_kWh || 6.NOK_per_kWh || 7.NOK_per_kWh || 8.NOK_per_kWh ||9.NOK_per_kWh ||10.NOK_per_kWh ||11.NOK_per_kWh ||12.NOK_per_kWh ||13.NOK_per_kWh ||14.NOK_per_kWh ||15.NOK_per_kWh ||16.NOK_per_kWh ||17.NOK_per_kWh ||18.NOK_per_kWh || 19.NOK_per_kWh || 20.NOK_per_kWh ||21.NOK_per_kWh ||22.NOK_per_kWh ||23.NOK_per_kWh

    DeviceName1=EnergyPrice - Average
    DeviceText1=Average Energy Price for [day]/[month]/[year] is [0+{1},+{2},+{3},+{4},+{5},+{6},+{7},+{8},+{9},+{10},+{11},+{ 12},+{13},+{14},+{15},+{16},+{17},+{18},+{19},+{20},+{21},+{ 22},+{23},/24]
    DeviceValue1=[0+{1},+{2},+{3},+{4},+{5},+{6},+{7},+{8},+{9},+{10},+{11},+{ 12},+{13},+{14},+{15},+{16},+{17},+{18},+{19},+{20},+{21},+{ 22},+{23},/24]
    DeviceImage1=[100]
    Speakbutton1=
    TriggerString1=
    SearchMode1=1
    TriggerEvent1=?

    DeviceName2=EnergyPrice - Low
    DeviceText2=Low Energy Price for [day]/[month]/[year] is [0+{1},+{2},+{3},+{4},+{5},+{6},+{7},+{8},+{9},+{10},+{11},+{ 12},+{13},+{14},+{15},+{16},+{17},+{18},+{19},+{20},+{21},+{ 22},+{23},/24,*0.85]
    DeviceValue2=[0+{1},+{2},+{3},+{4},+{5},+{6},+{7},+{8},+{9},+{10},+{11},+{ 12},+{13},+{14},+{15},+{16},+{17},+{18},+{19},+{20},+{21},+{ 22},+{23},/24,*0.85]
    DeviceImage2=[200]
    Speakbutton2=
    TriggerString2=
    SearchMode2=1
    TriggerEvent2=?

    DeviceName3=EnergyPrice - High
    DeviceText3=High Energy Price for [day]/[month]/[year] is [0+{1},+{2},+{3},+{4},+{5},+{6},+{7},+{8},+{9},+{10},+{11},+{ 12},+{13},+{14},+{15},+{16},+{17},+{18},+{19},+{20},+{21},+{ 22},+{23},/24,*1.15]
    DeviceValue3=[0+{1},+{2},+{3},+{4},+{5},+{6},+{7},+{8},+{9},+{10},+{11},+{ 12},+{13},+{14},+{15},+{16},+{17},+{18},+{19},+{20},+{21},+{ 22},+{23},/24,*1.15]
    DeviceImage3=[100]
    Speakbutton3=
    TriggerString3=
    SearchMode3=1
    TriggerEvent3=?

    DeviceName4=CurrentEnergyPrice
    DeviceText4=Energy Price for [day]/[month]/[year] @ [hour] is ???????????
    DeviceValue4=????????????
    DeviceImage4=[100]
    Speakbutton4=
    TriggerString4=
    SearchMode4=1
    TriggerEvent4=?

    [Info]
    Version=1.0.35

    #2
    Unfortunately, not possible without a helper script.

    Copy the following in a text editor and save it to your scripts directory as CurrentPrice.vb

    Code:
    Function Main(ByVal Parm As String)
    Return hs.GetIniSetting("Grab1Data",System.DateTime.Now.ToString("H"),"","Jon00DataScraperData.ini")
    End Function
    Then use the following for Device4:

    Code:
    DeviceName4=CurrentEnergyPrice
    DeviceText4=Energy Price for [day]/[month]/[year] @ [hour] is [0 func CurrentPrice.vb Main]
    DeviceValue4=[0 func CurrentPrice.vb Main]
    DeviceImage4=[100]
    Speakbutton4=
    TriggerString4=
    SearchMode4=1
    TriggerEvent4=?
    That's it!
    Jon

    Comment


      #3

      Thanks, Jon, that's brilliant!

      Unfortunately, something goes wrong. Seems it tries to do something with the value of hour 0 (1.1156)
      Here's the log:


      1/29/2023 22:51:40
      Script
      Jon00_DScraper
      [Grab1] Task 1 - Scriptfunc Function: 1.1156 > CurrentPrice.vb Main >

      1/29/2023 22:51:40
      HomeSeer
      Error 00
      Running script: Exception has been thrown by the target of an invocation.

      1/29/2023 22:51:40
      Script
      Jon00_DScraper
      [Grab1] Task 0 - Data value to be processed: 1.1156

      1/29/2023 22:51:40
      Script
      Jon00_DScraper
      [Grab1] Task 1 - Scriptfunc Function: 1.1156 > CurrentPrice.vb Main >

      1/29/2023 22:51:40
      HomeSeer
      Error 00
      Running script: Exception has been thrown by the target of an invocation.

      1/29/2023 22:51:40
      Script
      Jon00_DScraper
      [Grab1] Task 0 - Data value to be processed: 1.1156

      Click image for larger version

Name:	bilde.png
Views:	101
Size:	96.5 KB
ID:	1589343

      Comment


        #4
        Please post the script you have saved.
        Jon

        Comment


          #5
          Originally posted by jon00 View Post
          Please post the script you have saved.
          Code:
          Function Main(ByVal Parm As String)
          Return hs.GetIniSetting("Grab1Data",System.DateTime.Now.ToString("H "),"","Jon00DataScraperData.ini")
          End Function​
          Click image for larger version

Name:	bilde.png
Views:	81
Size:	20.4 KB
ID:	1589349

          Comment


            #6
            OK, try this script:

            Code:
            Function Main(ByVal Parm As Object)
            Dim H As Integer = System.DateTime.Now.ToString("HH")
            Return hs.GetIniSetting("Grab1Data",H.ToString,"","Jon00DataScraperData.ini")
            End Function
            Jon

            Comment


              #7
              Originally posted by jon00 View Post
              OK, try this script:

              Code:
              Function Main(ByVal Parm As Object)
              Dim H As Integer = System.DateTime.Now.ToString("HH")
              Return hs.GetIniSetting("Grab1Data",H.ToString,"","Jon00DataScraperData.ini")
              End Function
              Perfect, Sir! Thank you so much!


              Click image for larger version

Name:	image.png
Views:	67
Size:	24.6 KB
ID:	1589418

              Comment

              Working...
              X