Home Assistant and KE100 Valves (+ ChatGPT)
Fairly recently bought my Kasa valves and mostly impressed with them.
I can see them and control them directly from within my Home Assistant but it's a fair bit of micro management to get the temperature to be what I want the room to be as the temperature readings are taken by the valves themselves which are heated by the fact of being close to the heat source. You can get around this by using a Tapo temperature sensor (which came free with the kit) but that's just one room and extras are £16 each.
Like any Home Assistant user I already have a large number ot temperature sensors around the house feeding back info to the system, so why can't I use them? Right, it should be easy?
I have virtually no idea about yaml files but yesterday I was messing with ChatGPT and thought why can that not help?
So I started a new convo and typed "write a home assistant routine to adjust kasa thermostats based on chosen temperature" thinking it would never work but might be good for a punt. And it kicked out some code that looked like yaml and suggested some enhancements 'such as dynamic heating/cooling mode switching'.
OK I'll bite 'add dynamic heating/cooling mode switching' and more code came out with more suggestions like 'Hysteresis Logic – Prevent frequent mode switching by adding a buffer (e.g., switch only if the temperature difference is >1°C)' ... and more code came out.
I then tell it the entity name for my valve and my temp sensor and it modifies the code again. It also fixed the code for Heat-Only Smart Valves, I don't think I asked for that.
It then talked me through how to setup a helper to pick a target temperature and a slider to set the helper value on my dashboard. There was then an issue with the valves just turning off, and it fixed the code again.
Eventually I ended up with this:
alias: Adjust Smart Valve Thermostat with Hysteresis
description: "Dynamically adjusts the Smart Valve thermostat with a buffer to prevent frequent switching."
trigger:
- platform: state
entity_id: input_number.target_temperature
- platform: state
entity_id: sensor.ble_temperature_bathroom_temperature
condition: []
action:
- variables:
target_temp: "{{ states('input_number.target_temperature') | float }}"
current_temp: "{{ states('sensor.ble_temperature_bathroom_temperature') | float }}"
hysteresis: 0.5 # Adjust buffer zone
- choose:
- conditions:
- condition: template
value_template: "{{ current_temp < (target_temp - hysteresis) }}"
sequence:
- service: climate.set_hvac_mode
target:
entity_id: climate.smart_valve_03
data:
hvac_mode: "heat"
- service: climate.set_temperature
target:
entity_id: climate.smart_valve_03
data:
temperature: "{{ target_temp }}"
- conditions:
- condition: template
value_template: "{{ current_temp > (target_temp + 1.5) }}" # Increased threshold
sequence:
- service: climate.set_hvac_mode
target:
entity_id: climate.smart_valve_03
data:
hvac_mode: "off" # Turns off only if significantly warmer
mode: restart
I am sure there are better options out there but I was so pleased that this actually worked I figured I had to share it here.
(I assume I am OK posting code here? And why no Corier font?)