Sub Main(ByVal pParams() As String) End Sub Sub Ny_Termostat(ByVal pParam As String) 'Parameters: Housecode for Termostat Devices | Device address for Temp sensor Dim pParams() As String = pParam.ToString.Split("|") Dim sHouseCode As String = pParams(0) Dim sTempSensorAddress As String = pParams(1) Dim EVENT_GROUP As String = "Termostat" Dim sLocation As String = "Termostat" ' For testing Dim sLocation2 As String = "Termostat" ' For testing Dim dDeviceSensor As Object Dim rDeviceSensorref As Object ' ----------- Get sensor parameters rDeviceSensorref = hs.GetDeviceRef(sTempSensorAddress) If rDeviceSensorref < 0 Then hs.WriteLog("Error", "Unknown Device requested as Temp Sensor for Termostat: " & sTempSensorAddress) Exit Sub End If dDeviceSensor = hs.GetDeviceByRef(rDeviceSensorref) Dim sTermostatName As String = dDeviceSensor.location sLocation = dDeviceSensor.location ' Comment out this line for testing sLocation2 = dDeviceSensor.location2 ' Comment out this line for testing Dim iDev As Integer Dim oTempDev As Object Dim pDevice As String Dim sMyScript As String Dim oEvent As Object Dim lRef As Long ' -------- Adding Controls ' Add Main iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat CONTROL") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = CStr(iDev) oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev hs.WriteLog("Termostat script", "Bulding Devices for new termostat statring at" & pDevice) hs.DeviceValuesAdd(pDevice, "OFF" & Chr(2) & "0" & Chr(1) & _ "AUTO" & Chr(2) & "50" & Chr(1) & _ "ON" & Chr(2) & "100", True) ' Add Value Changed Event oEvent = hs.NewEventEx(sTermostatName & "Termostate CONTROL Changed") oEvent.group = EVENT_GROUP oEvent.ev_abs_time = 12 ' Value change oEvent.ev_trig_hc = oTempDev.ref oEvent.ev_trig_dc = 6 ' Set to Any sMyScript = "termostat.vb(""Check_temp"",""" & sTempSensorAddress & """)" hs.AddAction(oEvent.EvRef, 5, sMyScript) hs.EnableEventByRef(oEvent.EvRef) ' Add Max iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Max Temp") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev sMyScript = "termostat.vb(""Down"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Down") sMyScript = "termostat.vb(""Up"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Up") hs.SetDeviceValue(pDevice, "240") hs.SetDeviceString(pDevice, "24°C") ' Add Hysteresis iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Hysteresis") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev sMyScript = "termostat.vb(""Down"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Down") sMyScript = "termostat.vb(""Up"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Up") hs.SetDeviceValue(pDevice, "5") hs.SetDeviceString(pDevice, "0,5°C") ' Add Night Control iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Night Reduction CONTROL") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = CStr(iDev) oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev hs.SetDeviceStatus(pDevice, 3) ' OFF ' Add Status Change Event oEvent = hs.NewEventEx(sTermostatName & "Termostate Night Reduction CONTROL Changed") oEvent.group = EVENT_GROUP oEvent.ev_abs_time = 11 ' status change oEvent.ev_trig_hc = oTempDev.ref oEvent.ev_trig_dc = 23 ' Set to Any sMyScript = "termostat.vb(""Check_temp"",""" & sTempSensorAddress & """)" hs.AddAction(oEvent.EvRef, 5, sMyScript) hs.EnableEventByRef(oEvent.EvRef) ' Add Night MAx Temp iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Max Night Temp") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev sMyScript = "termostat.vb(""Down"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Down") sMyScript = "termostat.vb(""Up"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Up") hs.SetDeviceValue(pDevice, "200") hs.SetDeviceString(pDevice, "20°C") ' Add Night Start iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Night Start Time") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev hs.SetDeviceString(pDevice, "00:00") sMyScript = "termostat.vb(""AddOneHour"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "+1 Hour") sMyScript = "termostat.vb(""SubOneHour"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "-1 Hour") sMyScript = "termostat.vb(""Add10Min"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "+10 Min") ' Add Night End iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Night End Time") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev hs.SetDeviceString(pDevice, "05:00") sMyScript = "termostat.vb(""AddOneHour"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "+1 Hour") sMyScript = "termostat.vb(""SubOneHour"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "-1 Hour") sMyScript = "termostat.vb(""Add10Min"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "+10 Min") ' Add Hollyday Control iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Hollyday Reduction CONTROL") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = CStr(iDev) oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev hs.SetDeviceStatus(pDevice, 3) ' OFF ' Add Status Change Event oEvent = hs.NewEventEx(sTermostatName & "Termostate Holyday Reduction CONTROL Changed") oEvent.group = EVENT_GROUP oEvent.ev_abs_time = 11 ' status change oEvent.ev_trig_hc = oTempDev.ref oEvent.ev_trig_dc = 23 ' Set to Any sMyScript = "termostat.vb(""Check_temp"",""" & sTempSensorAddress & """)" hs.AddAction(oEvent.EvRef, 5, sMyScript) hs.EnableEventByRef(oEvent.EvRef) ' Add MAx Hollyday Reduction iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Max Hollyday Temp") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev sMyScript = "termostat.vb(""Down"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Down") sMyScript = "termostat.vb(""Up"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Up") hs.SetDeviceValue(pDevice, "150") hs.SetDeviceString(pDevice, "15°C") ' Add Hollyday End iDev = GetFirstAvaliableDeviceOnHousecode(sHouseCode) oTempDev = hs.NewDeviceEx("Termostat Hollyday Date End") oTempDev.location = sLocation oTempDev.location2 = sLocation2 oTempDev.hc = sHouseCode oTempDev.dc = iDev oTempDev.misc = &H10 oTempDev.dev_type_string = "Virtual" pDevice = sHouseCode & iDev sMyScript = "termostat.vb(""Reset"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "Reset") sMyScript = "termostat.vb(""AddOneDay"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "+1 Day") sMyScript = "termostat.vb(""AddOneWeek"",""" & pDevice & """)" hs.DeviceButtonAdd(pDevice, sMyScript, "+1 Week") hs.SetDeviceString(pDevice, Today) ' -------------- Events ' Add Turn ON Event oEvent = hs.NewEventEx("Turn ON " & sTermostatName & " Heating") oEvent.group = EVENT_GROUP hs.EnableEventByRef(oEvent.EvRef) ' Add Turn OFF Event oEvent = hs.NewEventEx("Turn OFF " & sTermostatName & " Heating") oEvent.group = EVENT_GROUP hs.EnableEventByRef(oEvent.EvRef) ' check if main event is present If Not hs.EventExists("Check All Termostates") Then oEvent = hs.NewEventEx("Check All Termostates") oEvent.group = EVENT_GROUP oEvent.ev_abs_time = 6 ' recuring oEvent.rec_mins = 10 ' Interval in minutes hs.EnableEventByRef(oEvent.EvRef) End If ' add Check on this termostat lRef = hs.GetEventRefByName("Check All Termostates") sMyScript = "termostat.vb(""Check_temp"",""" & sTempSensorAddress & """)" hs.AddAction(lRef, 11, 0, 2, 0) ' wait 2 sec to aviod net congestion hs.AddAction(lRef, 5, sMyScript) ' Run script hs.WriteLog("Termostat script", "New termostat events created") End Sub ' ------- Buttons Sub Up(ByVal pDevice As String) Dim oldval As Single = hs.DeviceValue(pDevice) / 10 Dim newval As Single = oldval + 0.5 hs.SetDeviceValue(pDevice, newval * 10) hs.SetDeviceString(pDevice, newval.ToString("F1") & "°C") End Sub Sub Down(ByVal pDevice As String) Dim oldval As Single = hs.DeviceValue(pDevice) / 10 Dim newval As Single = oldval - 0.5 hs.SetDeviceValue(pDevice, newval * 10) hs.SetDeviceString(pDevice, newval.ToString("F1") & "°C") End Sub Sub Reset(ByVal pDevice As String) hs.SetDeviceString(pDevice, DateAdd("d", -1, Today)) End Sub Sub AddOneDay(ByVal pDevice As String) Dim oldval As Date = CDate(hs.DeviceString(pDevice)) hs.SetDeviceString(pDevice, DateAdd("d", 1, oldval)) End Sub Sub AddOneWeek(ByVal pDevice As String) Dim oldval As Date = CDate(hs.DeviceString(pDevice)) hs.SetDeviceString(pDevice, DateAdd("d", 7, oldval)) End Sub Sub AddOneHour(ByVal pDevice As String) Dim oldval As Date = CDate(hs.DeviceString(pDevice)) hs.SetDeviceString(pDevice, Format(DateAdd("h", 1, oldval), "HH:mm")) End Sub Sub SubOneHour(ByVal pDevice As String) Dim oldval As Date = CDate(hs.DeviceString(pDevice)) hs.SetDeviceString(pDevice, Format(DateAdd("h", -1, oldval), "HH:mm")) End Sub Sub Add10Min(ByVal pDevice As String) Dim oldval As Date = CDate(hs.DeviceString(pDevice)) hs.SetDeviceString(pDevice, Format(DateAdd("n", 10, oldval), "HH:mm")) End Sub '----------------------- TEMP CHECK -------------------- Sub Check_temp(ByVal pParam As String) ' Parameter: Device address for Temp Sensor Dim sTempSensorAddress As String = pParam Dim dDeviceSensor As Object Dim rDeviceSensorref As Object rDeviceSensorref = hs.GetDeviceRef(sTempSensorAddress) If rDeviceSensorref < 0 Then hs.WriteLog("Error", "Unknown Temp sensor Device: " & sTempSensorAddress) Exit Sub End If dDeviceSensor = hs.GetDeviceByRef(rDeviceSensorref) Dim sLocation As String = dDeviceSensor.location Dim dHolydayEnd As Date Dim sTimeFrom As String Dim sTimeTo As String Dim iCurrTemp As Integer = hs.DeviceValue(sTempSensorAddress) Dim iMaxTemp As Integer = hs.DeviceValueByName(sLocation & " Termostat Max Temp") ' Temp * 10 Dim iHysteresis As Integer = hs.DeviceValueByName(sLocation & " Termostat Hysteresis") ' Temp * 10 Dim iMinTemp As Integer = iMaxTemp - iHysteresis If hs.DeviceValueByName(sLocation & " Termostat CONTROL") = 0 Then ' OFF hs.WriteLog("Termostat script", "Override OFF so Turning OFF Heating at " & sLocation) hs.TriggerEvent("Turn OFF " & sLocation & " Heating") ElseIf hs.DeviceValueByName(sLocation & " Termostat CONTROL") = 100 Then ' ON hs.WriteLog("Termostat script", "Override ON so Turning ON Heating at " & sLocation) hs.TriggerEvent("Turn ON " & sLocation & " Heating") Else ' Auto hs.WriteLog("Termostat script", "Checking Dev: " & dDeviceSensor.name & "(" & sTempSensorAddress & ") in location: " & sLocation & " Measured Temp is " & iCurrTemp / 10) If hs.IsOnByName(sLocation & " Termostat Hollyday Reduction CONTROL") Then ' Hollyday ON hs.WriteLog("Termostat script", "Hollyday Reduction mode is ON at " & sLocation) dHolydayEnd = CDate(hs.DeviceStringByName(sLocation & " Termostat Hollyday Date End")) If dHolydayEnd < Today Then SetDeviceStatusByName(sLocation & " Termostat Hollyday Reduction CONTROL", 3) ' skru av hollyday Check_temp(pParam) ' Call me to start over Else iMaxTemp = hs.DeviceValueByName(sLocation & " Termostat Max Hollyday Temp") iMinTemp = iMaxTemp - iHysteresis compare_temp(sLocation, iCurrTemp, iMinTemp, iMaxTemp) End If Else ' No Hollyday If hs.IsOnByName(sLocation & " Termostat Night Reduction CONTROL") Then ' Night Reduction ON hs.WriteLog("Termostat script", "Night reduction mode is ON at " & sLocation) sTimeFrom = hs.DeviceStringByName(sLocation & " Termostat Night Start Time") sTimeTo = hs.DeviceStringByName(sLocation & " Termostat Night End Time") If IsNight(sTimeFrom, sTimeTo) Then iMaxTemp = hs.DeviceValueByName(sLocation & " Termostat Max Night") iMinTemp = iMaxTemp - iHysteresis compare_temp(sLocation, iCurrTemp, iMinTemp, iMaxTemp) Else 'No Night compare_temp(sLocation, iCurrTemp, iMinTemp, iMaxTemp) ' Normal Day temp End If Else ' Night Reduction OFF compare_temp(sLocation, iCurrTemp, iMinTemp, iMaxTemp) ' Normal Day temp End If End If End If End Sub ' --------------------- Sercive Routines Function GetFirstAvaliableDeviceOnHousecode(ByVal sHouseCode As String) As Integer Dim iDev = 0 Dim iStatus As Integer Do iDev = iDev + 1 iStatus = hs.DeviceExistsRef(sHouseCode & Trim(iDev)) Loop Until iStatus = -1 GetFirstAvaliableDeviceOnHousecode = iDev End Function Sub SetDeviceStatusByName(ByVal sDeviveName As String, ByVal iStatus As Integer) Dim dvRef As Object Dim dv As Object dvRef = hs.GetDeviceRefByName(sDeviveName) If dvRef > 0 Then dv = hs.GetDeviceByRef(dvRef) hs.SetDeviceStatus(dv.hc & dv.dc, iStatus) End If End Sub Sub compare_temp(ByVal sLocation As String, ByVal iCurrTemp As Integer, ByVal iMinTemp As Integer, ByVal iMaxTemp As Integer) hs.WriteLog("Termostat script", "Comparing Temp: " & iCurrTemp & "to MAx temp: " & iMAxTemp & " and Min temp: " & iMinTemp & " at " & sLocation) If iCurrTemp >= iMaxTemp Then hs.WriteLog("Termostat script", "Turning OFF Heating at " & sLocation) hs.TriggerEvent("Turn OFF " & sLocation & " Heating") Else If iCurrTemp <= iMinTemp Then hs.WriteLog("Termostat script", "Turning ON Heating at " & sLocation) hs.TriggerEvent("Turn ON " & sLocation & " Heating") End If End If End Sub Function IsNight(ByVal sNightStart As String, ByVal sNightEnd As String) As Boolean Dim tTimeFrom As Date = TimeValue(sNightStart) Dim tTimeTo As Date = TimeValue(sNightEnd) IsNight = False If tTimeFrom > tTimeTo Then ' Crossing midnight If tTimeFrom <= TimeValue(Now) And TimeValue(Now) <= TimeValue("00:00") Then ' before midnight IsNight = True ElseIf TimeValue("00:00") <= TimeValue(Now) And TimeValue(Now) <= tTimeTo Then ' after midnight IsNight = True End If Else If tTimeFrom <= TimeValue(Now) And TimeValue(Now) <= tTimeTo Then IsNight = True End If End If End Function