The below script can be run periodically from an event to create and update HS devices based upon the reporting provided from this energy provider. It is setup to pull the cost including VAT, but this could be changed by editing the 4th line to be "exc" rather than "inc". Whoever uses this provider can change the URL for their specific account. This account is based upon information in another thread.
The data is reported in 30 minute intervals so each of the two is added to form the hourly total. The parent device contains the data of the last item reported from the site. If a different day such as earlier in the 24 hour period then the sDay logic can be changed in the script.
Code:
Sub Main(ByVal Parms As Object) 'Get the data Dim sDownload As String = hs.GetURL("https://api.octopus.energy", "/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-N/standard-unit-rates/?period_from=", True, 80) Dim arrS() As String = Split(sDownload, "value_inc_vat"":") Dim sDay As String = "" 'accumulate the 30 minute values Dim nHourly(23) As Double For i As Integer = 0 To 23 nHourly(i) = 0.0 Next For i As Integer = 1 To arrS.Length - 1 Dim arrItem() As String = arrS(i).Split(",") Dim sUse As String = arrItem(0) Dim sStartTime As String = arrItem(2) Dim arrDate() As String = sStartTime.Split("T") Dim arrDay() As String = arrDate(0).Split("""") sDay = arrDay(arrDay.Length - 1) Dim arrHour() As String = arrDate(1).Split(":") Dim index = CType(arrHour(0), Integer) nHourly(index) = nHourly(index) + CType(sUse, Double) Next 'store values for each hour, create HS device is not existing Dim sDeviceLocation As String = "Energy" Dim sDateName As String = "Last Update Date" Dim iRef As Integer Dim iParentRef As Integer iParentRef = hs.GetDeviceRefByName(sDeviceLocation & " " & sDateName) If iParentRef = -1 Then iParentRef = hs.NewDeviceRef(sDateName) Dim dv As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(iParentRef) dv.Location(hs) = sDeviceLocation dv.Relationship(hs) = Enums.eRelationship.Parent_Root End If hs.SetDeviceString(iParentRef, sDay, True) For i As Integer = 0 To 23 Dim sDeviceName As String = "Hour " & i.ToString() & " Use" iRef = hs.GetDeviceRefByName(sDeviceLocation & " " & sDeviceName) If iRef = -1 Then iRef = hs.NewDeviceRef(sDeviceName) Dim dv As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(iRef) dv.Location(hs) = sDeviceLocation dv.Relationship(hs) = Enums.eRelationship.Child dv.AssociatedDevice_Add(hs, iParentRef) Dim dvParent As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(iParentRef) dvParent.AssociatedDevice_Add(hs, iRef) Dim Pair As New HomeSeerAPI.VSPair(HomeSeerAPI.ePairStatusControl.Status) Pair.PairType = HomeSeerAPI.VSVGPairType.Range Pair.RangeStart = &H80000000 Pair.RangeEnd = &H7FFFFFFF Pair.RangeStatusDecimals = 2 Pair.IncludeValues = True Pair.ValueOffset = 0 Pair.RangeStatusPrefix = "" Pair.RangeStatusSuffix = "" Pair.ControlUse = HomeSeerAPI.ePairControlUse.Not_Specified hs.DeviceVSP_AddPair(iRef, Pair) Dim vgPair As New HomeSeerAPI.VSVGPairs.VGPair() vgPair.PairType = HomeSeerAPI.VSVGPairs.VSVGPairType.Range vgPair.RangeStart = &H80000000 vgPair.RangeEnd = &H7FFFFFFF vgPair.Graphic = "/images/HomeSeer/status/electricity.gif" hs.DeviceVGP_AddPair(iRef, vgPair) End If hs.SetDeviceValueByRef(iRef, Math.Round(nHourly(i), 2), True) Next End Sub
Comment