Regen monitor script

Forum over Homeseer scripts (DUTCH forum)

Moderators: TANE, Ruud

Locked
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Klopt. In de nieuwe url staat ook niet de plaats. Dat was de eerste url die ik gebruikte. Kijk maar: http://www.weeronline.nl/Go/FlashCharts ... Id=4057886
Alexander
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Daar istie weer. Versie 2.2
Een aantal wijzigingen:
- Een extra parameter voor het bepalen of de string van het device in tekst moet staan (lichte regen, e.d.) of als een waarde ("0,20 mm/uur"). Zet je als parameter "yes" neer dan wordt het als tekst weggeschreven.
- Nog een extra parameter indien je wilt dat de 24 vermelde tijden in de XML van weeronline per device wordt vermeld. Daarbij moet je rekening houden met het gegeven dat de device die je als parameter meegeeft de eerste is uit de 24 devices. Ik heb bijv v7 als parameter. Bij het starten van het script zal het script controleren of v7 t/m v30 bestaat. Zo niet, dan zal het script de devices aanmaken. Zijn in die reeks bestaande devices, dan worden deze OVERSCHREVEN. Kijk dus of je een reeks vrij hebt! (ongetwijfeld!) De string van de devices zullen dan de tekst bevatten afhankelijk van de vorige ingevulde parameter + "om <tijdstip>".
- Bij niet multi device ondersteuning (dus parameter is "no" (of eigenlijk alles behalve "yes")) zal het ene device aangemaakt worden als deze niet bestaat. Daarbij zal gebruik worden gemaakt van de events die aangemaakt worden voor die 5 minuten intervallen. Bij multi device zal dit juist niet gedaan worden en krijgt iedere device DIRECT de value die opgegeven staat voor die interval. Dit moest ik wel doen, omdat het script recurring moet zijn en daarmee het event principe nooit zal werken, omdat de tijd binnen zo'n event telkens opschuift. Het eerste device zal dan telkens gevuld worden en de rest niet.

Nog even het nut van die multi-devices: Je zou in combinatie met andere events/scripts van te voren kunnen melden of op dat moment het regent of niet. Denk bijv maar even aan de was die buiten hangt, jullie/jij wilt even de deur uit en krijg van te voren gemeld of over twee uur het regent of niet, zodat of a) je op tijd terug moet om de was binnen te halen b) je de was direct binnenhaalt. Was kan natuurlijk vervangen door whatever (zonwering, open ramen?).

Helaas regent het weer niet, dus ik heb niet kunnen testen of de waarden goed worden meegenomen. Het werkte wel vanmiddag voordat ik ging verbouwen, maar durf mijn hand niet in het vuur te steken of dit zo gebleven is. Ik denk dat het wel werkt, maar zeker zijn doe ik niet. Graag testen dus.

Code: Select all

' RegenMonitor.vb script
' Author: Alexander
' Version: 2.2
' Last Update: 21-07-2010 23:40

' ChangeLog:
' 1.0: Initial
' 1.1: - Added the ability to stop the script if the website doesn't return a known text, if that is the case the device will get the status unknown.
'      - If the website would update the rain prediction to a lower time, the event that is already created will get the earlier condition.
'      - If the time of the rain prediction is suddenly earlier that the current time, the device will be set instantly and earlier created event is deleted.
'      - Parameters have to be added. Example ("Main","4057886;v7"). First is the geoAreaId and second is the device.
'      - First code optimization
'      - Code fault in handling if rain is predicted was not defined
' 2.0: - Script rewrite for other url
' 2.1: - Added DeviceStatusString
' 2.2: - Multi Devices & Rain measurement in text or number support. Activation by extra parameters like ("Main","4057886;v7;yes;yes")

Sub Main(ByVal Params As String)
    Dim strGeoAreaId As String = hs.StringItem(Params, 1, ";")
    Dim strDevice As String = hs.StringItem(Params, 2, ";")
    Dim blRainInText As Boolean = hs.StringItem(Params, 3, ";") = "yes"
    Dim blMultiDevice As Boolean = hs.StringItem(Params, 4, ";") = "yes"
    Dim strData As String = hs.GetURL("www.weeronline.nl", "/Go/FlashCharts/RainImmediate?geoAreaId=" & strGeoAreaId, false, 80)
    Dim i As Integer

    If blMultiDevice Then
        For i = 0 To 23
           If hs.DeviceExistsRef(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = -1 Then
              Dim dv As Object
              dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen" & "-" & i+1))
              dv.hc = GetChar(strDevice,1)
              dv.dc = Val(GetChar(strDevice,2))+i
              dv.misc = "&h10"
           End If
        Next
    Else
        If hs.DeviceExistsRef(GetChar(strDevice,1) & GetChar(strDevice,2)) = -1 Then
           Dim dv As Object
           dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen"))
           dv.hc = GetChar(strDevice,1)
           dv.dc = GetChar(strDevice,2)
           dv.misc = "&h10"
        End If
    End If

    If (Not InStr(strData, "?xml") > 0) Then
        hs.SetDeviceString(strDevice, "Website weeronline.nl down")
        hs.SetDeviceStatus(strDevice, 17)
        hs.SetDeviceLastChange(strDevice, Now)
    Else
        Dim strArr() As String
        Dim strDate(24) As String
        Dim strTime(24) As String
        Dim strValue(24) As String
       
        strArr = strdata.split(chr(13))
        Dim j As Integer

        For i = 12 To strArr.Length - 1
            If InStr(strArr(i), "<date>") > 0 Then
                Dim a As Integer = InStr(strArr(i), "<date>")
                Dim b As Integer = InStr(strArr(i), "</date>")
                Dim c As Integer = InStr(strArr(i), "<date>")
                Dim strTmp = strArr(i).SubString(a+5, b-c-6)
                strDate(j) = Left(strTmp, InStr(strTmp, "T")-1)
                strTime(j) = Mid(strTmp, InStr(strTmp, "T")+1)
            Else If InStr(strArr(i), "<item") > 0 Then
                Dim a As Integer = InStr(strArr(i), "value=")
                Dim b As Integer = InStr(strArr(i), " />")
                Dim c As Integer = InStr(strArr(i), "value=")
                strValue(j) = strArr(i).SubString(a+6, b-c-8)
                j +=1
            End If
        Next
      
        For i = 0 To j - 1
            Dim strEvent As String = strDate(i) & "_" & strTime(i)
            If hs.EventExists(strEvent) Then hs.DeleteEvent(strEvent)

            Dim lngValueCommand As Long = Math.Round((Left(strValue(i), InStr(strValue(i), " mm") - 1) * 100), 0)
            Dim strValueCommand As String
            If blMultiDevice Then
                strValueCommand = "&hs.SetDeviceValue(" & GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i & ", " & lngValueCommand.toString() & ")"
            Else
                strValueCommand = "&hs.SetDeviceValue(" & strDevice & ", " & lngValueCommand.toString() & ")"
            End If

            If (DateTime.Compare(strTime(i), FormatDateTime(Now, 4)) <= 0) Then
                If InStr(strValue(i), "0 mm/uur") > 0 Then
                    If blMultiDevice Then
                        If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off")
                        hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0)
                        hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                    Else
                        If Not hs.DeviceStatus(strDevice) = 3 Then hs.Transmit(strDevice, "off")
                        hs.SetDeviceValue(strDevice, 0)
                        hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText))
                    End If
                Else
                    If blMultiDevice Then
                        If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(strDevice, "on")
                        hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString())
                        hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                    Else
                        If Not hs.DeviceStatus(strDevice) = 2 Then hs.Transmit(strDevice, "on")
                        hs.SetDeviceValue(strDevice, lngValueCommand.toString())
                        hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText))
                    End If
                End If
            Else If Not blMultiDevice Then
                If (InStr(strValue(i), "0 mm/uur") > 0) And ((Not InStr(strValue(i-1), "0 mm/uur") > 0) or (i = 0)) Then
                    hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":off", 1, "", "")
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand)
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, "&hs.SetDeviceString(" & strDevice & ", " & WeerType(lngValueCommand, blRainInText) & ")")
                    hs.EnableEvent(strEvent)
                Else If (Not InStr(strValue(i), "0 mm/uur") > 0) Then
                    hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":on", 1, "", "")
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand)
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, "&hs.SetDeviceString(" & strDevice & ", " & WeerType(lngValueCommand, blRainInText) & ")")
                    hs.EnableEvent(strEvent)
                End If
            Else
                If InStr(strValue(i), "0 mm/uur") > 0 Then
                     If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off")
                     hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0)
                     hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                Else
                     If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(strDevice, "on")
                     hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString())
                     hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                End If
             End If
            'hs.writelog("RegenMonitor", strDate(i) & " - " & strTime(i) & " - " & strValue(i))
        Next
    End If
End Sub

Function WeerType(Value As Integer, blRainInText As Boolean) As String
    If blRainInText Then
       Select Case Value 
           Case 0
               Return "Geen regen"
           Case 1 To 100
               Return "Lichte regen"
           Case 101 To 500
               Return "Matige regen"
           Case 501 To 2500
               Return "Zware regen"
           Case Is > 2500
               Return "Wolkbreuk"
       End Select
    Else
       Return Math.Round(Value/100, 2).toString() & " mm/uur"
    End If
End Function

Wat nog te doen:
- iconen?
- meerdere woonplaatsen checken zodat afhankelijk van de windrichting de logische woonplaats te gebruiken voor verwachting
- ?
Alexander
Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

Re: Regen monitor script

Post by Digit »

Alexander wrote: Wat nog te doen:
- meerdere woonplaatsen checken zodat afhankelijk van de windrichting de logische woonplaats te gebruiken voor verwachting
Glad ijs :) Heuvels en dalen, en zo zijn er nog wel meer invloeden te bedenken. Ik bedoel, soms stevent een flinke bui recht op 'ons' af, maar met een heuveltje van +85 NAP in de buurt maakt die bui dan vaak toch maar een omweggetje... als je dat ook nog kan meenemen, dan mag je wat mij betreft je nick veranderen in Pelleboer :D
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Ja i know, maar nu is het ook niet helemaal werkend. Zevenhuizen bij mij regent het, Nieuwerkerk alleen maar 0-tjes, terwijl NIeuwerkerk vlakbij ligt en het toch even regende.
Alexander
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Ik zie toch ergens een fout in de website van weeronline. Of het is geen fout?
Bekijk onderstaande screendumps op hetzelfde moment genomen.
webpagina1.png
webpagina1.png (238.39 KiB) Viewed 10235 times
The attachment webpagina2.png is no longer available

De ene gebruikt: http://www.weeronline.nl/Go/FlashCharts ... Id=4951513
en de ander: http://www.weeronline.nl/Go/FlashCharts ... Id=4057886

Gewoon een andere regio?
Attachments
webpagina2.png
webpagina2.png (112.74 KiB) Viewed 10235 times
Alexander
User avatar
Snelvuur
Forum Moderator
Forum Moderator
Posts: 3156
Joined: Fri Apr 06, 2007 11:01 pm
Location: Netherlands
Contact:

Re: Regen monitor script

Post by Snelvuur »

Volgende keer even F11 indrukken heb je fullscreen, hoef je niet meer je bookmarks weg te vegen :)
// Erik (binkey.nl)
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

True.

Voor amsterdam:
op de hoofdpagina (wijzig plaats): http://www.weeronline.nl/Go/FlashCharts ... Id=4950076
doe je het rechtsboven invullen en dan de pagina bekijken: http://www.weeronline.nl/Go/FlashCharts ... Id=4058223

Ik denk dat ik moet concluderen dat de code verschil in de postcode zit die "bepaald" wordt. Ik adviseer jullie daarom dan nu ook om "wijzig postcode" te kiezen op de hoofdpagina van weeronline. Heb je ook gelijk een gerichter prognose. De code achterhalen is alleen dan niet zo makkelijk. Voor diegene die dit willen (en dus echt op de postcode), PM mij even je postcode en dan zoek ik even de code daarbij op.
Alexander
User avatar
RdP
Advanced Member
Advanced Member
Posts: 989
Joined: Thu May 04, 2006 10:14 am
Location: Netherlands

Re: Regen monitor script

Post by RdP »

Hi,

Tijdens m'n vakantie dit eens goed gelezen en dit is echt heel leuk en handig, ben sinds gisteren terug en zit lekker te prutsen.
Bedankt Alexander....

Ik heb een paar kleine toevoegingen toegepast:
1. Icons voor het type regen. (function weer)
-----------------

Code: Select all

Function WeerType(Value As Integer, blRainInText As Boolean) As String
    If blRainInText Then
       Select Case Value 
           Case 0
               Return "<img src='images\RainDetection\no_rain.png'>  Geen regen"
           Case 1 To 100
               Return "<img src='images\RainDetection\light_rain.png'>  Lichte regen"
           Case 101 To 500
               Return "<img src='images\RainDetection\medium_rain.png'>  Matige regen"
           Case 501 To 2500
               Return "<img src='images\RainDetection\heavy_rain.png'>  Zware regen"
           Case Is > 2500
               Return "<img src='images\RainDetection\thunderstorm.png'>  Wolkbreuk"
       End Select
    Else
       Return Math.Round(Value/100, 2).toString() & " mm/uur"
    End If
End Function
------------------------------
Zip file met icons is toegevoegd en kan je uitpakken in \html\Images directory of je gebruikt natuurlijk eigen icons.

2. ik heb location info en device type info toegevoegd bij het creëren van de devices.
----------------------------

Code: Select all

    If blMultiDevice Then
        For i = 0 To 23
           If hs.DeviceExistsRef(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = -1 Then
              Dim dv As Object
              dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen" & "-" & i+1))
              dv.hc = GetChar(strDevice,1)
              dv.dc = Val(GetChar(strDevice,2))+i
              dv.misc = "&h10"

              dv.location = "Virtual - Monitoring"
              dv.dev_type_string = "Expected Rain Monitoring"

           End If
        Next
    Else
        If hs.DeviceExistsRef(GetChar(strDevice,1) & GetChar(strDevice,2)) = -1 Then
           Dim dv As Object
           dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen"))
           dv.hc = GetChar(strDevice,1)
           dv.dc = GetChar(strDevice,2)
           dv.misc = "&h10"

           dv.location = "Virtual - Monitoring"
           dv.dev_type_string = "Expected Rain Monitoring"

        End If
    End If
---------------------------------

Nu nog even wachten tot het gaat regenen ;-)

Greetz en nogmaals bedankt,

Rien
Rien
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

briljant, thanks!
Alexander
ejdebruin1983
Starting Member
Starting Member
Posts: 31
Joined: Thu May 27, 2010 10:07 am

Re: Regen monitor script

Post by ejdebruin1983 »

wellicht doe ik iets stoms fout

maar ik krijg het script niet draaiend
dit staat er in de log... help.. :)

Code: Select all

23-7-2010 14:37:49   Info  Event Trigger "Regencheck" 
23-7-2010 14:37:49   Info  Running script in background: regenmonitor.vb("Main","4057605;v1").) 
23-7-2010 14:37:50   Error  Script compile error: Argument not specified for parameter 'data2' of 'Public Function Transmit(code As String, cmd As String, dimval As Integer, data2 As Integer, wait As Boolean, [raw As Boolean = False], [update As Boolean = True], [out_interface As Integer = 0]) As Integer'.on line 104 
23-7-2010 14:37:50   SCR  Option Strict Offimports Schedulerimports SystemPublic Module scriptcode4#Region "Automatically generated code, do not modify"'Automatically generated code, do not modify'Event Sources Begin Public WithEvents hs As Scheduler.hsapplication Public WithEvents hsp As scheduler.hsp Public WithEvents hssystem As scheduler.phone0'Event Sources End'End of automatically generated code#End Region' RegenMonitor.vb script' Author: Alexander' Version: 2.2' Last Update: 21-07-2010 23:40' ChangeLog:' 1.0: Initial' 1.1: - Added the ability to stop the script if the website doesn't return a known text, if that is the case the device will get the status unknown.' - If the website would update the rain prediction to a lower time, the event that is already created will get the earlier condition.' - If the time of the rain prediction is suddenly earlier that the current time, the device will be set instantly and earlier created event is deleted.' - Parameters have to be added. Example ("Main","4057886;v7"). First is the geoAreaId and second is the device.' - First code optimization' - Code fault in handling if rain is predicted was not defined' 2.0: - Script rewrite for other url' 2.1: - Added DeviceStatusString' 2.2: - Multi Devices & Rain measurement in text or number support. Activation by extra parameters like ("Main","4057886;v7;yes;yes")Sub Main(ByVal Params As String) Dim strGeoAreaId As String = hs.StringItem(Params, 1, ";") Dim strDevice As String = hs.StringItem(Params, 2, ";") Dim blRainInText As Boolean = hs.StringItem(Params, 3, ";") = "yes" Dim blMultiDevice As Boolean = hs.StringItem(Params, 4, ";") = "yes" Dim strData As String = hs.GetURL("www.weeronline.nl", "/Go/FlashCharts/RainImmediate?geoAreaId=" & strGeoAreaId, false, 80) Dim i As Integer If blMultiDevice Then For i = 0 To 23 If hs.DeviceExistsRef(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = -1 Then Dim dv As Object dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen" & "-" & i+1)) dv.hc = GetChar(strDevice,1) dv.dc = Val(GetChar(strDevice,2))+i dv.misc = "&h10" End If Next Else If hs.DeviceExistsRef(GetChar(strDevice,1) & GetChar(strDevice,2)) = -1 Then Dim dv As Object dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen")) dv.hc = GetChar(strDevice,1) dv.dc = GetChar(strDevice,2) dv.misc = "&h10" End If End If If (Not InStr(strData, "?xml") > 0) Then hs.SetDeviceString(strDevice, "Website weeronline.nl down") hs.SetDeviceStatus(strDevice, 17) hs.SetDeviceLastChange(strDevice, Now) Else Dim strArr() As String Dim strDate(24) As String Dim strTime(24) As String Dim strValue(24) As String strArr = strdata.split(chr(13)) Dim j As Integer For i = 12 To strArr.Length - 1 If InStr(strArr(i), "") > 0 Then Dim a As Integer = InStr(strArr(i), "") Dim b As Integer = InStr(strArr(i), "") Dim c As Integer = InStr(strArr(i), "") Dim strTmp = strArr(i).SubString(a+5, b-c-6) strDate(j) = Left(strTmp, InStr(strTmp, "T")-1) strTime(j) = Mid(strTmp, InStr(strTmp, "T")+1) Else If InStr(strArr(i), " 0 Then Dim a As Integer = InStr(strArr(i), "value=") Dim b As Integer = InStr(strArr(i), " />") Dim c As Integer = InStr(strArr(i), "value=") strValue(j) = strArr(i).SubString(a+6, b-c-8) j +=1 End If Next For i = 0 To j - 1 Dim strEvent As String = strDate(i) & "_" & strTime(i) If hs.EventExists(strEvent) Then hs.DeleteEvent(strEvent) Dim lngValueCommand As Long = Math.Round((Left(strValue(i), InStr(strValue(i), " mm") - 1) * 100), 0) Dim strValueCommand As String If blMultiDevice Then strValueCommand = "&hs.SetDeviceValue(" & GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i & ", " & lngValueCommand.toString() & ")" Else strValueCommand = "&hs.SetDeviceValue(" & strDevice & ", " & lngValueCommand.toString() & ")" End If If (DateTime.Compare(strTime(i), FormatDateTime(Now, 4)) <= 0) Then If InStr(strValue(i), "0 mm/uur") > 0 Then If blMultiDevice Then If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off") hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0) hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i)) Else If Not hs.DeviceStatus(strDevice) = 3 Then hs.Transmit(strDevice, "off") hs.SetDeviceValue(strDevice, 0) hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText)) End If Else If blMultiDevice Then If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(strDevice, "on") hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString()) hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i)) Else If Not hs.DeviceStatus(strDevice) = 2 Then hs.Transmit(strDevice, "on") hs.SetDeviceValue(strDevice, lngValueCommand.toString()) hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText)) End If End If Else If Not blMultiDevice Then If (InStr(strValue(i), "0 mm/uur") > 0) And ((Not InStr(strValue(i-1), "0 mm/uur") > 0) or (i = 0)) Then hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":off", 1, "", "") hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand) hs.AddAction(hs.GetEventRefByName(strEvent), 5, "&hs.SetDeviceString(" & strDevice & ", " & WeerType(lngValueCommand, blRainInText) & ")") hs.EnableEvent(strEvent) Else If (Not InStr(strValue(i), "0 mm/uur") > 0) Then hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":on", 1, "", "") hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand) hs.AddAction(hs.GetEventRefByName(strEvent), 5, "&hs.SetDeviceString(" & strDevice & ", " & WeerType(lngValueCommand, blRainInText) & ")") hs.EnableEvent(strEvent) End If Else If InStr(strValue(i), "0 mm/uur") > 0 Then If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off") hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0) hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i)) Else If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(strDevice, "on") hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString()) hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i)) End If End If 'hs.writelog("RegenMonitor", strDate(i) & " - " & strTime(i) & " - " & strValue(i)) Next End IfEnd SubFunction WeerType(Value As Integer, blRainInText As Boolean) As String If blRainInText Then Select Case Value Case 0 Return "Geen regen" Case 1 To 100 Return "Lichte regen" Case 101 To 500 Return "Matige regen" Case 501 To 2500 Return "Zware regen" Case Is > 2500 Return "Wolkbreuk" End Select Else Return Math.Round(Value/100, 2).toString() & " mm/uur" End IfEnd FunctionEnd Module
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Die "." en ")" aan het einde van je tweede log regel lijkt mij niet te kloppen. Bekijk even het event of die er toevallig staan, zo ja: weghalen.
Alexander
Bastiaan
Senior Member
Senior Member
Posts: 1257
Joined: Sat May 24, 2008 11:36 am
Location: Netherlands
Contact:

Re: Regen monitor script

Post by Bastiaan »

Misschien wil je nog even het complete script met eventueel de veranderingen van Rien hier zetten?
Draaien we allemaal hetzelfde ipv van deeltjes copy pasten?

gr Bastiaan
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Onderstaand script is versie 2.3.
Wijzigingen:
- De images die Rien toegevoegd heeft, maar daarbij heb ik de mapnaam aangepast ivm de script naam die ook zo heet.
- De code toegevoegd om de images van Rien te gebruiken, en daarbij eveneens opgenomen dat de string ook gecentreerd staat.
- Diezelfde images worden ook getoond zodra je gekozen hebt om de value te zien ipv een tekst.
- Bugfix in de code string, waardoor een opdracht niet uitgevoerd werd.
- Device locatie en type toegevoegd zoals Rien voorgesteld, alleen als variabelen opgenomen bovenaan het script.

Code: Select all

' RegenMonitor.vb script
' Author: Alexander
' Version: 2.3
' Last Update: 23-07-2010 17:52

' ChangeLog:
' 1.0: Initial
' 1.1: - Added the ability to stop the script if the website doesn't return a known text, if that is the case the device will get the status unknown.
'      - If the website would update the rain prediction to a lower time, the event that is already created will get the earlier condition.
'      - If the time of the rain prediction is suddenly earlier that the current time, the device will be set instantly and earlier created event is deleted.
'      - Parameters have to be added. Example ("Main","4057886;v7"). First is the geoAreaId and second is the device.
'      - First code optimization
'      - Code fault in handling if rain is predicted was not defined
' 2.0: - Script rewrite for other url
' 2.1: - Added DeviceStatusString
' 2.2: - Multi Devices support
' 2.3: - Bugfix about the double quotes which didn't were added in setdevicestring commands
'      - Added images from Rien and included those in the script for display
'      - Added DeviceLocation and DeviceType option when created idea by Rien

Sub Main(ByVal Params As String)
    ' Customize this value if you want to change the new created device(s) if not already existing
    Dim strDeviceLocation As String = "Virtual - Monitoring"
    Dim strDeviceType As String = "Verwachte Regen Monitoring"



    Dim strGeoAreaId As String = hs.StringItem(Params, 1, ";")
    Dim strDevice As String = hs.StringItem(Params, 2, ";")
    Dim blRainInText As Boolean = hs.StringItem(Params, 3, ";") = "yes"
    Dim blMultiDevice As Boolean = hs.StringItem(Params, 4, ";") = "yes"
    Dim strData As String = hs.GetURL("www.weeronline.nl", "/Go/FlashCharts/RainImmediate?geoAreaId=" & strGeoAreaId, false, 80)
    Dim i As Integer

    If blMultiDevice Then
        For i = 0 To 23
           If hs.DeviceExistsRef(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = -1 Then
              Dim dv As Object
              dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen" & "-" & i+1))
              dv.hc = GetChar(strDevice,1)
              dv.dc = Val(GetChar(strDevice,2))+i
              dv.misc = "&h10"
              dv.location = strDeviceLocation
              dv.dev_type_string = strDeviceType
           End If
        Next
    Else
        If hs.DeviceExistsRef(GetChar(strDevice,1) & GetChar(strDevice,2)) = -1 Then
           Dim dv As Object
           dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen"))
           dv.hc = GetChar(strDevice,1)
           dv.dc = GetChar(strDevice,2)
           dv.misc = "&h10"
           dv.location = strDeviceLocation
           dv.dev_type_string = strDeviceType
        End If
    End If

    If (Not InStr(strData, "?xml") > 0) Then
        hs.SetDeviceString(strDevice, "Website weeronline.nl down")
        hs.SetDeviceStatus(strDevice, 17)
        hs.SetDeviceLastChange(strDevice, Now)
    Else
        Dim strArr() As String
        Dim strDate(24) As String
        Dim strTime(24) As String
        Dim strValue(24) As String
       
        strArr = strdata.split(chr(13))
        Dim j As Integer

        For i = 12 To strArr.Length - 1
            If InStr(strArr(i), "<date>") > 0 Then
                Dim a As Integer = InStr(strArr(i), "<date>")
                Dim b As Integer = InStr(strArr(i), "</date>")
                Dim c As Integer = InStr(strArr(i), "<date>")
                Dim strTmp = strArr(i).SubString(a+5, b-c-6)
                strDate(j) = Left(strTmp, InStr(strTmp, "T")-1)
                strTime(j) = Mid(strTmp, InStr(strTmp, "T")+1)
            Else If InStr(strArr(i), "<item") > 0 Then
                Dim a As Integer = InStr(strArr(i), "value=")
                Dim b As Integer = InStr(strArr(i), " />")
                Dim c As Integer = InStr(strArr(i), "value=")
                strValue(j) = strArr(i).SubString(a+6, b-c-8)
                j +=1
            End If
        Next
      
        For i = 0 To j - 1
            Dim strEvent As String = strDate(i) & "_" & strTime(i)
            If hs.EventExists(strEvent) Then hs.DeleteEvent(strEvent)

            Dim lngValueCommand As Long = Math.Round((Left(strValue(i), InStr(strValue(i), " mm") - 1) * 100), 0)
            Dim strValueCommand As String
            If blMultiDevice Then
                strValueCommand = "&hs.SetDeviceValue(" & GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i & ", " & lngValueCommand.toString() & ")"
            Else
                strValueCommand = "&hs.SetDeviceValue(" & strDevice & ", " & lngValueCommand.toString() & ")"
            End If

            If (DateTime.Compare(strTime(i), FormatDateTime(Now, 4)) <= 0) Then
                If InStr(strValue(i), "0 mm/uur") > 0 Then
                    If blMultiDevice Then
                        If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off")
                        hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0)
                        hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                    Else
                        If Not hs.DeviceStatus(strDevice) = 3 Then hs.Transmit(strDevice, "off")
                        hs.SetDeviceValue(strDevice, 0)
                        hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText))
                    End If
                Else
                    If blMultiDevice Then
                        If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(strDevice, "on")
                        hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString())
                        hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                    Else
                        If Not hs.DeviceStatus(strDevice) = 2 Then hs.Transmit(strDevice, "on")
                        hs.SetDeviceValue(strDevice, lngValueCommand.toString())
                        hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText))
                    End If
                End If
            Else If Not blMultiDevice Then
                If (InStr(strValue(i), "0 mm/uur") > 0) And ((Not InStr(strValue(i-1), "0 mm/uur") > 0) or (i = 0)) Then
                    hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":off", 1, "", "")
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand)
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, Chr(34) & "&hs.SetDeviceString(" & strDevice & ", " & Chr(34) & WeerType(lngValueCommand, blRainInText) & Chr(34) & ")")
                    hs.EnableEvent(strEvent)
                Else If (Not InStr(strValue(i), "0 mm/uur") > 0) Then
                    hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":on", 1, "", "")
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand)
                    hs.AddAction(hs.GetEventRefByName(strEvent), 5, Chr(34) & "&hs.SetDeviceString(" & strDevice & ", " & Chr(34) & WeerType(lngValueCommand, blRainInText) & Chr(34) & ")")
                    hs.EnableEvent(strEvent)
                End If
            Else
                If InStr(strValue(i), "0 mm/uur") > 0 Then
                     If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off")
                     hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0)
                     hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                Else
                     If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(strDevice, "on")
                     hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString())
                     hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i))
                End If
             End If
            'hs.writelog("RegenMonitor", strDate(i) & " - " & strTime(i) & " - " & strValue(i))
        Next
    End If
End Sub

Function WeerType(Value As Integer, blRainInText As Boolean) As String
    If blRainInText Then
       Select Case Value 
           Case 0
               Return "<img align='absmiddle' src='images/RegenMonitor/no_rain.png'> Geen regen"
           Case 1 To 100
               Return "<img align='absmiddle' src='images/RegenMonitor/light_rain.png'> Lichte regen"
           Case 101 To 500
               Return "<img align='absmiddle' src='images/RegenMonitor/medium_rain.png'> Matige regen"
           Case 501 To 2500
               Return "<img align='absmiddle' src='images/RegenMonitor/heavy_rain.png'> Zware regen"
           Case Is > 2500
               Return "<img align='absmiddle' src='images/RegenMonitor/heavy_rain.png'> Wolkbreuk"
       End Select
    Else
       Dim strTmp As String
       Select Case Value 
           Case 0
               strTmp = "<img align='absmiddle' src='images/RegenMonitor/no_rain.png'>"
           Case 1 To 100
               strTmp = "<img align='absmiddle' src='images/RegenMonitor/light_rain.png'>"
           Case 101 To 500
               strTmp = "<img align='absmiddle' src='images/RegenMonitor/medium_rain.png'>"
           Case 501 To 2500
               strTmp = "<img align='absmiddle' src='images/RegenMonitor/heavy_rain.png'>"
           Case Is > 2500
               strTmp = "<img src='images/RegenMonitor/heavy_rain.png'>"
       End Select
       Return strTmp & " " & Math.Round(Value/100, 2).toString() & " mm/uur"
    End If
End Function
PM mij met je postcode zodat ik je de juiste code kan retourneren.
Attachments
RegenMonitor.zip
de images plaatsen in <HS path>\html\images
(10.95 KiB) Downloaded 436 times
Alexander
Alexander
Global Moderator
Global Moderator
Posts: 1532
Joined: Sat Mar 10, 2007 11:19 pm
Location: Netherlands

Re: Regen monitor script

Post by Alexander »

Bastiaan wrote:Misschien wil je nog even het complete script met eventueel de veranderingen van Rien hier zetten?
Draaien we allemaal hetzelfde ipv van deeltjes copy pasten?

gr Bastiaan
Jouw submit was net eerder dan die van mij ;-)
Alexander
Bastiaan
Senior Member
Senior Member
Posts: 1257
Joined: Sat May 24, 2008 11:36 am
Location: Netherlands
Contact:

Re: Regen monitor script

Post by Bastiaan »

Lekker bezig :-)

Ik krijg de plaatjes van Rien er niet lekker uit:
De link die op de status pagina komt:
http://192.168.1.4/images%5CRegenMonitor%5Cno_rain.png
Dus er lijkt iets met de aanmaak code voor device V7 niet helemaal lekker. %5C komt me niet erg bekend voor...

(Ik wis elke keer alle events en Devices voor een test run)
Locked

Return to “Homeseer Scripts Forum”