Takker og bukker!
High og Auto ble motsatt, så da blir scriptet slik(starter dagen litt tidligere enn dere)
' Moskus 2017'Version 0.0.1Private scriptName As String = "VirtualThermostat"Private scriptFile As String = scriptName & ".vb"Public Sub Main(ByVal rootDeviceRef As Object) 'Devices: 'Root: Heating, Cooling 'Mode: Manual, High, Low, Off 'Thermostat: Range 5 - 40, step 0.5 'Temperature: Adjusted for locationEnd SubPrivate Enum Modes Off = 0 High = 1 Low = 2 Auto = 3 Manual = 4End EnumPrivate Enum ThermostatDeviceType Thermostat_Root = 0 Thermostat_Mode = 1 Thermostat_Setpoint = 2 Thermostat_Temperature = 3End EnumPublic Sub UpdateConfig(ByVal input As Object) Dim rootDeviceRef As Integer = CInt(input.ToString.Split("|")(0)) Dim s As New ThermostatSettings(hs, rootDeviceRef) s.Load(input.ToString.Split("|")(1)) s.Save(hs, rootDeviceRef)End SubPublic Sub Setup(ByVal inputString As Object) 'HeaterDeviceReference=860,ExternalTemperatureSensorRef=521 Dim useFahrenheit As Boolean = hs.GetINISetting("Settings", "gGlobalTempScaleF", "False", "settings.ini") Try Dim SVpair As HomeSeerAPI.VSPair Dim VGpair As HomeSeerAPI.VGPair '============= 'Root device 'Mode device '============= Dim rootRef As Integer = hs.NewDeviceRef("Thermostat mode") Dim rootDevice As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(rootRef) rootDevice.Location(hs) = scriptName rootDevice.Location2(hs) = scriptName rootDevice.Device_Type_String(hs) = ThermostatDeviceType.Thermostat_Mode.ToString rootDevice.DeviceType_Set(hs) = New HomeSeerAPI.DeviceTypeInfo rootDevice.Status_Support(hs) = True rootDevice.Can_Dim(hs) = False hs.SaveEventsDevices() rootDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.SHOW_VALUES) rootDevice.MISC_Clear(hs, HomeSeerAPI.Enums.dvMISC.STATUS_ONLY) rootDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.NO_LOG) For i As Integer = 0 To 4 Dim value As Modes = i SVpair = New HomeSeerAPI.VSPair(HomeSeerAPI.ePairStatusControl.Status) SVpair.PairType = HomeSeerAPI.VSVGPairType.SingleValue SVpair.Value = CInt(value) SVpair.Status = value.ToString hs.DeviceVSP_AddPair(rootRef, SVpair) VGpair = New HomeSeerAPI.VGPair VGpair.PairType = HomeSeerAPI.VSVGPairType.SingleValue VGpair.Set_Value = CInt(value) Select Case CInt(value) Case Is = Modes.Off VGpair.Graphic = "images/HomeSeer/contemporary/off.gif" Case Is = Modes.Manual VGpair.Graphic = "images/HomeSeer/contemporary/yellow.png" Case Is = Modes.Low VGpair.Graphic = "images/HomeSeer/contemporary/blue.png" Case Is = Modes.High VGpair.Graphic = "images/HomeSeer/contemporary/red.png" Case Is = Modes.Auto VGpair.Graphic = "images/HomeSeer/contemporary/green.png" End Select hs.DeviceVGP_AddPair(rootRef, VGpair) hs.DeviceScriptButton_AddButton(rootRef, value.ToString, CInt(value), scriptFile, "ModeButtonPress", CInt(value), 1, CInt(value) + 1, 1) Next 'Storing settings in ini file Dim s As New ThermostatSettings(inputString.ToString) s.Save(hs, rootRef) 'Setting up initial schedule hs.SaveINISetting("Schedule", "5:00", "High", scriptName & "_" & rootRef & ".ini") hs.SaveINISetting("Schedule", "8:00", "Low", scriptName & "_" & rootRef & ".ini") hs.SaveINISetting("Schedule", "13:00", "High", scriptName & "_" & rootRef & ".ini") hs.SaveINISetting("Schedule", "23:00", "Low", scriptName & "_" & rootRef & ".ini") 'Setting up initial alternative schedule hs.SaveINISetting("AlternativeSchedule", "6:00", "High", scriptName & "_" & rootRef & ".ini") hs.SaveINISetting("AlternativeSchedule", "23:00", "Low", scriptName & "_" & rootRef & ".ini") '============= 'Setpoint device '============= Dim setpointRef As Integer = hs.NewDeviceRef("Thermostat setpoint") Dim setpointDevice As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(setpointRef) setpointDevice.Location(hs) = scriptName setpointDevice.Location2(hs) = scriptName setpointDevice.Device_Type_String(hs) = ThermostatDeviceType.Thermostat_Setpoint.ToString setpointDevice.DeviceType_Set(hs) = New HomeSeerAPI.DeviceTypeInfo setpointDevice.Status_Support(hs) = True setpointDevice.Can_Dim(hs) = False hs.SaveEventsDevices() setpointDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.SHOW_VALUES) setpointDevice.MISC_Clear(hs, HomeSeerAPI.Enums.dvMISC.STATUS_ONLY) setpointDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.NO_LOG) Dim tempStart As Double = 5 Dim tempEnd As Double = 35 Dim forStep As Double = 0.5 Dim scale As String = "C" If useFahrenheit Then tempStart = 41 tempEnd = 95 forStep = 1 scale = "F" End If For i As Double = tempStart To tempEnd Step forStep SVpair = New HomeSeerAPI.VSPair(HomeSeerAPI.ePairStatusControl.Both) SVpair.PairType = HomeSeerAPI.VSVGPairType.SingleValue SVpair.Value = i SVpair.Status = i & " °" & scale SVpair.Render = HomeSeerAPI.Enums.CAPIControlType.Values SVpair.Render_Location.Row = 1 SVpair.Render_Location.Column = 1 SVpair.Render_Location.ColumnSpan = 1 hs.DeviceVSP_AddPair(setpointRef, SVpair) Next If useFahrenheit Then hs.SetDeviceValueByRef(setpointRef, 64, False) Else hs.SetDeviceValueByRef(setpointRef, 17.5, False) End If hs.DeviceScriptButton_AddButton(setpointRef, "-", 200, scriptFile, "SetpointButtonPress", ScriptParm:=200, Row:=1, Column:=2, ColumnSpan:=1) hs.DeviceScriptButton_AddButton(setpointRef, "+", 201, scriptFile, "SetpointButtonPress", ScriptParm:=201, Row:=1, Column:=3, ColumnSpan:=1) VGpair = New HomeSeerAPI.VGPair VGpair.PairType = HomeSeerAPI.VSVGPairType.Range VGpair.RangeStart = 0 VGpair.RangeEnd = 1000 VGpair.Graphic = "images/HomeSeer/contemporary/Thermometer-50.png" hs.DeviceVGP_AddPair(setpointRef, VGpair) '============= 'Temperature device '============= Dim roomTempRef As Integer = hs.NewDeviceRef("Thermostat room temp") Dim roomTempDevice As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(roomTempRef) roomTempDevice.Location(hs) = scriptName roomTempDevice.Location2(hs) = scriptName roomTempDevice.Device_Type_String(hs) = ThermostatDeviceType.Thermostat_Temperature.ToString roomTempDevice.DeviceType_Set(hs) = New HomeSeerAPI.DeviceTypeInfo roomTempDevice.Status_Support(hs) = True roomTempDevice.Can_Dim(hs) = False hs.SaveEventsDevices() roomTempDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.SHOW_VALUES) roomTempDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.STATUS_ONLY) roomTempDevice.MISC_Set(hs, HomeSeerAPI.Enums.dvMISC.NO_LOG) SVpair = New HomeSeerAPI.VSPair(HomeSeerAPI.ePairStatusControl.Status) SVpair.PairType = HomeSeerAPI.VSVGPairType.Range SVpair.RangeStart = -1000 SVpair.RangeEnd = 1000 SVpair.RangeStatusSuffix = " °" & scale SVpair.RangeStatusDecimals = 1 hs.DeviceVSP_AddPair(roomTempRef, SVpair) VGpair = New HomeSeerAPI.VGPair VGpair.PairType = HomeSeerAPI.VSVGPairType.Range VGpair.RangeStart = -1000 VGpair.RangeEnd = 1000 VGpair.Graphic = "images/HomeSeer/contemporary/Thermometer-90.png" hs.DeviceVGP_AddPair(roomTempRef, VGpair) 'Save devices hs.SaveEventsDevices() 'Associating devices rootDevice.Relationship(hs) = HomeSeerAPI.Enums.eRelationship.Parent_Root rootDevice.AssociatedDevice_Add(hs, setpointRef) rootDevice.AssociatedDevice_Add(hs, roomTempRef) setpointDevice.Relationship(hs) = HomeSeerAPI.Enums.eRelationship.Child setpointDevice.AssociatedDevice_Add(hs, rootRef) roomTempDevice.Relationship(hs) = HomeSeerAPI.Enums.eRelationship.Child roomTempDevice.AssociatedDevice_Add(hs, rootRef) 'Setting up events Dim eventRef As Integer = hs.NewEventGetRef("Thermostat " & rootRef, "Virtual Thermostat", "") hs.EventSetRecurringTrigger(eventRef, New TimeSpan(0, 5, 0), False, True) hs.AddActionRunScript(eventRef, "VirtualThermostat.vb", "CheckTemperature", rootRef) hs.WriteLog("Virtual Thermostat", "New thermostat created successfully!") Catch ex As Exception hs.WriteLog("Virtual Thermostat", "Error creating root device: " & ex.Message) End TryEnd SubPublic Sub ModeButtonPress(ByVal input As Object) Dim rootDeviceRef As Integer = input(0) Dim value As Modes = input(1) Dim s As New ThermostatSettings(hs, rootDeviceRef) 'Updating the mode device hs.SetDeviceValueByRef(rootDeviceRef, CInt(value), True) 'Updating the temperature device Select Case value Case = Modes.Off TurnOff(rootDeviceRef) Case Is = Modes.Low hs.SetDeviceValueByRef(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Setpoint), s.TemperatureLow, True) Case Is = Modes.High hs.SetDeviceValueByRef(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Setpoint), s.TemperatureHigh, True) Case Is = Modes.Auto Dim temp As Double = GetSetpointFromSchedule(rootDeviceRef) hs.SetDeviceValueByRef(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Setpoint), temp, True) End Select If value > Modes.Off Then CheckTemperature(rootDeviceRef)End SubPublic Sub SetpointButtonPress(ByVal input As Object) Dim setpointDeviceRef As Integer = input(0) Dim value As Integer = input(1) 'Get the current setpoint value Dim devValue As Double = hs.DeviceValueEx(setpointDeviceRef) 'Set the new setpoint value Select Case value Case = 200 'Down hs.SetDeviceValueByRef(setpointDeviceRef, devValue - 0.5, True) Case 201 'Up hs.SetDeviceValueByRef(setpointDeviceRef, devValue + 0.5, True) End Select 'Find the root device (the setpoint device is only associated to the root device) Dim setpointDevice As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(setpointDeviceRef) Dim rootDeviceRef As Integer = 0 If setpointDevice.AssociatedDevices_Count(hs) = 1 Then rootDeviceRef = setpointDevice.AssociatedDevices(hs)(0) End If If rootDeviceRef > 0 Then 'Set the mode to "manual" If hs.DeviceValue(rootDeviceRef) Modes.Manual Then hs.SetDeviceValueByRef(rootDeviceRef, Modes.Manual, True) 'Check the temperature and adjust the heater as needed CheckTemperature(rootDeviceRef) Else hs.WriteLog(scriptName, "Could not find root device for setpoint device") End IfEnd SubPrivate Function GetChildReference(ByVal rootDeviceRef As Integer, ByVal deviceType As ThermostatDeviceType) As Integer Dim root As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(rootDeviceRef) For Each i As Integer In root.AssociatedDevices(hs) Dim child As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(i) If child.Device_Type_String(hs).ToString = deviceType.ToString Then Return i End If Next Return 0End FunctionPrivate Sub UpdateTemperatureDevice(ByVal rootDeviceRef As Integer) Dim s As New ThermostatSettings(hs, rootDeviceRef) Dim roomTemperature As Double = Math.Round((hs.DeviceValueEx(s.ExternalTemperatureSensorRef) + s.TemperatureCorrectionAddition) * s.TemperatureCorrectionMultiplier, 1) hs.SetDeviceValueByRef(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Temperature), roomTemperature, True)End SubPublic Sub CheckTemperature(ByVal rootDeviceRef As Object) Dim s As New ThermostatSettings(hs, CInt(rootDeviceRef)) UpdateTemperatureDevice(rootDeviceRef) Dim currentMode As Modes = hs.DeviceValue(rootDeviceRef) 'If the root is Off, then If currentMode = Modes.Off Then Exit Sub 'Check mode for auto, and update the setpoint device If currentMode = Modes.Auto Then Dim scheduleTemp As Double = GetSetpointFromSchedule(rootDeviceRef) hs.SetDeviceValueByRef(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Setpoint), scheduleTemp, True) End If 'Get roomtemperature from device and setpoints Dim roomTemperature As Double = hs.DeviceValueEx(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Temperature)) Dim setPoint As Double = hs.DeviceValueEx(GetChildReference(rootDeviceRef, ThermostatDeviceType.Thermostat_Setpoint)) Dim lowSetPoint As Double = setPoint - s.TemperatureOffset 'Set the mode button to manual if the temperature is not equal to TempHigh or TempLow and the mode is not Off or Auto If currentMode Modes.Auto AndAlso currentMode Modes.Off Then If setPoint s.TemperatureHigh AndAlso setPoint s.TemperatureLow Then hs.SetDeviceValueByRef(rootDeviceRef, Modes.Manual, True) End If End If If roomTemperature TurnOn(rootDeviceRef) If s.Log Then hs.WriteLog(scriptName, "Heating ON - Temp: " & roomTemperature & "° - , Setpoint: " & setPoint & "°") ElseIf roomTemperature > lowSetPoint AndAlso roomTemperature Idle() If s.Log Then hs.WriteLog(scriptName, "Heating IDLE - Temp: " & roomTemperature & "° - , Setpoint: " & setPoint & "°") ElseIf roomTemperature > setPoint Then TurnOff(rootDeviceRef) If s.Log Then hs.WriteLog(scriptName, "Heating OFF - Temp: " & roomTemperature & "° - , Setpoint: " & setPoint & "°") Else If s.Log Then hs.WriteLog(scriptName, "Heating status unchanged - Temp: " & roomTemperature & "° - Setpoint: " & setPoint & "°") End IfEnd SubPrivate Sub TurnOff(ByVal rootDeviceRef As Object) Dim s As New ThermostatSettings(hs, rootDeviceRef) Dim cc As HomeSeerAPI.CAPI.CAPIControl = hs.CAPIGetSingleControlByUse(s.HeaterDeviceReference, HomeSeerAPI.ePairControlUse._Off) Dim ccResponse As HomeSeerAPI.CAPI.CAPIControlResponse = hs.CAPIControlHandler(cc) If s.Log Then hs.WriteLog("Virtual Thermostat", "Turn Off: " & ccResponse.ToString & " for device " & s.HeaterDeviceReference)End SubPrivate Sub TurnOn(ByVal rootDeviceRef As Integer) Dim s As New ThermostatSettings(hs, rootDeviceRef) Dim cc As HomeSeerAPI.CAPI.CAPIControl = hs.CAPIGetSingleControlByUse(s.HeaterDeviceReference, HomeSeerAPI.ePairControlUse._On) Dim ccResponse As HomeSeerAPI.CAPI.CAPIControlResponse = hs.CAPIControlHandler(cc) If s.Log Then hs.WriteLog("Virtual Thermostat", "Turn On: " & ccResponse.ToString & " for device " & s.HeaterDeviceReference)End SubPrivate Sub Idle() 'Do nothingEnd SubPrivate Function GetSetpointFromSchedule(ByVal rootDeviceRef As Integer) As Double Dim s As New ThermostatSettings(hs, rootDeviceRef) Dim schedule As System.Collections.Generic.List(Of String) Dim scheduleArray() As String If s.AlternativeScheduleDeviceReference > 0 Then If hs.IsOff(s.AlternativeScheduleDeviceReference) Then scheduleArray = hs.GetINISectionEx("Schedule", scriptName & "_" & rootDeviceRef & ".ini") Else scheduleArray = hs.GetINISectionEx("AlternativeSchedule", scriptName & "_" & rootDeviceRef & ".ini") End If Else scheduleArray = hs.GetINISectionEx("Schedule", scriptName & "_" & rootDeviceRef & ".ini") End If schedule = scheduleArray.ToList 'Failsafe if no schedule is found If schedule.Count = 0 Then Return s.TemperatureLow If Not schedule(0).StartsWith("0:00") Then schedule.Insert(0, "0:00=Low") End If schedule.Add("24:00=Low") Dim timeNow As Double = Now.Hour + Now.Minute / 60 For i As Integer = 0 To schedule.Count - 2 Dim line() As String = schedule(i).Split("=") Dim nextLine() As String = schedule(i + 1).Split("=") Dim time_this As Double = line(0).Split(":")(0) + line(0).Split(":")(1) / 60 Dim time_next As Double = nextLine(0).Split(":")(0) + nextLine(0).Split(":")(1) / 60 If timeNow >= time_this AndAlso timeNow Select Case line(1) Case = "High" Return s.TemperatureHigh Case = "Low" Return s.TemperatureLow Case Else Dim temp As Double = 0 Dim success As Boolean = Double.TryParse(line(1), temp) If Not success Then hs.WriteLog(scriptName, "Error parsing value in schedule at time: " + line(i)) Return temp End Select End If Next Return 0End FunctionClass ThermostatSettings Public Property ExternalTemperatureSensorRef As Integer = 0 Public Property TemperatureCorrectionMultiplier As Integer = 1 Public Property TemperatureCorrectionAddition As Integer = 0 Public Property TemperatureOffset As Double = 0.3 Public Property HeaterDeviceReference As Integer = 0 Public Property TemperatureHigh As Double = 23 Public Property TemperatureLow As Double = 16 Public Property AlternativeScheduleDeviceReference = 0 Public Property Log As Boolean = False Private scriptName As String = "VirtualThermostat" Public Sub Save(ByRef hs As Object, ByVal rootDeviceRef As Integer) Dim filename As String = scriptName & "_" & rootDeviceRef & ".ini" hs.SaveINISetting("Settings", "ExternalTemperatureSensorRef", Me.ExternalTemperatureSensorRef, filename) hs.SaveINISetting("Settings", "TemperatureCorrectionAddition", Me.TemperatureCorrectionAddition, filename) hs.SaveINISetting("Settings", "TemperatureCorrectionMultiplier", Me.TemperatureCorrectionMultiplier, filename) hs.SaveINISetting("Settings", "TemperatureOffset", Me.TemperatureOffset, filename) hs.SaveINISetting("Settings", "Log", Me.Log, filename) hs.SaveINISetting("Settings", "HeaterDeviceReference", Me.HeaterDeviceReference, filename) hs.SaveINISetting("Settings", "TemperatureHigh", Me.TemperatureHigh, filename) hs.SaveINISetting("Settings", "TemperatureLow", Me.TemperatureLow, filename) hs.SaveINISetting("Settings", "AlternativeScheduleDeviceReference", Me.AlternativeScheduleDeviceReference, filename) End Sub Public Sub New(ByRef hs As Object, ByVal rootDeviceRef As Integer) Dim lines() As String = hs.GetINISectionEx("Settings", scriptName & "_" & rootDeviceRef & ".ini") Dim inputString = String.Join(",", lines) 'Dim rootDevice As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(rootDeviceRef) 'Dim inputString As String = rootDevice.DeviceType_Get(hs).Device_SubType_Description.ToString Load(inputString) End Sub Public Sub New(ByVal inputString As String) Load(inputString) End Sub Public Sub Load(ByVal inputString As String) Dim lines() As String = inputString.Split(",") For Each line As String In lines Dim properties() As String = line.Split("=") Select Case properties(0) Case = "ExternalTemperatureSensorRef" Me.ExternalTemperatureSensorRef = CInt(properties(1)) Case = "TemperatureCorrectionAddition" Me.TemperatureCorrectionAddition = CInt(properties(1)) Case = "TemperatureCorrectionMultiplier" Me.TemperatureCorrectionMultiplier = CInt(properties(1)) Case = "TemperatureOffset" Me.TemperatureOffset = CInt(properties(1)) Case = "Log" Me.Log = CBool(properties(1)) Case = "HeaterDeviceReference" Me.HeaterDeviceReference = CInt(properties(1)) Case = "TemperatureHigh" Me.TemperatureHigh = properties(1) Case = "TemperatureLow" Me.TemperatureLow = properties(1) Case = "AlternativeScheduleDeviceReference" Me.AlternativeScheduleDeviceReference = properties(1) End Select Next 'HeaterDeviceReference=860,ExernalTemperatureSensorRef=521 End SubEnd Class
config.json for termostaten slik:
{ "ref": "7569", "type": "Thermostat", "name": "Badet Termostat", "controlRef": "7567", "stateRef": "7567", "temperatureUnit":"C", "humidityRef": "362", "heatingSetpointRef": "7568", "coolingSetpointRef": "7568", "uuid_base": "Thermostat7569" },
Og config.ini-fila slik(med HeaterDeviceReference=6936,ExternalTemperatureSensorRef=6913, samt at 467 er fridagscriptet ditt)
[settings]ExternalTemperatureSensorRef=6913TemperatureCorrectionAddition=0TemperatureCorrectionMultiplier=1TemperatureOffset=0,3Log=FalseHeaterDeviceReference=6936TemperatureHigh=23TemperatureLow=16AlternativeScheduleDeviceReference=467[schedule]5:00=High8:00=Low13:00=High23:00=Low[AlternativeSchedule]5:30=High23:00=Low
Kjøler ned ser litt dumt ut, om jeg dropper den så står det -10 grader, så det får bare være.