No announcement yet.

How to control a light - after all, how hard can it be?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Uncle Michael
    Turning one light on or off is easy.
    As the number of lights increases (especially if they do not all operate identically), and the questions of When? and By how much? creep in, the problem's complexity increases geometrically. Good description of your approach. Thanks.

    Leave a comment:

  • larhedse
    started a topic How to control a light - after all, how hard can it be?

    How to control a light - after all, how hard can it be?

    So I have been using Home Automation since 2008. It all started out with the simple task of automating the window lamps when it was dark outside. That was simple. Nowadays, well let's just say it is a bit of a different challenge... I might have started with simple things and 433MHz (Telldus) plugins - nowadays it is more Z-Wave and HomeSeer (I tested a few others like Vera, Fibaro, OpenHAB, Indigo and a few others).

    So after I changed to Z-Wave, I decided all lamps plus then some would be controlled over my HA controller. Done!

    Then I got thinking, if I use the light sensors, I could do a sliding dimmer scenario (slinding in my interpretation is when the level only changes 1% at every time) where the lights are dimming after the level of light that comes in thru the windows - that would be nice, right?

    And then we have that projector with that 135" screen - need to handle that one also?
    And when someone manually activates the ceiling lights, that also needs to be handled?
    And depending on how many people (and who) one could adapt the lights after that too?
    And would it not be nice if all lamps had some sort of automagic turn off?

    It all seems so easy when one reads this - however it is after all multiple lights involved and well as it turned out it has taken med a year or two to get everything working since my last change (that would be the automagic turnoff).

    So let's start with how to calculate if it is dark outside. Well it did turn out that I needed to track "Dark", "Shadow" and "Light" - three levels, not two. Anyway I have 5 light sensors inside and one outside, I collect all the values and calculates an average - that is easy. Then I noticed that well that was not good enough. It failed during some situations when we got a lot of clouds. At the same time I was playing around with an online graphing utility ( and noticed that I could use my outdoor humidity reading as a replacement for when it got a few clouds - not perfect in any way but better than expected. So I adjusted the calculated "lightlevel" from the lightsensors by removing the amount of humidity. It got better but still not good enough. I then decided to subtract also the outside temperature. And that happened to be about as good as I can do this. Of course if I had something like the hight and color of the clouds that would maybe be better - but I am actually satisfied with how this turned out - it is good enough.

    In the first incarnation I simply had a script that had a target of 70% dim level, and for every minute I matched this against the calculated light sensor, and it "jumped" to the target (no sliding) - it was an easy way to get the most basic dimming system to work - however it did not cover the projector or any of the other stuff.

    So then "it" started. That project that I thought would take a weekend or so - boy was I wrong. A year later, well now it does work as intended. No I did not work on this all the time, it been more of a on/off kind of project. When I hit a "wall" I just left it in semi working order and took some time away from it. Not the fastest way to solve the issues I had, but I do have a life to take care of ;-)

    First I decided to handle dimming calculations separate from actually turning on/off/dimming lights. So I set up a bunch of Virtual Devices to do the "calculalted dimmed value", and then a few events that would trigger to whenever that Virtual Device changed and then handle the on/off/dimming execution. I separated this since I needed to control different lights depending on when the Projector was on or off, and other kind of stuff like "manuall overide" (I will come to that...). It was just simpler to develop this way instead of trying to do everything at once (I tried that, it failed...).

    So let's dig into this calculated dim value then, a few VD (Virutal Devices):
    TargetDimLevel = Calculation for all lightsensors, humidity and outside temperature. It is designed to get me a max value between 0% and 99%, however I only allow 0% and 70% since the lamps are just to strong in max power.
    ActualDimLevel = The current level om dim that all lamps should be at if nothing else...
    TurnDownLevel = This is one of the features I have, when TOD (TimeOfDay) is TurnDown, I start to ramp this one up from 0% to 30%, to slowly reduce the dim level on all lamps. This is very effective I noticed, I seem to get to sleep in the sofa a bit to often :-)
    ProjectorDimLevel = This is when the projector is turned on, this is more of either 0% or 20% when projector is turned on - it is of course adjustable, but for now it is 20%.
    TrueDimLevel = ActualDimLevel - TurnDownDimLevel - ProjectorDimLevel - and testing for minimum of 0% of course.

    It may seem to many, but there are some more... Anyway, the general idea is that TrueDimLevel will fight to catch TargetDimLevel. ActualDimLevel is the one that goes up/down all the time, the rest is just to compensate for my strange automagic stuff.

    So that sounds easy right - well it is, when one has everything in place.

    Then we move on to the turn on/off/dim routine. The major reason this is a separate script is because when the projector is turned on some lamps are off (like ceiling) and well the others are reduced (the VD for ProjectorDimLevel). And since we sometime needs more light, we have what is called "Manual override" which in general does nothing more than allow more light level than TargetDimLevel is at - it is when you simply need to override the system. This is also handled in this script. On top of all that we also have a semi manual override - it gives us the possibility to go 30% or 50% or 70% for what ever reason. Works perfect.

    And then I decided to have that automagic turn off of all devices. Yea this made my day - and crashed the system... It is very easy in once mind, however I stumbled onto a few different obstacles... Like the fact that some of my Z-Wave fibaro devices did not update status to HS3. That one I was able to solve by changing from S1 input to S2 input on thoose devices, however that took like for ever to figure out. I also messed up the "when is it manual override" during a period, because I simply coded things wrong. I made it to advanced to be honest, and I payed for that.

    Anyway my current "automagic turn off lights system" uses the notes field on the devices I like to handle automagic on. I decided to use the notes field since well it is a way of not hardconding everything - this is more of general routine. It works from parameters stored into the device I need to control. Basicly I have a string like a "Parametersyntax":

    vv = AO = AutoOff is active
    vv = MO = Default position = ManualOff
    xx = number of time units for daytime control
    yy = number of time units for nighttime control
    zz = number of time units for manual control
    t = timeunit (m/n=minute, h=hour)
    b = OPTIONAL, Makes it possible to change what DeviceID that is to be controlled: v=Virtuellt DeviceID typ manual override, x=eXlusiv DeviceID in other words it will be depending on this DeviceID
    dddd = OPTIONAL, DeviceID to control instead of the current one in the loop - so it's like a replacement...

    May feel strange, however it works for us - it does what we need. And I never have to change the code for any device, I just play around with the parameter.

    Yes there is a separate script that runs every minute for turning off devices.

    So now in the end, I have everything I wanted, it works and just well works.

    So can it be so hard to turn on a light? yea it is - depends on what you actually mean by turning on a lamp :-)