Thermostat with PID controller

Hi all,

just wanted to share my experience with smart thermostat. I am using it to control the Raspberry Pi CPU cooling fan: indeed, piin 18 can be used for PWM speed control. Find my configuration below:

# CPU cooling control   
switch:
  - platform: rpi_gpio
    ports:
      18: RPI Cooling Fan

climate:
  - platform: smart_thermostat
    name: Controllo Temperatura CPU - PID
    heater: switch.rpi_cooling_fan
    target_sensor: sensor.processor_temperature
    min_temp: 20
    max_temp: 80
    ac_mode: true
    target_temp: 40
    keep_alive:
      seconds: 15
#   kp: 0.4743813504974523
#   ki: 0.010442197418386305
#   kd: 11.706605200464034
    kp : 5
    ki : 0.05
    kd : 0
    pwm:
      seconds: 15
    sampling_period: 0
    target_temp_step: 0.5
    noiseband: 0.5
    initial_hvac_mode: "cool"
#    lookback: 01:00:00
#    autotune: ciancone-marlin

I have also used the autotuning feature (commented gains came from it) but then adjusted the gains according to my needs.

Obiouvsly, this is not a speed control, it just turns the fan on and off according to pwm window. So now my questions

  • Has anyone else used this setup? Any suggestion on better configuration?
  • How should I consider the parameter pwm? Is that the time window that PID uses to calculate the pulse lenght? E.g. if my pwm is 15 seconds and output is 50%, the fan will be on for 7.5 seconds
  • Would it be possible to implement a true pwm control (e.g. using pigpio library) for fan speed control?
  • Would it be possible to expose all the PID parameters for integration into Lovelace?

Thank you for your great job on this smart thermostat!

They are available as attributes under climate entity. I log all to influxdb, but you can also display them as graph/history stats within HA

Hi together, i didnt read all comments here.

So my question is, if i could control a heater valve with it, which is controlled with the valve opening from 0% to 100%. I know here are PID sontrollers which output a value from 0 to 100 to open the valve accordingly and not like the PWM style what i read here.

So i mean open the valve by lets say 25% and not open the valve completely for 2,5 minutes in the range of 10 minutes.

I hope you understand what i mean.

Hello.
Itā€™s difficult to be sure, as the pid_dt attribute is setting the scale so I canā€™t see the levels of p, i and d.
But the heater activation with positive error often means integral is too high or Kd gain is too high Vs the temperature decrease rate.

I donā€™t recommend to use this thermostat to adjust fan speed of the Raspberry through Home Assistant. If Home Assistant core hangs or its priority becomes low due to heavy resources loading, fan control will be down while processor may heat a lot.
This PWM should be controlled at Linux core system instead, there are python scripts that can be directly executed by the system services controller, Iā€™m using it with satisfaction.

Yes, you can create a template sensor to get the control_output attribute, and feed this to your valve.

1 Like

I am using HA OS, is it possible to use these phyton scripts as well? Can you point me to the right resources or help me with a step-by step guide?

Then I canā€™t help. I donā€™t know how HA OS is managing the Pi, but itā€™s Linux based, so there should be systemd services management.

I used this tutorial :

If you can manage your pi with SSH you may be able to add it, but Iā€™m afraid HA OS updates may overwrite and break manually added services.

I personally use Raspbian and run several servers in Docker containers (Home Assistant, ZWaveJS2MQTT, Grafana, Portainer, InfluxDB, MQTTā€¦) when there are frequent updates, or directly as services at system level (fan controller, SomfyProtect2MQTT, deConz, NodeJS, TVheadEndā€¦). It gives much more flexibility.

Sounds good, i will try this. Thanks!

Maybe you can try using a script, the script will read the attribute directly (using {{ state_attr('climate.thermostat', 'control_output') }} template) and set the valve directly, and set the script as heater entity.

1 Like

Hi!

Really likes this thermostat! Using it with a shelly RGBW-unit to operate 2 actuators(for the moment) and it seems to work very well. My issue is that my actuators is of type NO(normally open) which means that when thermostat is trying to heat, it closes the valve and room will be colder. For now i made i Q&D solution by calling SERVICE_TURN_OFF instead of SERVICE_TURN_ON and also validating that entity is STATE_OFF in function _is_device_active. It seems to work but as i said, this is very dirty. I think that the configuration should have one parameter like invert output=Yes/No or something. If this is already possible, please show me.
Finally, thanks for this awesome work!

I see that there is a HACS integration called Pyscript that might do the trick, but I need to study a little bit more to understand how it works

https://hacs-pyscript.readthedocs.io/en/stable/index.html

Iā€™ve added a invert_heater setting to invert the polarity of the heater switch. You may test it on v2022.1.0

1 Like

Thank you so much adrien for the invert_heater feature in the new version. Will test it right away!

IĀ“m testing it and at first i realized some odd behavior since sometimes when actual temperature was one deg below set temp it was idle. Then after looking at the history for the thermostat(very useful!), I realize that the problem was meā€¦
IĀ“m using Shelly H&T and to save batteries i configured it to only send when the temperature has changed one degreeā€¦ I guess the thermostat is having a hard life regulating in this conditions. Will set the update for a higher resolution tomorrow. I guess this is what you call SISO(Shit In Shit Out). So just make sure you are smarter than me :slight_smile:

The PID needs precision for temperature measurement to adapt the modulation whenever temperature changes. So yes you need at least 0.1Ā°C precision on the sensor otherwise the overshoot will be strong and regulation not good at all.
And please note that the themostat might remain idle while measured temperature is below the set point. As the PID considers the error (set point - measured temperature) and temperature variation (increasing or decreasing rate and duration above or below set point), if the temperature is 1Ā°C below the target but increasing, the thermostat may reduce the ON time or switch off completely the heater.
This will depend on the PID gains.

Hi all,

I have read the discussion above, and previously I was also experimenting with my own PID controlled thermostat.
In my setup I want to control the boiler modulation using OpenTherm, not just on/off.
My question is: How to translate the PID output to the boiler temperature ? Simple scale to allowed temperature range (ex. if PID is 50% and the temperature range is 35-55 degrees, then the boiler temperature is 45 degrees ?)

Pyscripter allows you to import RPi.GPIO and I can manipulate the pins reading and writing. I canā€™t get the PWM to work though, no errors, but also no output.

I can now confirm that pigpio works in the Pyscript environment.
Have a look here if youā€™re interested

Does this work for standard heaters also? I have made mine available as on/off switches and want to use external sensors. Or is there something better to use than just generic thermostat?