Skip to content

Commit 5b7f445

Browse files
committed
update(smart_thermostat): expose control_output with Autotune.
1 parent f99d71c commit 5b7f445

5 files changed

Lines changed: 138 additions & 181 deletions

File tree

README.md

Lines changed: 131 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,135 @@
1-
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs)
1+
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge)](
2+
https://github.com/custom-components/hacs)
23

3-
# custom_components
4-
missing feature test repository
4+
# HASmartThermostat
5+
## Smart Thermostat with PID controller for Home Assistant
6+
Create a virtual thermostat with accurate and reactive temperature control through PID controller.
7+
[Principle of the PID controller.](https://en.wikipedia.org/wiki/PID_controller)
8+
Any heater or air conditioner unit with ON/OFF switch or pilot wire can be controlled using a pulse width modulation
9+
that depends on the temperature error and its variation over time.
510

6-
## PID controller thermostat
11+
![](climate_chart.png?raw=true)
712

8-
### Installation:
13+
## Installation:
14+
I recommend using HACS for easier installation.
15+
16+
### Install using HACS:
17+
Go to HACS, select Integrations, click the three dots menu and select
18+
"Custom repositories". Add the path to the Github repository in the first field, select Integration as category and
19+
click Add. Once back in integrations panel, click the Install button on the Smart Thermostat PID card to install.
20+
Set up the smart thermostat and have fun.
21+
22+
### Manual installation:
923
1. Go to <conf-dir> default /homeassistant/.homeassistant/ (it's where your configuration.yaml is)
1024
2. Create <conf-dir>/custom_components/ directory if it does not already exist
11-
3. Clone this repository content into <conf-dir>/custom_components/
25+
3. Copy the smart_thermostat folder into <conf-dir>/custom_components/
1226
4. Set up the smart_thermostat and have fun
1327

14-
### Usage:
15-
pid controller will be called periodically.
16-
If no pwm interval is definde. It will set the state of "heater" 0-"difference"
17-
Else it will turn off and on the heater proportionally.
18-
19-
#### Autotune:
20-
You can use the autotune feature to set the PID parameters.
21-
22-
Issue! I'couldn't yet save the settings to the configuration.yaml file.
23-
The PID parmaters set by the autotune won't be stored and the process restarts everytime hassio is restarted.
24-
To save the parameters read log, it will be shown like this:
25-
LOGGER.info("Set Kp, Ki, Kd. Smart thermostat now runs on PID Controller." self.kp , self.ki, self.kd)
26-
27-
### Parameters:
28-
29-
#### Still same:
30-
31-
* name (Required): Name of thermostat
32-
* heater (Required): entity_id for heater switch, must be a toggle device. Becomes air conditioning switch when ac_mode is set to True
33-
* target_sensor (Required): entity_id for a temperature sensor, target_sensor.state must be temperature.
34-
* min_temp (Optional): Set minimum set point available (default: 7)
35-
* max_temp (Optional): Set maximum set point available (default: 35)
36-
* target_temp (Optional): Set initial target temperature. Failure to set this variable will result in target temperature being set to null on startup. As of version 0.59, it will retain the target temperature set before restart if available.
37-
* ac_mode (Optional): Set the switch specified in the heater option to be treated as a cooling device instead of a heating device.
38-
* initial_operation_mode (Optional): Set the initial operation mode. Valid values are off or auto. Value has to be double quoted. If this parameter is not set, it is preferable to set a keep_alive value. This is helpful to align any discrepancies between generic_thermostat and heater state.
39-
* away_temp (Optional): Set the temperature used by “away_mode”. If this is not specified, away_mode feature will not get activated.
28+
## Configuration:
29+
The smart thermostat can be added to Home Assistant after installation by adding a climate section to your
30+
configuration.yaml file.
4031

41-
#### Removed:
42-
43-
* min_cycle_duration (Optional):
44-
* cold_tolerance (Optional):
45-
* hot_tolerance (Optional):
46-
47-
#### Edited:
48-
49-
* keep_alive (Required): Set a keep-alive interval. Interval pid controller will be updated.
50-
51-
#### New:
32+
### Configuration example:
33+
#### configuration.yaml
34+
```
35+
climate:
36+
- platform: smart_thermostat
37+
name: Smart Thermostat Example
38+
heater: switch.on_off_heater
39+
target_sensor: sensor.ambient_temperature
40+
min_temp: 7
41+
max_temp: 28
42+
ac_mode: False
43+
target_temp: 19
44+
keep_alive:
45+
seconds: 60
46+
away_temp: 14
47+
kp : 75
48+
ki : 0.001
49+
kd : 70000
50+
pwm : 00:15:00
51+
```
5252

53-
* kp (Optional): Set PID parameter, p controll value.
54-
* ki (Optional): Set PID parameter, i controll value.
55-
* kd (Optional): Set PID parameter, d controll value.
56-
* pwm (Optional): Set period time for pwm signal in minutes. If it's not set pwm is disabled.
57-
* autotune (Optional): Chose a string for autotune settings. If it's not set autotune is disabled.
58-
tuning_rules
53+
## Usage:
54+
The target sensor measures the ambient temperature while the heater switch controls an ON/OFF heating system.
55+
The PID controller computes the amount of time the heater should remain ON over the PWM period to reach the temperature
56+
set point, in example with PWM set to 15 minutes, if output is 100% the heater will be kept on for the next 15 minutes
57+
PWM period. If PID output is 33%, the heater will be switched ON for 5 minutes only.
58+
59+
By default, the PID controller will be called each time the target sensor is updated. When using main powered sensor
60+
with high sampling rate, the _sampling_period_ parameter should be used to slow down the PID controller refresh rate.
61+
62+
By adjusting the Kp, Ki and Kd gains, you can tune the system response to your liking. You can find many tutorials for
63+
guidance on the web. Here are a few useful links:
64+
* [PID Control made easy](https://www.eurotherm.com/temperature-control/pid-control-made-easy/)
65+
* [Practical PID Process Dynamics with Proportional Pressure Controllers](
66+
https://clippard.com/cms/wiki/practical-pid-process-dynamics-proportional-pressure-controllers)
67+
* [PID Tuner](https://pidtuner.com/)
68+
* [PID development blog from Brett Beauregard](http://brettbeauregard.com/blog/category/pid/)
69+
* [PID controller explained](https://controlguru.com/table-of-contents/)
70+
71+
To make it quick and simple:
72+
* Kp gain adjusts the proportional part of the error compensation. Higher values means
73+
stronger reaction to error. Increase the value for faster rise time.
74+
* Ki gain adjusts the integral part. Integral compensates the residual error when temperature settles in a cumulative
75+
way. The longer the temperature remains below the set point, the higher the integral compensation will be. If your
76+
system settles below the set point, increase the Ki value. If it overshoots the set point, decrease the Ki value.
77+
* Kd gain adjusts the derivative part of the compensation. Derivative compensates the inertia of the system. If the
78+
sensor temperature increases quickly between two samples, the PID will decrease the PWM level accordingly to limit the
79+
overshoot.
80+
81+
![](https://upload.wikimedia.org/wikipedia/commons/4/43/PID_en.svg)
82+
83+
PID output value is the weighted sum of the control terms:\
84+
`error = set_point - current_temperature`\
85+
`di = ` temperature change between last two samples\
86+
`dt = ` time elapsed between last two samples\
87+
`P = Kp * error`\
88+
`I = last_I + (Ki * error * dt)`\
89+
`D = -(Kd * di) / dt`\
90+
`output = P + I + D`\
91+
Output is then limited to 0% to 100% range to control the PWM.
92+
93+
### Autotune (not tested, not guaranteed to work):
94+
You can use the autotune feature to find some working PID parameters.
95+
Add the autotune: parameter with the desired tuning rule, and optionally set the noiseband and lookback duration if the
96+
default 2 hours doesn't match your HVAC system bandwidth.\
97+
Restart Home Assistant to start the thermostat in autotune mode, make a step on the set point (positive for heating,
98+
negative for cooling) and wait for the autotune to finish by checking the _autotune_status_ attribute for success.\
99+
The Kp, Ki and Kd gains will be shown in the attributes and shown in the log like this: **"Set Kp, Ki, Kd. Smart
100+
thermostat now runs on PID Controller."** followed by the three computed gains Kp, Ki and Kd respectively.
101+
102+
**Warning**: I'couldn't yet save the settings to the configuration.yaml file. The PID parameters set by the autotune
103+
won't be stored and the process restarts everytime Home Assistant is restarted.\
104+
To save the parameters read attributes or log after autotune has finished, manually copy the values in the
105+
corresponding fields and remove the autotune parameter in the YAML configuration file before restarting Home Assistant.
106+
107+
## Parameters:
108+
* **name** (Required): Name of the thermostat.
109+
* **heater** (Required): entity_id for heater switch, must be a toggle device. Becomes air conditioning switch when
110+
ac_mode is set to True.
111+
* **target_sensor** (Required): entity_id for a temperature sensor, target_sensor.state must be temperature.
112+
* **keep_alive** (Required): sets update interval for the PWM pulse width. If interval is too big, the PWM granularity
113+
will be reduced, leading to lower accuracy of temperature control, can be float in seconds, or time hh:mm:ss.
114+
* **kp** (Recommended): Set PID parameter, proportional (p) control value (default 100).
115+
* **ki** (Recommended): Set PID parameter, integral (i) control value (default 0).
116+
* **kd** (Recommended): Set PID parameter, derivative (d) control value (default 0).
117+
* **pwm** (Optional): Set period of the pulse width modulation. If too long, the response time of the thermostat will
118+
be too slow, leading to lower accuracy of temperature control. Can be float in seconds or time hh:mm:ss (default 15mn).
119+
* **sampling_period** (Optional): interval between two computation of the PID. If set to 0, PID computation is called
120+
each time the temperature sensor sends an update. Can be float in seconds or time hh:mm:ss (default 0)
121+
* **precision** (Optional): the adjustment step of the set point (valid are 0.1, 0.5 and 1.0, default 0.1 for Celsius
122+
and 1.0 for Fahrenheit)
123+
* **min_temp** (Optional): Set minimum set point available (default: 7).
124+
* **max_temp** (Optional): Set maximum set point available (default: 35).
125+
* **target_temp** (Optional): Set initial target temperature. If not set target temperature will be set to null on
126+
startup.
127+
* **ac_mode** (Optional): Set the switch specified in the heater option to be treated as a cooling device instead of a
128+
heating device.
129+
* **away_temp** (Optional): Set the temperature used by “away_mode”. If this is not specified, away_mode feature will
130+
not be available.
131+
* **autotune** (Optional): Chose a string for autotune settings. If it's not set, autotune is disabled. The following
132+
tuning_rules are available:
59133

60134
ruler | Kp_divisor, Ki_divisor, Kd_divisor
61135
------------ | -------------
@@ -67,36 +141,15 @@ ruler | Kp_divisor, Ki_divisor, Kd_divisor
67141
"no-overshoot" | 100, 40, 60
68142
"brewing" | 2.5, 6, 380
69143

70-
* difference (Optional): Set analog output offset to 0. (default : 100). If it's 500 the output Value can be everything between 0, 500.
71-
* noiseband (Optional): (default : 0.5), set noiseband (float): Determines by how much the input value must overshoot/undershoot the setpoint before the state changes.
144+
* **noiseband** (Optional): set noiseband for autotune (float): Determines by how much the input value
145+
must overshoot/undershoot the set point before the state changes (default : 0.5).
146+
* **lookback** (Optional): duration of the signal analysis for the autotune, need to have 5 cycles (temperature crossing
147+
the set point) included in the analysis period, can be float in seconds, or time hh:mm:ss (default 2 hours).
72148

73-
#### configuration.yaml
74-
```
75-
climate:
76-
- platform: smart_thermostat
77-
name: Study
78-
heater: switch.study_heater
79-
target_sensor: sensor.study_temperature
80-
min_temp: 15
81-
max_temp: 21
82-
ac_mode: False
83-
target_temp: 17
84-
keep_alive:
85-
seconds: 5
86-
initial_operation_mode: "off"
87-
away_temp: 16
88-
kp : 5
89-
ki : 3
90-
kd : 2
91-
pwm : 10
92-
autotune : ziegler-nichols
93-
difference : 100
94-
noiseband : 0.5
95-
```
96-
### Help
97149

98-
The python PID module:
150+
### Credits
151+
This code is a fork from Smart Thermostat PID project:
152+
[https://github.com/aendle/custom_components](https://github.com/aendle/custom_components) \
153+
The python PID module with Autotune is based on pid-autotune:
99154
[https://github.com/hirschmann/pid-autotune](https://github.com/hirschmann/pid-autotune)
100155

101-
PID controller explained. Would recommoned to read some of it:
102-
[https://controlguru.com/table-of-contents/](https://controlguru.com/table-of-contents/)

custom_components/smart_thermostat/climate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def device_state_attributes(self):
361361
"""attributes to include in entity"""
362362
if self.autotune != "none":
363363
return {
364-
"control_output": 0,
364+
"control_output": self.control_output,
365365
"pid_p": 0,
366366
"pid_i": 0,
367367
"pid_d": 0,
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"domain": "smart_thermostat",
33
"name": "Smart thermostat",
4-
"documentation": "https://github.com/ScratMan/custom_components",
4+
"documentation": "https://github.com/ScratMan/HASmartThermostat",
5+
"issue_tracker": "https://github.com/ScratMan/HASmartThermostat/issues",
56
"requirements": [],
67
"dependencies": [],
7-
"codeowners": [],
8-
"version":"0.0.2"
8+
"codeowners": ["@ScratMan"],
9+
"version": "2021.11.1"
910
}

hacs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Smart thermostat (PID)",
3-
"country": "US",
4-
"domains": ["switch", "sensor"],
3+
"domains": ["climate"],
4+
"render_readme": true,
55
"homeassistant": "0.104.2"
66
}

info.md

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)