Thanks for the tip! I looked at MultiModeItem now, it’s going to take a while to wrap my head around it, but it seems like it can avoid having to use proxy items for certain things. Could be useful.
Also, I’m using PyCharm as you suggested in the tutorial. Great suggestion – I just installed it, and syntax highlighting and autocomplete just works automatically. That makes an incredible difference for a novice Python programmer.
I’ve written my first script! It’s working perfectly, but it’s ugly – I’ve written it as I would have in C++, not taking advantage of anything Python since I don’t know it (yet).
One common problem in home automation is asymmetrical delays.
That is:
While X is 1, do A
While X is 0, do B, and 10 seconds after that do C, as long as X didn’t become 1 again within that time.
I did that the arduino way… that is, I literally implemented a millis function and used timestamps:
def millis():
return int(time.monotonic_ns() / 1000000)
def __init__(self):
self.chandelier_level = 0
self.chandelier_on = 0
self.chandelier_on_timeout = 30000
self.chandelier_on_millis = millis() - 9999999
self.chandelier_power = -1
def periodicUpdate(self):
desired_on = 0
if self.chandelier_level > 0:
desired_on = 1
if desired_on:
self.chandelier_on = 1
self.chandelier_on_millis = millis()
elif millis() - self.chandelier_on_millis > self.chandelier_on_timeout:
self.chandelier_on = 0
if self.chandelier_power != self.chandelier_on:
self.chandelier_power = self.chandelier_on
self.ctrl_mcu5_elchand.oh_send_command("ON" if self.chandelier_power else "OFF")
Imagine the chandelier controller is WiFi controlled, and this power to the controller is itself connected to a WiFi relay with lots of channels that control multiple things. This saves a lot of power, compared to having everything in the house on active standby, because this way only the multichannel WiFi relay stays on.
self.chandelier_level
is the desired brightness of the chandelier.
When chandelier_level is greater than zero, then obviously the power to the chandelier controller needs to be on, and we need to tell it the level to be at.
When chandelier_level becomes zero again, we tell the chandelier controller to fade out, and then we give it plenty of time to fade out before we actually cut its power.
It works fine this way, but this seems like a lot of code for what is a common home automation problem!
@Spaceman_Spiff, since you’re a native Python programmer, I’ll bet you know a much more efficient way to accomplish the same thing, making use of HABApp and Python magic?
I also have a question regarding HABApp logging. With the default settings it’s logging every message on my MQTT server. I will change the level to WARNING of course, but:
- It doesn’t seem to obey the size restriction. logging.yml specifies
maxBytes: 1_048_576
and yet the events.log grows to a gigabyte before it rolls over.
- It’s nice to have detailed logging but not if it kills my SSD – is there an easy way to keep a log ONLY in RAM?
That blows my mind a little bit. As a C++ programmer, I assumed item
to be a handle or pointer.
Shouldn’t item == compare_value
always return false since they’re different types? What sorcery is this?