Author Topic: Using HMS100 for sunrise / sunset  (Read 6171 times)

Offline FlyBoyBob

  • Full Member
  • ***
  • Posts: 163
  • Karma: +0/-0
Using HMS100 for sunrise / sunset
« on: February 18, 2011, 11:39:31 am »
My first post  (hoping for good things...)

I have an HMS100 motion/light sensor in a window that gives excellent light readings that ramp up quickly at sunrise and down quickly at sunset. Setting up a Scene to trigger an event (turning on lights at first) when the value goes > for sunrise and < for sunset has a lot of potential, except that I get an Event every time the value changes in the > or < range = blinking lights.

I am a programmer by trade and can warm up to LUUP code quickly I'm sure, but have not found any relevant samples in these forums. Can someone please point me to what must be an often asked topic or suggest how I can modify my event so that it triggers once on a > value and not again for a certain amount of time or maybe a date change? Or anything more clever...

thanks in advance

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Using HMS100 for sunrise / sunset
« Reply #1 on: February 18, 2011, 11:57:19 am »
You can look here at the second example. It should be similar to what you want.

Offline FlyBoyBob

  • Full Member
  • ***
  • Posts: 163
  • Karma: +0/-0
Re: Using HMS100 for sunrise / sunset
« Reply #2 on: February 18, 2011, 12:58:13 pm »
Thanks for that pointer. Below is my first shot at some code.
The first section defines my HMS100 device number, the sensor's SID, and a low and high light level that defines "sunrise" (to be adjusted on test)
First I get the current level (every x minutes that the event timer is set for) and return false if it is outside the valid-for-sunrise range.
Then I need to test a persistant variable for a date when this scene last triggered, if it was today then return false otherwise set it to today and return true. I also set that variable to 0 when the light level < lo_level so I don't get a "sunrise" as the sun sets...

I have not tried or tested the code yet, my first doubt is the lastSunrise variable and how to make it persist between executions? Or can anyone suggest an alternative to remembering the date the scene was last run? Any other corrections to code below that won't run properly also greatly appreciated.

<source lang="lua">
local lo_level = 20
local hi_level = 40
lastSunrise

local deviceNo = 13
local SS_SID   = "urn:micasaverde-com:serviceId:LightSensor1"

local currentLevel = luup.variable_get(SS_SID, "CurrentLevel", deviceNo) or "0"
currentLevel = tonumber(currentLevel)

if currentLevel < lo_level then
   lastSunrise = 0
   return false
end
if currentLevel > hi_level then
   return false
end

if lastSunrise == os.date() or 0 then
   return false
end

lastSunrise = os.date()
return true
</source>
« Last Edit: February 18, 2011, 01:29:10 pm by FlyBoyBob »

Offline FlyBoyBob

  • Full Member
  • ***
  • Posts: 163
  • Karma: +0/-0
Re: Using HMS100 for sunrise / sunset
« Reply #3 on: February 18, 2011, 02:02:09 pm »
Update:

changed first lastSunrise to "= 0"
deleted:
<source lang="lua">
</source>

I don't know if it works as intended but at least it seems to be valid code...
« Last Edit: February 18, 2011, 02:39:52 pm by FlyBoyBob »

Offline CMRancho

  • Hero Member
  • *****
  • Posts: 1008
  • Karma: +1/-0
Re: Using HMS100 for sunrise / sunset
« Reply #4 on: February 18, 2011, 03:13:48 pm »
I believe the 3-in-1's light sensor adjusts itself over time to adjust its range to the conditions of its placement. Since it doesn't display light levels based on an absolute scale of actual lumens, does  this complicate the ability to use the device in this way?
Vera123beta; Schlage, Kwikset, TZEMT400; Linksys cams, Smarteye cams, HSM100; Aeon, Everspring, Cooper, Leviton, Intermatic, FortrezZ, Global Caché, USB-UIRT, Somfy, etc.

Offline FlyBoyBob

  • Full Member
  • ***
  • Posts: 163
  • Karma: +0/-0
Re: Using HMS100 for sunrise / sunset
« Reply #5 on: February 18, 2011, 03:32:01 pm »
I've only had it a few days, and it's advertised as 4-in-1 at the moment = motion, light level, temperature, and battery. I have it set to "wake up" every 5 minutes (default is 1/2 hour) and this morning the light level on my front porch went from 0 to 75 in one hour (cloudy) and is now sitting at 98 with the sun shining.

I don't know about the self-adjusting but both dawn and dusk seem to go linearly from limit to limit in a little over an hour, so I think I should be able to get a good relative-light-level event to turn lights on and off, based on real light vs predicted times. If my code works!

So far I am very impressed with the current device:
http://www.amazon.com/gp/product/B003A6UQL4/ref=oss_product

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Using HMS100 for sunrise / sunset
« Reply #6 on: February 21, 2011, 04:54:43 am »
I've written a piece of Lua code to make a scene run once at dawn and once at dusk. You can find it here.

It's not tested so it may not work as it should. Play with the code and modify it as you like. If you get it to work well then give me the code so I can update the code on the Wiki page.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Using HMS100 for sunrise / sunset
« Reply #7 on: February 21, 2011, 05:27:08 am »
From the manual:

Quote
Light Report

EZMotion can report the amount of light currently in the room. The value is from 0 to 100% and is relative
to the maximum and minimum amount of light EZMotion has measured since the blue reset button was
last pressed.

If your setup doesn't work reliably, you could install DAD (http://forum.micasaverde.com/index.php?topic=5466.0) and use a Luup timer to run scenes at begin/end of civil twilight.

Offline BillC

  • Sr. Newbie
  • *
  • Posts: 34
  • Karma: +0/-0
Re: Using HMS100 for sunrise / sunset
« Reply #8 on: February 23, 2011, 11:06:13 am »
You can look here at the second example. It should be similar to what you want.

I followed the instructions and created an event to run every hour and added the LUA code.  The part I can't understand is how to link an event - turn a light off at dawn and on at dusk- to the LUA code or to the event.  It seems the code will detect both dawn and daylight conditions, so how do you set up the event for the correct on/off?

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Using HMS100 for sunrise / sunset
« Reply #9 on: February 24, 2011, 04:02:09 am »
You need to create two scenes: one to turn on the lights at dusk, and one to turn off the lights at dawn. In the scene that turns on the lights you need to put the part of the code that runs when the light level is low:
Code: [Select]
if currentLevel <= LOW_LEVEL then
    local timeOfDay = luup.variable_get (LS_SID, "TimeOfDay", DEVICE_NO) or "Day"
    if timeOfDay == "Night" then
        -- It's night, so the scene has run once.
        return false
    else
        -- It's sunset, run the scene.
        luup.variable_set (LS_SID, "TimeOfDay", "Night", DEVICE_NO)
        return true
    end
end

and in the scene that turns off the lights you need to put the code that runs when the light level is high:
Code: [Select]
if currentLevel >= HIGH_LEVEL then
    local timeOfDay = luup.variable_get (LS_SID, "TimeOfDay", DEVICE_NO) or "Night"
    if timeOfDay == "Day" then
        -- It's day, so the scene has run once.
        return false
    else
        -- It's morning, run the scene.
        luup.variable_set (LS_SID, "TimeOfDay", "Day", DEVICE_NO)
        return true
    end
end

Hope this helps.

Offline Ap15e

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1998
  • Karma: +12/-0
Re: Using HMS100 for sunrise / sunset
« Reply #10 on: February 24, 2011, 06:06:50 am »
DAD V0.1 with support for events will be available at http://forum.micasaverde.com/index.php?topic=5466.0 in a few hours.