So I’ve had help with this script in the past but lately I’ve ran into some situations where both of my switches were turned on at once and this can lead to fan failure and I’d really like to avoid that. So initially I did some checks to make sure that they weren’t both on at once and it does help but I’d really like to just try and code it as logically as possible to prevent it.
So I took some code from another one of my apps with a while not loop because I don’t want the script to proceed further until the while not is no longer a case and then it can roll. But I wanted to throw it out here to you all and see if my logic looks sound.
def fan_control_cb(self, entity, attribute, old, new, kwargs):
if new == "False":
if self.get_state(self.fan_low) == "on":
self.log("Turning off the fan")
self.run_in(self.turn_it_off, 1, fan_speed=self.fan_low)
elif self.get_state(self.fan_high) == "on":
self.log("Turning off the fan")
self.run_in(self.turn_it_off, 1, fan_speed=self.fan_high)
elif (
self.get_state("sensor.all_doors_and_windows") != "closed"
and self.get_state("sensor.kids_bathroom_door") != "closed"
):
self.log("We have an open window and an open door we can turn on the fan")
# Passed safety checks now lets get the fan ready to go
if (
self.get_state(self.whole_house_thermostat, attribute="fan_mode")
!= "Auto Low"
):
self.log("Turning off the furnace fan because its time for the big fan")
self.run_in(self.fan_circulate_off_cb, 2)
if self.notify == 1:
self.notify = None
if new == "High":
self.fan_wrong_speed = False
while not self.fan_wrong_speed:
self.fan_wrong_speed = self.run_in(
self.check_for_wrong_speed, 5, fan_speed=self.fan_low
)
self.log("Turning on high speed")
self.run_in(self.turn_it_on, 5, fan_speed=self.fan_high)
if new == "Low":
self.fan_wrong_speed = False
while not self.fan_wrong_speed:
self.fan_wrong_speed = self.run_in(
self.check_for_wrong_speed, 5, fan_speed=self.fan_high
)
self.log("Turning on low speed")
self.run_in(self.turn_it_on, 5, fan_speed=self.fan_low)
else: # fan turned on but no widows or doors are open
self.select_option(self.fan_setting, "False")
if self.get_state("group.all_persons") == "home" and self.notify == None:
self.log("Announcing that all windows are closed")
self.window_notify_cb()
self.notify = 1
def check_for_wrong_speed(self, kwargs):
self.fan_wrong_speed=True
if self.get_state(kwargs["fan_speed"]) == "on":
self.log(
"We found out that {} was still on so we're going to turn it off now".format(
kwargs["fan_speed"]
)
)
self.run_in(self.turn_it_off, 5, fan_speed=kwargs["fan_speed"])
self.fan_wrong_speed = False
return(self.fan_wrong_speed)
Thank you all for your assistance with this. I do understand that putting while loops in appdaemon is bad which is why we do the while not to hopefully not tie up multiple threads for long periods of time while it checks the conditions. Mainly I just want to make sure that as long as that while not is running it will not proceed further and attempt to turn on the other switch. I tried to put in an if statement to check the value of the self.fan_wrong_speed but then we just never turned on the fan at all, and of course that defeats the purpose of the fan.