This python script (using the python.scripts integration of HA) takes a temperature schedule and finds the ramp thermostat temperature at any given time to be fed into an existing climate erntity as the new target temperature.
This can be useful to make thermostat actions to take into account the fact that on a cold day the climate entity will start heating earlier, and vice versa, on a warm day start heating later.
Now the schedule of time/temperatures is in the script, I tried various ways to get the schedule to be read from an external file, but seem to run in to a limitation of python scripts to import files.
Any suggestion how to make this happen: import the schedule file such as
Bathroom temperature schedule.txt
“00:00”, 16
“06:00”, 16
“08:00”, 19
“10:00”, 19
“10:01”, 16
“20:00”, 16
“23:00”, 19
“23:59”, 19
Or use a schedule file from the scheduler integration(?)
Using the services.yaml file to enter the schedule with an example(?)
Or use any other user friendly way to establish the schedule file without the need for the user to modify into the python script.
Any help wil be appreciated.`
#a script for home assistant to compute ramp thermostat temperatures to be fed into a climate entity
# Define the time and temperature schedule
schedule = [("00:00", 14), ("03:00", 14), ("08:00", 19), ("10:00", 19), ("10:01", 16), ("20:00", 16), ("23:00", 19), ("23:59", 19)]
#get the climate entity
climate_entity = data.get('climate_entity') #'climate.badkamer'# setto your climate entity
# Initialize variables
current_temp = hass.states.get(climate_entity).attributes['current_temperature']
# Get the current time of Hass
now = datetime.datetime.now()
# Calculate the time in minutes since midnight
current_time_in_minutes = now.hour * 60 + now.minute
# Convert the time points in the schedule to minutes since midnight
schedule_minutes = []
for time, temp in schedule:
hh, mm = time.split(":")
schedule_minutes.append((int(hh) * 60 + int(mm) , temp))
# Find the ramp temperature at the current time
ramp_temp = None
for i in range(1, len(schedule_minutes)):
prev_time, prev_temp = schedule_minutes[i-1]
next_time, next_temp = schedule_minutes[i]
if current_time_in_minutes >= prev_time and current_time_in_minutes < next_time:
time_diff = next_time - prev_time
temp_diff = next_temp - prev_temp
time_elapsed = current_time_in_minutes - prev_time
ramp_temp = prev_temp + temp_diff * (time_elapsed / time_diff)
break
# If a ramp temperature was found, use it, otherwise use the current temperature
if ramp_temp is not None:
final_temp = ramp_temp
else:
final_temp = current_temp
#write the final_temp ramp temperature for debug purposes
hass.states.set('sensor.ramp_temperature', final_temp)
#set the final_temp as the new target temperature in the climate entity
hass.services.call('climate', 'set_temperature', {'entity_id': climate_entity, 'temperature': final_temp})
If you want to use the script as is:
- store the script in the directory python_scripts
- name of the script e.g.: ramp.py
3 add a services.yaml file
ramp:
description: Ramp thermostat script
fields:
climate_entity:
description: The entity ID of the climate entity
example: climate.bathroom
required: true
- create an automation to call the script every minute, such as:
alias: Bathroom Ramp themostaat
description: ""
trigger:
- platform: time_pattern
minutes: /1
action:
- service: python_script.rampie
data:
climate_entity: climate.bathroom
mode: single