Hi all!
I sat back down to convert a lot of my automations to AppDaemon. I really like sliders, I think they are a neat UI element that can help simplify and elevate your cards/views for the average user that might be unfamiliar with your setup. At the end of the day, I like the build things that are both practical and intuitive.
Code below, but also can be found here, if you prefer to see the syntax highlighted.
import appdaemon.appapi as appapi
class SliderTemperature(appapi.AppDaemon):
def initialize(self):
current_temp = int(self.get_state('climate.hallway_hallway', attribute='current_temperature'))
self.call_service('input_slider/select_value',
entity_id='input_slider.target_temp', value=current_temp)
self.listen_state(self.adjust_temperature, 'input_slider.target_temp')
def adjust_temperature(self, entity, attribute, old, new, kwargs):
operation_mode = self.get_state('climate.hallway_hallway')
current_temp = self.get_state('climate.hallway_hallway', attribute='current_temperature')
# Nest thermo has 4 modes: heat, cool, auto (range), and eco (away)
if operation_mode == 'auto':
current_temp, new = int(float(current_temp)), int(float(new))
if current_temp < new:
lower, upper = new, new+3
else:
lower, upper = new-3, new
self.call_service('climate/set_temperature',
target_temp_low=lower,
target_temp_high=upper)
elif operation_mode == 'eco':
self.error('Setting the temperature is not allowed when no one is home!')
else:
self.call_service('climate/set_temperature',
temperature=int(new))
Most of this should be pretty self-explanatory, but letâs go over it briefly anyway! I like to group my settings of objects with the code that they will be affecting, so instead of having a general startup file that sets a bunch of âdefaultsâ on the UI elements and variables, I put them in the AppDaemon appâs initialize
function.
We start off by setting the value of the slider to the current_temp
and then setting a state listener to keep tabs on the target_temp slider. Since we want to adjust the temperature every time the slider is changed, we aptly name our callback function adjust_temperature
. Remember people, Python should be beautiful!
On line 13 and 14, youâll see we set two variables to grab out operation_mode
and current_temp
. The thermostat I am using, Nest, has 4 different modes to it, and so weâll want to account for all of these in our code to cover all different scenarios. Since the slider leaves us with a final or new
value, operation_mode == heat
and operation_mode == cool
are effectively the same type of mode. We receive input, and set the temperature to that input. Youâll find that that code is very simple to write, and is lines 31 and 32. When we are in operation_mode == eco
, we donât, or more accurately canât set the temperature, so weâll raise an error and do nothing else.
The difficult part comes into play when we have operation_mode == auto
, or the thermostat is told to stay within a specific range. Nest requires the temperature setting to stay at least 3° apart from each other. The input_slider
can only specify one value, and until it can have two controls on it, weâll have to jimmy-rig this automation up.
To solve this problem, I first asked myself âwhen will I manually change the thermostat?â and to that I simply answered âwhen Iâm too cold, and want to turn the heat onâ. So from this interaction, we know two bits of information.
- The current temperature --> âItâs too coldâ
- The target temperature direction --> âI want to turn the heat onâ
So someone will go to turn the slider up past the current temperature. Then we want the lowest temperature to be that slider value, and the highest temperature to be at least 3 higher than the slider value. The converse can be said about the opposite situation â and this is exactly what we are doing in lines 20 - 23. The rest is simply calling the climate/set_temperature
service to do what we want, and youâve got your automation!
This automation is still pretty simple, but I found myself wanting to change the temperature quickly from my dashboard without a way to do it. Iâm positive this code can be used as a base for other thermostats as well, feel free to comment below and Iâll do my best to help. More to come!
Thanks for reading,
SupahNoob