ScratMan / HASmartThermostat

Smart Thermostat with PID controller for HomeAssistant
338 stars 49 forks source link

Round P / I / D / Control Output values #52

Closed bamnoru closed 2 years ago

bamnoru commented 2 years ago

Hi ScratLan,

I've set a min_cycle_duration to 120 (my valve takes more than 1 minutes to be fully opened). But, after some times, due to values with lot of number after comma, the I or D values were close to 0, but not really (0,00...), and the output_control was very small (0,05%), and the valve was opened 2 minutes (min_cycle_duration). My PWM is 900, so 0,05% is 0,45 secondes.

image

Value copied from developpers screen in HA : control_output: 0.05850523519505746 pid_p: 0 pid_i: 0 pid_d: -0.0

Maybe, some values should be rounded.

Regards,

ScratMan commented 2 years ago

Hello, I'm afraid the rounding would cancel the heating, leaving the temperature down. In v2021.12.5-beta1 the when time_on is below the minimum cycle duration setting, the off time is increased to have a longer PWM and keep the ratio computed by the PID. Would it be enough ?

bamnoru commented 2 years ago

Hi,

With values seen today (on time of 0,45 seconds), if the real on time is 120 seconds, off time will be very long to keep the ratio, no ?

Using 'float' and not rouding could give some strange behavior, see this example : current_temperature: 19.9 temperature: 19.5 Kp: 200 pid_p: -79.99999999999972

pid_p should be 80 (error : 19.5-19.9 => -0.4 * 200 => -80)

See : https://docs.python.org/3/tutorial/floatingpoint.html

ScratMan commented 2 years ago

You mean only round the values decimal for display (like 80, 80.2 or 80.23) or rounding the final time period (45 or 68 seconds rounded to 1mn) ?

Rounding on the control output can have a strong impact on the heating modulation accuracy depending on the PWM period, 1% of 5 minutes is huge Vs 1% of 120 minutes.

bamnoru commented 2 years ago

Rounding values for display will not resolve this issue. But, I don't know exactly how to solve it.

Using float generates residual values, in particular, when calculating "I", and, after some cycles, when the value should be 0, with float error, the value is not really 0. Maybe rounding the values during calculation should avoid float errors.

Of course, rounding values during calculation will change the final result, but, the difference will be only about seconds, and, will have no real impact. Is it really huge to round 1% of 5 minutes (3 secondes), and, it's also depends on the keep_alive. If rounding error is less than keep_alive period, the time on/off will not change.

ScratMan commented 2 years ago

The integral doesn't need to tend to 0, a residual value won't disturb the system. I'll look into the rounding suggestion.

ScratMan commented 2 years ago

I've set the accuracy to 0.1% in the new beta version. Is it OK ?

bamnoru commented 2 years ago

Probably, I will download and test it now.

Thanks.

TippyLion28 commented 1 year ago

Does the sensor input get rounded too? My sensor has 3 decimal places of resolution but not sure whether this component is taking advantage of that