Ok here is today’s latest and greatest script by me the noob
import appdaemon.appapi as appapi
class keep_light_below(appapi.AppDaemon):
def initialize(self):
self.listen_state(self.adjust_light_function,(self.args["lightID"]))
# Found this cool function to convert the brightness to a percentage because well thats easier than doing math
def brightness_to_percent(self, brightness):
return 255/100 * int(brightness)
def adjust_light_function(self, entity, attribute, old, new, kwargs):
bright_level = self.get_state(self.args["lightID"], "brightness")
self.log(bright_level)
bright_level_percent = self.brightness_to_percent(bright_level)
self.log(bright_level_percent)
if bright_level_percent > 90:
brightness90 = self.brightness_to_percent(90)
self.turn_on(self.args["lightID"], brightness=brightness90)
All I’m trying to do with this is keep a certain light below 90% on the dimmer. If it goes above this the lights flicker. But as long as I keep them at 90 or lower then we’re all good.
Problem is with my math I do believe. I figured I could use that brightness_to_percent to get the value that is being returned to HA and then convert that value into a percentage. Then I could take that value and compare it that if its above 90 then we can act on it otherwise just do nothing. I thought I had this one, I was so sure of it but no luck here. Its like its reading the 90 as the level from HA not the converted to a percentage value.
I didn’t even think about that being an issue. And I’m sure you’re 100% right since we sometimes get back a number with a decimal in it. Ah see it was my math skills
import appdaemon.appapi as appapi
class keep_light_below(appapi.AppDaemon):
def initialize(self):
self.listen_state(self.adjust_light_function,(self.args["lightID"]))
# Found this cool function to convert the brightness to a percentage because well thats easier than doing math
def percent_to_brightness(self, brightness):
return int(round((int(brightness)/255)*100))
def brightness_to_percent(self, brightness):
return 255/100 * int(brightness)
def adjust_light_function(self, entity, attribute, old, new, kwargs):
bright_level = self.get_state(self.args["lightID"], "brightness")
bright_level_percent = self.percent_to_brightness(bright_level)
self.log(bright_level_percent)
if bright_level_percent > 90:
brightness90 = self.brightness_to_percent(90)
self.turn_on(self.args["lightID"], brightness=brightness90)
I need to add some logic that if the state changes to off to just exit out instead of freaking out because the brightness is null but not a big deal right now
I also had to rename the reverse math function and then add back in the other one but we’re in business now.
int(round((int(brightness)/255)*100)) is redundant, (int(brightness)/255)*100 should be fine. int(round( isn’t required. When performing math in python without a decimal point will treat the number as integers. Example:
i just checked if i am not wrong, but
self.log(355/100)
return 3.55
brightness can be a string, so you need the int there
int(brightness)/255*100 can be with a decimal so you need to round.
the int is needed to make it an integer again.
355/100 will return 3.55
int(355/100) will return 3
int(round(355/100)) will return 4
Did you read my reply are are you trying to prove a point? I said use >=? How does that not solve that? A threshold is a number. A number can be greater than or greater than or equal to.
but you are still wrong.
your formula would return the same threshold for 230, 231 and 232
so after the calculation you cant make a difference anymore.
you started with saying that it is redundant.
and thats just not true.
the 2 formulas are different and have different results and the results need to be observed different.
i can create 1000 different formulas, with all another result, but the point was to get the closest brightness int that comes to 90 (or any other % if you like)
you can never create the same outcome from that.
doesnt matter which you use > or >= the result will be different.
like i said, the easiest way and the most accurate way would be to not use the formula and just use the brightness itselve as threshold, but that was not the point from this formula.
Again: That’s where the 1/255 difference comes into play. Because 0.5% error is equivalent to 1 pot value:
0.5/100 = 0.005
0.005*255 = 1.275.
the .275 will always be truncated because of the integer translation.
I’m starting to suspect that you aren’t reading my responses and you are just trying to get an argument ‘win’, because I gave you the 1/255 error 2 responses ago.