Announcement

Collapse
No announcement yet.

Creating/Updating devices from API call

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

  • Creating/Updating devices from API call

    Hello everyone,

    I have what I hope is a noob problem here. I have a solar system installed on my house and I want to gather generation information that I can display or use as triggers. this information is available through an API and I finally figured out how to access that information. What I need to get done now is take the results and create the Virtual Devices, or at the very least, update Virtual Devices I would create myself (Once I figure out how to do that).

    The URL to the API is something like (userID and Key intentionally left out):

    https://api.enphaseenergy.com/api/v2...&user_id=My_ID

    which would return a JSON that looks something like this:

    {"system_id":SysID,"modules":31,"size_w":7690,"current_po wer ":914,"energy_today":13260,"energy_lifetime":109483300," summ ary_date":"2019-09-04","source":"microinverters","status":"normal","operation al _at":1201362300,"last_report_at":1567624045,"last_interval_ e nd_at":1567623262}

    I tried using the Big5 PI to ingest these values, but it seems to only create a single virtual device with a single string that I don't think I can use. I am comfortable with scripting and such, just not in VB, or with HS for that matter.

    Ideally, the script would go out to the API and gather all available results and create (or update) Virtual Devices. I would like to gather all of the data in a single call to limit the number of hits on the API since I only get 10000 hits per month, leaving my queries to every 5 min during daytime hours.

    Any assistance you could provide in accomplishing what I hope is a simple task would be very much appreciated.

  • #2
    It's relatively easy to parse json info using a vb script and assign to virtual devices. I can post some sample scripts. Are there any login methods to access the data or does it just require the URL with the user ID and key?
    HS 3.0.0.548: 1990 Devices 1172 Events
    Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

    Comment


    • #3
      The ID and key are the only things needed. The link below is to a test solar system for Dev's to test on if you needed one with real data.

      https://api.enphaseenergy.com/api/v2...45774e6a41320a

      Comment


      • #4
        Here's an example of a script I use to retrieve air quality info. Hopefully it will point you in the right direction. If not, let me know.

        Code:
        Imports System.IO
        Imports System.Net
        Imports Newtonsoft.Json
        Imports Newtonsoft.Json.Linq
        
        Sub Main(ByVal Parms As Object)
        
            Dim logName As String = "WAQI"                'set log name for HS log
            Dim Debug As Boolean = False
            Dim WAQIToken As String = "esfddferyfghdsdasfsadfsda"
            Dim LatLong As String = "51.xxx;-114.yyy"
        
            Dim hs_path As String = hs.GetAppPath
            Dim DebugFile As String = hs_path & "\Logs\DebugFileWAQI.json"
        
            Dim mmurl1 As String = "/feed/geo:" & LatLong & "/?token=" & WAQIToken
            Dim Server As String = "api.waqi.info"
            Dim Port As String = "80"
            Dim ReturnURL As String = ""
        
            Dim jsonObj as New JObject
            Dim aqi As String = ""
            Dim time As String = ""
            Dim tz As String = ""
            Dim dominantpol As String = ""
            Dim co As String = ""
            Dim no2 As String = ""
            Dim o3 As String = ""
            Dim pm25 As String = ""
            Dim so2 As String = ""
            Dim t As String = ""
            Dim w As String = ""
            Dim p As String = ""
            Dim wg As String = ""
        
            Dim StartRetrieval As Date = Now
        
            If Debug Then hs.writelog(logName, "Retrieval Started at " & CStr(StartRetrieval))
        
            Try
                ReturnURL = hs.GetURL(Server,mmurl1,False,Port)
                If Debug Then
                    If File.Exists(DebugFile) Then File.Delete(DebugFile)
                    File.WriteAllText(DebugFile, ReturnURL)
                End If
        
                jsonObj = JsonConvert.DeserializeObject(ReturnURL)
        
                If Not IsNothing(jsonObj("data")("aqi")) Then
                    aqi = jsonObj.SelectToken("data").SelectToken("aqi")
                    hs.SetDeviceValuebyRef(7881,CDbl(aqi),True)
                End If
                If Not IsNothing(jsonObj("data")("time")("s")) Then
                    time = jsonObj.SelectToken("data").SelectToken("time").SelectToken("s")
                    hs.SetDeviceString(7880,time,True)
                End If
        
                If Not IsNothing(jsonObj("data")("time")("tz")) Then tz = jsonObj.SelectToken("data").SelectToken("time").SelectToken("tz")
        
                If Not IsNothing(jsonObj("data")("dominentpol")) Then
                    dominantpol = jsonObj.SelectToken("data").SelectToken("dominentpol")
                    Select Case dominantpol
                        Case "pm25"
                            hs.SetDeviceValuebyRef(7882,0,True)
                            hs.SetDeviceString(7882,"PM₂.₅",False)
                        Case "co"
                            hs.SetDeviceValuebyRef(7882,1,True)
                            hs.SetDeviceString(7882,"CO",False)
                        Case "no2"
                            hs.SetDeviceValuebyRef(7882,2,True)
                            hs.SetDeviceString(7882,"NO₂",False)
                        Case "o3"
                            hs.SetDeviceValuebyRef(7882,3,True)
                            hs.SetDeviceString(7882,"O₃",False)
                        Case "so2"
                            hs.SetDeviceValuebyRef(7882,4,True)
                            hs.SetDeviceString(7882,"SO₂",False)
                        Case Else
                            hs.SetDeviceValuebyRef(7882,99,True)
                            hs.SetDeviceString(7882,"Unknown Pollutant",False)
                    End Select        
                End If
        
                If Not IsNothing(jsonObj("data")("iaqi")("co")) Then
                    co = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("co").SelectToken("v")
                    hs.SetDeviceValuebyRef(7883,CDbl(co),True)
                End If
                If Not IsNothing(jsonObj("data")("iaqi")("no2")) Then
                    no2 = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("no2").SelectToken("v")
                    hs.SetDeviceValuebyRef(7884,CDbl(no2),True)
                End If
                If Not IsNothing(jsonObj("data")("iaqi")("o3")) Then
                    o3 = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("o3").SelectToken("v")
                    hs.SetDeviceValuebyRef(7885,CDbl(o3),True)
                End If
                If Not IsNothing(jsonObj("data")("iaqi")("pm25")) Then
                    pm25 = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("pm25").SelectToken("v")
                    hs.SetDeviceValuebyRef(7886,CDbl(pm25),True)
                End If
                If Not IsNothing(jsonObj("data")("iaqi")("so2")) Then
                    so2 = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("so2").SelectToken("v")
                    hs.SetDeviceValuebyRef(7887,CDbl(so2),True)
                End If
        
                If Not IsNothing(jsonObj("data")("iaqi")("t")) Then t = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("t").SelectToken("v")
                If Not IsNothing(jsonObj("data")("iaqi")("w")) Then w = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("w").SelectToken("v")
                If Not IsNothing(jsonObj("data")("iaqi")("p")) Then p = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("p").SelectToken("v")
                If Not IsNothing(jsonObj("data")("iaqi")("wg")) Then wg = jsonObj.SelectToken("data").SelectToken("iaqi").SelectToken("wg").SelectToken("v")
        
                If Debug Then
                    hs.WriteLog(logName, "AQI: " & aqi)
                    hs.WriteLog(logName, "Last Updated: " & time & " " & tz)
                    hs.WriteLog(logName, "Dominant Pollutant: " & dominantpol)
                    hs.WriteLog(logName, "CO: " & co)
                    hs.WriteLog(logName, "NO2: " & no2)
                    hs.WriteLog(logName, "O3: " & o3)
                    hs.WriteLog(logName, "PM2.5: " & pm25)
                    hs.WriteLog(logName, "SO2: " & so2)
                    hs.WriteLog(logName, "Pressure: " & p)
                    hs.WriteLog(logName, "Temperature: " & t)
                    hs.WriteLog(logName, "Wind: " & w)
                    hs.WriteLog(logName, "WG: " & wg)
                End If
        
            Catch ex As Exception
                hs.writelog(logName, "Error:  " & ex.Message.ToString)
            End Try
            hs.writelog(logName, "AQI Retrieval Finished at " & CStr(Now))
        
        End Sub
        HS 3.0.0.548: 1990 Devices 1172 Events
        Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

        Comment


        • #5
          Here's an example of the json that my script parses. It has nesting so more complex than your needs.

          Code:
          {"status":"ok","data":{"aqi":17,"idx":5477,"attributions":[{"url":"http://aep.alberta.ca/air/","name":"Alberta Environment and Sustainable Resource Development - Air Quality monitoring"},{"url":"https://waqi.info/","name":"World Air Quality Index Project"}],"city":{"geo":[51.0471506,-114.0731477],"name":"Calgary Central 2, Alberta, Canada","url":"https://aqicn.org/city/canada/alberta/calgary-central-2"},"dominentpol":"pm25","iaqi":{"co":{"v":5.6},"h":{"v":51.8},"no2":{"v":34.3},"o3":{"v":0.1},"p":{"v":993.3},"pm25":{"v":17},"so2":{"v":1.5},"t":{"v":-0.5},"w":{"v":0.3},"wg":{"v":0.5}},"time":{"s":"2018-12-29 08:00:00","tz":"-06:00","v":1546070400},"debug":{"sync":"2018-12-30T00:27:46+09:00"}}}
          HS 3.0.0.548: 1990 Devices 1172 Events
          Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

          Comment


          • #6
            You can also use my DataScraper script to do this: https://forums.homeseer.com/forum/3r...scraper-script


            Here is the Jon00DataScraper.ini Config

            Code:
            [Grab1]
            Path=https://api.enphaseenergy.com/api/v2/systems/67/summary?key=96a7de32fabc1dd8ff68ec43eca21c06&user_id=4d7a45774e6a41320a
            TextFile=1
            Encoding=
            Username=
            Password=
            Options=
            UserAgent=
            StripHTML=0
            UseIE=0
            Delay=2
            Pattern1=(?s){"system_id":(.*?),"modules":(.*?),"size_w":(.*?),"current_power":(.*?),"energy_today":(.*?),"energy_lifetime":(.*?),"summary_date":"(.*?)","source":"(.*?)","status":"(.*?)","operational_at":(.*?),"last_report_at":(.*?),"last_interval_end_at":(.*?)}
            DeviceName1=System ID
            DeviceText1=[0]
            DeviceValue1=[0]
            DeviceName2=Modules
            DeviceText2=[1]
            DeviceValue2=[1]
            DeviceName3=Size W
            DeviceText3=[2]
            DeviceValue3=[2]
            DeviceName4=Current Power
            DeviceText4=[3]
            DeviceValue4=[3]
            DeviceName5=Energy Today
            DeviceText5=[4] W
            DeviceValue5=[4]
            DeviceName6=Energy Lifetime
            DeviceText6=[5] W
            DeviceValue6=[5]
            DeviceName7=Summary Date
            DeviceText7=[6]
            DeviceName8=Source
            DeviceText8=[7]
            DeviceName9=Status
            DeviceText9=[8]
            DeviceName10=Operational
            DeviceText10=[9 unixdt]
            DeviceName11=Last Report
            DeviceText11=[10 unixdt]
            DeviceName12=Last Interval End
            DeviceText12=[11 unixdt]
            These are the devices which are created:

            Click image for larger version  Name:	API.PNG Views:	0 Size:	94.9 KB ID:	1328090
            Jon

            Comment


            • #7
              Originally posted by jon00 View Post
              You can also use my DataScraper script to do this: https://forums.homeseer.com/forum/3r...scraper-script
              I actually am using your scraper to accomplish this in the meantime. I am playing with the script that sparkman provided me, perhaps I can make something of it.

              One question though, is there any way to change the status icons based on the values? For instance, if the current power is below 100W have the icon showing an idle icon, but when over 100W show a different icon?

              Thanks for your help jon00 that ini file is far better than the one I was using, I'll have to update it!

              Comment


              • #8
                I have set the device values for those that can be set. You can therefore create your own value graphics pairs to each device by adding the following to the ini file:

                DeviceMode=2

                Check out page 4 in the docs
                Jon

                Comment


                • #9
                  Oh, now that makes sense. Sweet, thanks jon00, you are awesome!

                  Comment

                  Working...
                  X