diff --git a/contour.py b/contour.py index 1121e69..7f2fe95 100644 --- a/contour.py +++ b/contour.py @@ -25,10 +25,10 @@ class Contour: return self.current_value return None - def check_state(self, state_value): + def check_state(self, state_value, force = False): if self.state == state_value: return - if (self.state is None) or (time.time() - self.state_change_time > self.CHANGE_FREQ): + if (self.state is None) or (time.time() - self.state_change_time > self.CHANGE_FREQ) or force: self.state = state_value self.logger.info("Contour " + self.sensor_topic + " changed state to " + str(self.state)) diff --git a/heat_control.py b/heat_control.py index 918e97e..8391219 100755 --- a/heat_control.py +++ b/heat_control.py @@ -105,10 +105,13 @@ def on_message(client, userdata, msg): any_opened_start = 0 any_opened = False +boiler_out = 0 +pump_out = 0 + def check_temperatures(): """Check temperatures against target and control GPIO""" - global any_opened_start, any_opened + global any_opened_start, any_opened, boiler_out, pump_out # check boiler exhaust, turn off if too big if (boiler_exhaust_t > BOILER_EXHAUST_LIMIT): logger.error("Boiler exhaust overheat") @@ -116,9 +119,9 @@ def check_temperatures(): # turn on the pump # and turn off the boiler of course for c in contours: - mqtt_client.publish(c.out_topic, "1", retain=True) - mqtt_client.publish(boiler_control, "0", retain=True) - mqtt_client.publish(pump_control, "1", retain=True) + c.check_state(True, True) + boiler_out = 0 + pump_out = 1 return # sum_t = 0 @@ -132,9 +135,10 @@ def check_temperatures(): if count_t == 0: logger.error("No temperatures available, switch to full-on mode") for c in contours: - mqtt_client.publish(c.out_topic, "1", retain=True) + c.check_state(True, True) # turn on boiler - mqtt_client.publish(boiler_control, "1", retain=True) + boiler_out = 1 + pump_out = 1 return for c in contours: @@ -152,11 +156,6 @@ def check_temperatures(): if contour_t > target_t + hysteresis: # should turn off contour c.check_state(False) - if c.state: - # 0 means "closed" - mqtt_client.publish(c.out_topic, "1", retain=True) - else: - mqtt_client.publish(c.out_topic, "0", retain=True) if any_opened: any_opened = False @@ -171,17 +170,31 @@ def check_temperatures(): logger.debug("Any opened: " + str(any_opened) + " aos " + str(any_opened_start) + " time " + str(time.time())) if any_opened and time.time() - any_opened_start > BOILER_TURN_ON_AFTER: - mqtt_client.publish(boiler_control, "1", retain=True) - mqtt_client.publish(pump_control, "1", retain=True) + logger.info("Starting up pump and boiler, contours should be opened now") + boiler_out = 1 + pump_out = 1 else: - mqtt_client.publish(boiler_control, "0", retain=True) - mqtt_client.publish(pump_control, "0", retain=True) + logger.info("All contours are closed, shutting down boiler and pump") + boiler_out = 0 + pump_out = 0 + +def issue_commands(): + for c in contours: + if c.state: + # 0 means "closed" + mqtt_client.publish(c.out_topic, "1", retain=True) + else: + mqtt_client.publish(c.out_topic, "0", retain=True) + mqtt_client.publish(boiler_control, str(boiler_out), retain=True) + mqtt_client.publish(pump_control, str(pump_out), retain=True) + def main_cycle(): """Main 5-second cycle""" while True: try: check_temperatures() + issue_commands() except Exception as e: logger.error(f"Error in main cycle: {e}") # Wait 5 seconds