Create a timer for climate systems on Home Assistant

6 minutes of reading
PURPOSES OF PROJET:
  • Creation a timer to plan the activation of a thermostat integrated with Home Assistant
  • Difficulty level: medium low
  • Cost: null
CONCEPTS FACED:
  • Software configuration
SOFTWARE COMPONENTS USED:
Prerequisites:
PHYSICAL DEVICES USED:
GUIDE more indicated for:

All environments

Notes and disclaimer
  • qualsiasi eventuale modifica agli impianti domestici dev'essere progettata ed realizzata SOLO da personale qualificato;
  • any changes implemented in probefore is a propersonal responsibility as well as a profirst risk and danger (the contents of the present page are purely educational);
  • any changes implemented in proprior to a device it voids the guarantee, quality approvals and certifications.
Driving review: 1.1

Abstract

One of the biggest and most obvious limitations of the component "Climate"- to which all the dedicated platforms belong to the integration of thermostats and more generically of climate control systems available on Home Assistant - is the absence of a scheduler, or of a timer which allows the entity of this type (both dedicated to the management of heating and cooling) to switch on / off not only in relation to temperature, but also to specifications daily / weekly time slots.

Front thermostat
a thermostat-timer.

Waiting for that Home Assistant it evolved in this sense proWe will see to it through this procasting, implementing the component "Python Scripts“, Which generates one or more entity service type from scripts written in language Python. Such scripts can customize the behavior of Home Assistant: in this case we will exploit them, appanointed, to create a timer.

To activate the entity "Climate"The script proplace in the projet uses a simple logic, namely that of varying the "target" temperature of the thermostat based on the day and time. The behavior of the thermostat, as the target temperature varies, will obviously be the ordinary one (it activates the system based on the comparison with the room temperature).

We will therefore also create two entities "Input Number"Suitable for the" collection "of two" target "temperatures:

  • for plants of heating, That high (ordinary heating target) and the threshold one low (temperature minima to always guarantee, regardless - sometimes known as "ECO temperature");
  • for plants of cooling (conditioners), the threshold one high (temperature maxim to be guaranteed always, regardless) and that low (ordinary cooling target).

Python Scripts

As anticipated, the component "Python Scripts”Serves to define the entity that, if evoked (as a service), execute scripts written in Python.

To activate this component you need to create a folder called:

python_scripts /

inside the main folder of Home Assistant (on Raspbian, "/home/homeassistant/.homeassistant"); later, change the configuration by entering:

python_scripts:

and finally restart Home Assistant. The restart must also be performed to any script addition (in the form of a text file with the extension ".py") inside the above folder; on reboot, a new entity called "python_scripts.nomedelfile” appwill appear in the list of services present on Home Assistant.

assumptions

We will take for granted the existence of a type entity "Climate"Call"climate.termostato“, As well as the presence of an environmental temperature reading, to be chosen from:

  • the attribute "temperature"Of the entity"climate.termostato";
  • a sensor "sensor.temperature"

Some component integration platforms "Climate", In fact, generate entities without the attribute"temperature“, Which obliges us to register the data directly from another entity.

Target temperatures

We define in configuration two "Input Number"In order to collect the two target temperatures from frontend:

input_number:
  temp_high:
    name: Target alto
    min: 0
    max: 30
    step: 1
  temp_low:
    name: Target basso
    min: 0
    max: 30
    step: 1

The two entities ("input_number.temp_high" and "input_number.temp_low") Will be used by the Python script to evaluate how to dynamically adjust the entity"climate.termostato"

Script

After activating the component "Python Script", We create a new file in the script folder called"scheduler.py”Containing the following code:

TEMP_HIGH = hass.states.get('input_number.temp_high')
TEMP_LOW = hass.states.get('input_number.temp_low')
climate_entity = 'climate.termostato' # definisce l'entità "Climate" da controllare
current_temp = hass.states.get(climate_entity).attributes['temperature']
# current_temp =  float(hass.states.get('sensor.temperature'))

WEEK_SCHEDULE = [
    [datetime.time( 0, 0), datetime.time( 1, 0)],     # dalle 00:00 alle 01:00
    [datetime.time( 7, 0), datetime.time( 9, 0)],     # dalle 07:00 alle 09:00
    [datetime.time(18, 0), datetime.time(23, 59, 59)] # dalle 18:00 alle 23:59
]
WEEKEND_SCHEDULE = [
    [datetime.time( 0, 0), datetime.time( 2, 0)],     # dalle 00:00 alle 01:00
    [datetime.time(10, 0), datetime.time(13, 0)],     # dalle 10:00 alle 13:00
    [datetime.time(18, 0), datetime.time(23, 59, 59)] # dalle 18:00 alle 23:59
]

now = datetime.datetime.now().time()
if datetime.datetime.now().date().weekday() < 5:
    current_schedule = WEEK_SCHEDULE
else:
    current_schedule = WEEKEND_SCHEDULE

in_high_time = False
for high_time in current_schedule:
    start = high_time[0]
    end = high_time[1]
    
    if start <= now <= end:        
        in_high_time = True
        break

new_temp = TEMP_HIGH if in_high_time else TEMP_LOW 
message = "Temperatura impostata a {} (era {})".format(new_temp.state, current_temp)
if str(new_temp.state) != str(current_temp):
    # imposta la nuova temperatura sul termostato
    hass.services.call('climate', 'set_temperature', {'entity_id': climate_entity, 'temperature': float(new_temp.state)})
    # invio inoltre una notifica di sistema
    hass.services.call('notify','notifications_send', {'title' : 'HA: Variazione al termostato', 'message': message})

Remember to restart Home Assistant to generate the entity "python_script.scheduler"
Restart Home Assitant.

Analysis

Initial block
TEMP_HIGH = hass.states.get('input_number.temp_high')
TEMP_LOW = hass.states.get('input_number.temp_low')
climate_entity = 'climate.termostato' # definisce l'entità "Climate" da controllare
current_temp = hass.states.get(climate_entity).attributes['temperature']
# current_temp = float(hass.states.get('sensor.temperature'))

WEEK_SCHEDULE = [
    [datetime.time( 0, 0), datetime.time( 1, 0)],     # dalle 00:00 alle 01:00
    [datetime.time( 7, 0), datetime.time( 9, 0)],     # dalle 07:00 alle 09:00
    [datetime.time(18, 0), datetime.time(23, 59, 59)] # dalle 18:00 alle 23:59
]
WEEKEND_SCHEDULE = [
    [datetime.time( 0, 0), datetime.time( 2, 0)],     # dalle 00:00 alle 01:00
    [datetime.time(10, 0), datetime.time(13, 0)],     # dalle 10:00 alle 13:00
    [datetime.time(18, 0), datetime.time(23, 59, 59)] # dalle 18:00 alle 23:59
]

In this first part we note the collection of the values ​​of the two "Input Number" ("input_number.temp_high" and "input_number.temp_low") In two ad hoc variables ("temp_high" and "temp_low") And two other variables, one containing the entity_id to check ("climate_entity", In this case set to"climate.termostato") and the current ambient temperature ("current_temp") Collected from the sensor status"sensor.temperature"

The commented line:

# current_temp = float (hass.states.get ('sensor.temperature'))

is useful for those entities "Climate" that do not expose a real datum of the environmental temperature, for example those prolearned through integration of the “Eqiva eQ-3” thermostatic head. In these cases - since the temperature reading value it is essential for the functionnameof the timer - it will suffice eliminate the comment # (and comment on the previous one) to make the variable ("current_temp") Is valued via the state value of a temperature sensor present in the configuration (here assumed as "sensor.temperature“).

Finally, the code block defines the weekly switch-on times ("WEEK_SCHEDULE") And at the weekend ("WEEKEND_SCHEDULE“).


Obviously this block it is widely customizable on the basis of proyour needs (especially the field "climate_entity“).

OPERATIONAL BLOCK

It is in the second part that all the magic happens:

now = datetime.datetime.now().time()
if datetime.datetime.now().date().weekday() < 5:
    current_schedule = WEEK_SCHEDULE
else:
    current_schedule = WEEKEND_SCHEDULE

in_high_time = False
for high_time in current_schedule:
    start = high_time[0]
    end = high_time[1]
    
    if start <= now <= end:        
        in_high_time = True
        break


new_temp = TEMP_HIGH if in_high_time else TEMP_LOW 
message = "Temperatura impostata a {} (era {})".format(new_temp.state, current_temp)
if str(new_temp.state) != str(current_temp):
    # imposta la nuova temperatura sul termostato
    hass.services.call('climate', 'set_temperature', {'entity_id': climate_entity, 'temperature': float(new_temp.state)})
    # invio inoltre una notifica di sistema
    hass.services.call('notify','notifications_send', {'title' : 'HA: Variazione al termostato', 'message': message}

The script first determines whether it is a weekly or weekend day; then check that you are in one of the time slots set by the user: if you are in an ignition band, set the value of the entity's target temperature "Climate"("climate.termostato") At the temperature"temp_high", Otherwise set"temp_low"

Obviously this is valid for a heating system. In the presence of plants of cooling, change the line:

new_temp = TEMP_HIGH if in_high_time else TEMP_LOW

in:

new_temp = TEMP_LOW if in_high_time else TEMP_HIGH

Automation

At this point you need to make the script run cyclically.
We therefore define automation of this type:

automation:
  - alias: 'Temporizzatore'
    initial_state: True
    trigger:
      - platform: time      
        minutes: '/1' # ogni minuto
        seconds: 00
      - platform: homeassistant
        event: start
    condition: []
    action:
      service: python_script.scheduler

In this way, automation will evoke the script once a minute (this is also customizable), so as to act on the thermostat based on the rules set.

The field "condition”Was left intentionally empty; an additional customization could be to verify (using the component)Device Tracker") the actual presence of tenants at home, so as not to act on the climate system in the absence of the latter.

final Thoughts

This type of implementation fits very well with projets e guide previously published, Including:


This projet is inspired to the work of Abílio Costa to which the gratitude of our community goes.

Domotise independent heating via clean contact e Home Assistant (without physical thermostat)


Home Assistant Official LogoATTENZIONE: remember that there is on our FORUM community an ad hoc section dedicated to Home Assistant, for any doubt, question, information on the specific merit of these components.


telegram

Stay up to date through ours Telegram channel!