Thermostat planning

From Domoticz
Jump to navigation Jump to search

Purpose

This script simulate a thermostat, using weekly planning and presence.

Dependencies

Thermostat & radiator to get T° and have some actions to do. Virtual devices to define outside the script different informations.

Installation instructions

Configuration

It is actually as french at the beginning of the file, so a more/less english version.

consigne_presence

You need to define the name of the switch which define the highest temperature you want inside. It refer to a virtual Thermostat.

consigne_absence

Same as above but it is the lowest temp.

temperature_consigne_idx

It refere to the idx in domoticx for a virtual device which show the temperature you are looking for

hysteresis

It is the temperature 'time' beetween the stat the radiators will not switch states

sonde

The name of the temp hardware (your oregon in most of case)

thermostat

Name of the virtual thermostat

thermostat

IDX in domoticz of your thermostat

thermostat_duree_retour_defaut

How many hours the thermostat will change from 'present' states to 'defaut'

localisation

Name of virtual device to know where you are

localisation_value

Number of the switch

liste_radiateur

Name of radiators, as a list

url

Url of your domoticz server

planning

Work as a week & hours planning. Basicly you have 3 states.

0 = radiators will never work

1 = radiators will work if you are inside the house

2 = radiators will work

You can easily add new states

Create virtual device

The name, are the one i'm using and refere to the configuration, you can use the name you want until you change it in the configuration.

Thermostat

It is a light/switch - selector switch or equivalent with Off/Defaut/Absent/Present.

'Off' : will not change any state

'Defaut' : Use planning & GPS location

'Absent' : Use the minimal temperature, don't use planning

'Present' : Use the maximal temperature, don't use planning, switch to 'Defaut' after number of hours define in thermostat_duree_retour_defaut


Consigne absent

It is a Thermostat - Setpoint.

It define the minimal temperature. For example, it is freezing at 0°C outside, you are not at home but you don't want your home to freeze, so if you define it to 10°C, it will keep home above 10°C

Consigne present

It is a Thermostat - SetPoint

It define the temperature you want at home when you are in (19°C ?)

Temperature consigne

It is a Temp - LaCrosse TX3 or same.

It's more/less to get a view on domoticz and log for grafana. The script update it.

Localisation

It is a Light/Switch - Selector Switch - Off/Local(value = 20)

It is update, in my case, by the domoticz android app

Design rationale

You define a work to do with planning which define the T° you want to have, lowest or normal T°.

Lua script

--[[
 --Simulation d un thermostat avec planning
--Virtual devices :
--'thermostat' - Light/Switch - Selector Switch - Off/Defaut/Absent/Present
--'consigne absent' - Thermostat - SetPoint - 10C
--'consigne present' - Thermostat - SetPoint - 18.5C
--'Temperature consigne' - Temp - LaCrosse TX3
--'Localisation' - Light/Switch - Selector Switch - Off/Local (change with domoticz)
--------------------------------
--Variable a definir
--------------------------------
local consigne_presence = otherdevices_svalues['Consigne present'] --Temperature de fonctionnement si prsent
local consigne_absence = otherdevices_svalues['Consigne absent'] --Temperature de fonctionnement si personne
local temperature_consigne_idx = 10 --IDX pour l affichage de la temperature de consigne
local hysteresis = 0.2 --Valeur seuil pour éviter que le relai ne cesse de commuter dans les 2 sens
local sonde = 'Temperature salon' --Nom de la sonde de température
local thermostat = 'Thermostat salon' --Nom de l'interrupteur virtuel du thermostat
local thermostat_idx = 8 --IDX thermostat
local thermostat_duree_retour_defaut = 2 --nombre d heure avant retour en mode defaut sur un lancement force
local localisation = 'Localisation'     -- interrupteur de gestion de localisation
local localisation_value = 20 --valeur de l appartement
local liste_radiateur =
{--liste des radiateurs a commuter
'Radiateur salon',
'Radiateur escalier',
'Radiateur SdB'
}
local url = 'http://127.0.0.1:8080' --url du serveur
local planning =
{
--0 on ne chauffe jamais
--1 on chauffe en fonction du GPS
--2 on chauffe toujours
--          00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Monday   = { 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1},
Tuesday  = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Wednesday= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1},
Thursday = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Friday   = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1},
Saturday = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Sunday   = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1}
}
--------------------------------
-- Fin des variables a definir
--------------------------------
--------------------------------

local consigne = 0 --temperature de fonctionnement
local log = os.date('%A') .. ' ' .. os.date('%H') .. ':' .. os.date('%M') .. ' '
local presence = true --si true, on chauffe, sinon test d'exclusion
local planning_etat = planning[os.date('%A')][tonumber(os.date('%H'))+1]

function Commande_radiateur(radiateur, commande)
        for key,value in pairs(radiateur) do
                commandArray[value]=commande
        end
end

commandArray = {}

--La sonde Oregon defini l appel
if (devicechanged[sonde]) and (otherdevices[thermostat]~='Off') then
        local temperature = devicechanged[string.format('%s_Temperature', sonde)] --Temperature relevée dans le salon

        --on est absent, consigne_absence
        if (otherdevices[thermostat]=='Absent') then
                log = log .. 'Consigne thermostat = Absent force - '
                presence = false
        end

        --on est present, consigne_presence, on regule via le script de timing
        if (otherdevices[thermostat]=='Present') then
                log = log .. 'Consigne thermostat = Presence force - '
                presence = true
        end

        --programmation 'defaut' en fonction des jours d'exclusion
        if (otherdevices[thermostat]=='Defaut') then
                log = log .. 'Consigne thermostat = Defaut - '

                if (planning_etat == 0) then --on chauffe pas
                        log = log .. 'planning_etat = 0 '
                        presence = false
                elseif (planning_etat == 1) then --on chauffe en fonction du GPS
                        log = log .. 'planning_etat = 1 '
                        if (otherdevices_svalues[localisation] == localisation_value) then
                                log = log .. 'gps == sur place '
                                presence = true
                        else
                                log = log .. 'gps != sur place '
                        else
                                log = log .. 'gps != sur place '
                                presence = false
                        end
                elseif (planning_etat == 2) then --on force le chauffage
                        log = log .. 'planning_etat = 2 '
                        presence = true
                end
        end

        --on definit la temperature de fonctionnement
        if (presence == true) then
                consigne = consigne_presence
        else
                consigne = consigne_absence
        end
        commandArray['OpenURL']= url .. '/json.htm?type=command&param=udevice&idx=' .. temperature_consigne_idx  ..'&svalue=' .. consigne

        --on gere l'allumage du chauffage
        if (temperature <= (consigne - hysteresis) ) then
                log = log .. 'Allumage du chauffage - '
                Commande_radiateur(liste_radiateur, 'Off')
        elseif (temperature >= (consigne + hysteresis)) then
                log = log .. 'Extinction du chauffage - '
                Commande_radiateur(liste_radiateur, 'On')
        elseif (temperature >= (consigne - hysteresis)) and (temperature <= (consigne + hysteresis)) then
                log = log .. 'Entre 2 (hyerisis) - '
        end

        --on verifie si <n est a x heure de plus en present
        if (otherdevices[thermostat] == 'Present') then
                local last_update = otherdevices_lastupdate[thermostat]
                local heure_lastupdate = string.sub(last_update, 12, 13)
                local minute_lastupdate = string.sub(last_update, 15, 16)
                local heure = os.date('%H')
                local minute = os.date('%M')

                if (heure_lastupdate * 60 + minute_lastupdate < heure * 60 + minute + thermostat_duree_retour_defaut*60) then
                        commandArray['OpenURL']= url .. '/json.htm?type=command&param=udevice&idx=' .. thermostat_idx .. '&svalue=10'
                        log = log .. 'Changement du thermostat du salon en defaut'
                end
        end

        --on ecrit une ligne de log unique
        print (log .. 'Consigne = ' .. consigne .. ' - Temperature salon = ' .. temperature)
end
return commandArray

Todo

This describe may be a little be too short, will try to work on it. Next should be to optimize the start of radiators to get the perfect T° depending on outside T° and time to go on. I open a forum topic to exchange about it. Feel free to comment and ask.

http://domoticz.com/forum/viewtopic.php?f=61&t=20620&p=159492#p159492

Froggy / FroggyCorp.