UbhiTS / ad-autofanspeed

Automatically control a room fan's speed based on a temperature sensor. Please :star: if you like this app :)
https://www.youtube.com/c/ubhits
Apache License 2.0
18 stars 6 forks source link

Four Speed Fans, 25%-50%-75%-100% #11

Open Taylorp913 opened 3 years ago

Taylorp913 commented 3 years ago

The treatlife fan switch connected to Home assistant through the TUYA integration has four speeds, 25%-50%-75%-100%.

Create and optional parameter between mid and high that can control the fan at 75%.

Something like this? speeds: low: 67 medium: 69 med-high: 73 high: 76

Taylorp913 commented 3 years ago

Would you like me to make a pull request for this?

UbhiTS commented 2 years ago

Hi, after a year of a break, I'm back. Are you still using this automation? should I fix it ?

Taylorp913 commented 2 years ago

Haha yeah I will be using it as soon as tuya fixes their home assistant integration to re-enable the fan control. Ultimately I love this automation and brag about it all the time.

UbhiTS commented 2 years ago

i'm on it ... i'll send you a private version since I do not have any way to test it, once you confirm, will update the repo ...

Azaretdodo commented 2 years ago

Hello,

I had the insight but I wasn't found the word for 75 percentage !

The good word is halfway !

(you can try with your word you will fall on a mistake),

by my side I have found the loop in 3.10 on google but it print nothing...

and the indent show is false :

[(https://docs.python.org/fr/3/reference/compound_stmts.html#the-match-statement)]

a teacher of python was say it isn't advice to use python 3.10 now...

the code is over :

#-------------------------------------------------------------------------------
# Name:        Azaretdodo
# Purpose:
#
# Author:      Dorian ROSSE
#
# Created:     06/11/2021
# Copyright:   (c) Dorian ROSSE 2021
# Licence:     <your licence>
#-------------------------------------------------------------------------------

def initialize(self):
    # DEFAULTS
    self.debug        = True;
    self.low          = 67
    self.medium       = 69
    self.halfway      = 73
    self.high         = 76
    self.medium       = 70
    self.halfway      = 71
    self.high         = 72
    self.offset       = 0
    self.start        = datetime.strptime("21:00:00", '%H:%M:%S').time()
    self.end          = datetime.strptime("09:30:00", '%H:%M:%S').time()
def temperature_change(self, entity, attribute, old, new, kwargs):

    if self.is_time_okay(self.start, self.end):
      room_temperature = float(new)
      fan_speed = self.get_target_fan_speed(room_temperature)
      self.call_service("fan/set_speed", entity_id = self.fan, speed = fan_speed)
      fan_speed_percentage = self.get_target_fan_speed(room_temperature)
      self.call_service("fan/set_percentage", entity_id = self.fan, percentage = fan_speed_percentage)

def get_target_fan_speed(self, room_temperature):

    # if sun is above horizon, then add offset
    sun_above_horizon = self.get_state(self.sun) == "above_horizon"
    offset = self.offset if sun_above_horizon else 0
    fan_speed = "off"
    fan_speed_percentage = 0

    if room_temperature < self.low + offset:
      fan_speed = "off"
    elif room_temperature >= self.low + offset and room_temperature < self.medium + offset:
      fan_speed = "low"
    elif room_temperature >= self.medium + offset and room_temperature < self.high + offset:
      fan_speed = "medium"
    elif room_temperature >= self.halfway + offset and room_temperature < self.hafway + offset:
      fan_speed = "halfway"
    elif room_temperature >= self.high + offset:
      fan_speed = "high"
    if room_temperature >= self.low + offset: fan_speed_percentage = 25
    if room_temperature >= self.medium + offset: fan_speed_percentage = 50
    if room_temperature >= self.halfway + offset: fan_speed_percentage = 75
    if room_temperature >= self.high + offset: fan_speed_percentage = 100

    self.debug_log(f"AUTO FAN SPEED: {str(room_temperature)}/{fan_speed}")

    if sun_above_horizon: self.debug_log(f" (SUN OFFSET)")
    self.debug_log(f"AUTO FAN SPEED: {str(room_temperature)}/{fan_speed_percentage}%" + (" (SUN OFFSET)" if sun_above_horizon else ""))

    return fan_speed
    return fan_speed_percentage

def hvac_daily_shut_off(self, kwargs):
    self.call_service("fan/turn_off", entity_id = self.fan)
    self.debug_log("FAN AUTO OFF")
def is_time_okay(self, start, end):
    current_time = datetime.now().time()
    if (start < end):
      return start <= current_time and current_time <= end
    else:
      return start <= current_time or current_time <= end

def debug_log(self, message):
    if self.debug:
      self.log(message)

def match():
    match(self.low or self.medium or self.halfway or self.high)
    case(self.low)
    print("the fan is slow and cool")
    case(self.medium)
    print("the fan is moderate and temperate")
    case(self.halfway)
    print("the fan is between medium and high")
    case(self.high)
    print("the fan is hight and hot")

have a nice end of weekend from the France the sun is shining and it is sixteen to thirteen here,

Regards.

Azaretdodo.