Page 1 of 2

Wake up Light

Posted: Thursday 13 February 2014 23:28
by Mavy
Hi Everyone,

I was wondering if anyone has managed to create some sort of wake up light using domoticz yet. I tried to create one but all my efforts so far have failed.

At first i tried to use a blocky based event, however I seem to be missing a basic function in the blocky event builder. That function being a delay between set actions.
Then i tried to use lua, however this hangs the entire domoticz process due to it not being forked as a separate thread (for which i will file a bug later because imo thats a major flaw).
After this i attempted to use the bash script, however this would need an extensive script to fetch device status using curl/json (which seems to lack support to fetch a single device?). Unless its the only option i am not yet willing to try this due to the load it will put om my poor raspberry pi :?

Are there any other ideas/solutions to doing this?
I played with the idea of creating several virtual devices to trigger each new level but that would create a mess of dummy switches and i cannot think of anyway to add the delay between levels.

Regards,
Mavy

Re: Wake up Light

Posted: Friday 14 February 2014 5:04
by alfred_j_kwak

Re: Wake up Light

Posted: Friday 14 February 2014 9:05
by Mavy
I did see that thread but that script kind of defeats the purpose of domoticz. Plus it has a similar issue as the bash script i mentioned earlier. There is no decent way to get a single device status using json.

Re: Wake up Light

Posted: Friday 14 February 2014 23:16
by simonrg
@Mavy, please could you share the scripts you have tried, this will make it easier to help :)

Re: Wake up Light

Posted: Saturday 15 February 2014 3:38
by Mavy
The original script i tried was:

Code: Select all

commandArray = {}
if (devicechanged['Bedroom Rene Wakeup'] == 'On') then
     if (otherdevices['Bedroom Rene Light'] == 'Off') then
             print("Wake up light on!")
             for i = 1, 16, 1 do
                     print("Setting Level: "..i)
                     oss = "curl 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=4&switchcmd=Set+Level&level="..i.."'&"
                     os.execute(oss)
                     os.execute("sleep 60")
             end
     end
     commandArray['Bedroom Rene Wakeup'] = 'Off'
end
However domoticz would hang completely during the sleep. My guess is that the lua code gets run inside the main process loop instead of as a fork/thread.
I then tried a bash script but its hard to process all the json code returned by the json scripts.

Currently i am planning to use the following:

Code: Select all

local http = require("socket.http")
JSON = (loadfile "JSON.lua")()

local function get_status(idx)
        url = 'http://127.0.0.1:8080/json.htm?type=devices&filter=light&used=true&order=Name'

        b, c, h = http.request(url)
        local o = JSON:decode(b)

        for key,value in pairs(o["result"]) do
                if value["idx"] == idx then
                        return value["Status"]
                end
        end
end

local function set_device(idx, level)
        seturl = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx='..idx..'&switchcmd=Set+Level&level='..level
        b, c, h = http.request(seturl)
end

local function wait(seconds)
        local _start = os.time()
        local _end = _start+seconds
        while (_end ~= os.time()) do
        end
end

status = get_status("4")
if status ~= "Off" then return end

for i = 2, 16, 1 do
        print("Setting Level: "..i)
        set_device("4", i)
        wait(120)
        status = get_status("4")
        if status == "Off" or status == "On" then print (status) return end
end
I plan on running this from the os lua. Probably using the domoticz_main script. I just need to test if this will hang the rest of the process as well. If it does i am out of ideas.

Re: Wake up Light

Posted: Saturday 15 February 2014 9:12
by simonrg
Nice, thanks, what does JSON.lua have in it?

I have changed your script from a device to a time script, so every 60 seconds Domoticz will check the state of the wakeup switch, if it is on then, Domoticz will increase the light level by 1 for every 60 seconds since the wakeup switch was turned on, until at 16 minutes it will turn the wakeup switch off. So while Domoticz will check the script every 60 seconds all day nothing will happen unless the wakeup switch is on.

I haven't tested this script, so hope there aren't too many errors.

Code: Select all

-- ~/domoticz/scripts/lua/script_time_wakeup.lua
-- If the wakeup switch is on
-- Then increases the lighting by 1 level every 60 seconds
-- After 8 minutes turns the wakeup switch off

function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

commandArray = {}

if (otherdevices['Bedroom Rene Wakeup'] == 'On') then
     wake_time = timedifference(otherdevices_lastupdate['Bedroom Rene Wakeup'])            
     level = 1 + wake_time % 60
     if(level <= 16) then
         commandArray['Bedroom Rene Light'] = 'Set Level '..level
     else
        commandArray['Bedroom Rene Wakeup'] == 'Off'
     end
end

return commandArray
Thanks for the chance to think about this challenge, it has now inspired me to generalise this code, so if I create a switch called "Wakeup Room Name" then the Lua script will get Domoticz to check all "Wakeup ....." switches, and do the level changing on each "Wakeup...." switch which is on. I have already written similar code for PIRs, so it is just a matter of adding the bits together and then we have the ultimate "Wakeup...." switch ;) . So then any light could have a wakeup sequence just by creating a new "Wakeup Room Name" switch in Domoticz. See http://www.domoticz.com/forum/viewtopic ... 348&p=9003

Re: Wake up Light

Posted: Saturday 15 February 2014 10:44
by Mavy
Oh nice idea, i hadn't thought of using the last update. The json code is here http://regex.info/blog/lua/json

Re: Wake up Light

Posted: Saturday 15 February 2014 11:44
by simonrg
Thanks for JSON reference.

I have now written a quick general version - the Ultimate Smart WakeUp (tm) :lol: - "WakeUpxroom name" - when switched on will increase the level of "room name" linearly over 800 seconds until the light is fully on, at which point "WakeUpxroom name" will be switched off. x^2 is the number of minutes the wake-up will take, not sure this is ideal, but from 1, 4, 9, 16, 25, 36, 49, 64 and 81 minutes give a number of choices anyway.

Code: Select all

-- ~/domoticz/scripts/lua/script_time_wakeup.lua
-- There is no error checking, so case of letters, spacing and correct characters in locations are key
-- If it doesn't work check for errors in the log
-- This script needs two switches to exist in Domoticz
-- A switch for the room with any legitimate Domoticz name "Room Name"
-- A dummy switch to start the wake-up sequence - "WakeUpxRoom Name"
-- Where x should be replaced by a single number and determines
-- number of minutes taken to reach full intensity
-- Time = x^2 * 60 seconds, so 4 is 16 minutes, 5 is 25 minutes etc..
-- e.g. Setting the WakeUp4Room Name" switch to on
-- Then the light intensity increases 1/16 every 60 seconds
-- After 16 minutes turns the wakeup switch off
            
function timedifference(s)
   print("s "..s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   print("t1 "..t1)
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   print("t2 "..t2)
   difference = os.difftime (t1, t2)
   return difference
end

commandArray = {}
   
for i, v in pairs(otherdevices) do
   tc = tostring(i)
   v = i:sub(1,6)
   if (v == 'WakeUp') then
      if (otherdevices[tc] == 'On') then
         time_range = 60 * tonumber(i:sub(7,7))^2
         print("Time range "..time_range)
         wake_time = timedifference(otherdevices_lastupdate[tc])
         print("Wake Time "..wake_time)
         level = 100 * (wake_time / time_range)
         print(level)
         if(level <= 100) then
            print('Set Level '..string.format("%u",level))
            commandArray[tc:sub(8)] = 'Set Level '..string.format("%u",level)
         else
            commandArray[tc:sub(8)] = 'Set Level 100'
            commandArray[tc] = 'Off'
         end
      end
   end
end

return commandArray

Remove all the print statements when you are happy the script is working correctly.

Re: Wake up Light

Posted: Saturday 15 February 2014 12:12
by Mavy
I would add a check in the loop to check if a light is Off before starting to dim it. This is to prevent the wakeup from starting if the light is already On.
Another thing i would do is add a condition to stop the wake up sequence if the light is turned Full On or Off (So your able to turn the "alarm" off when your awake)

Re: Wake up Light

Posted: Saturday 15 February 2014 13:21
by simonrg
Thanks, good additions, I am using LightwaveRF components, so there is no feedback, hence status in Domoticz is not necessarily the state of the light, so I probably won't add the checks on light condition.

To turn the alarm off, it would be cleanest to set the WakeUp switch to off, so a check in the script for state at 100% which would then set WakeUp off. In the case of LightwaveRF in order to ensure the light was really on I would need to send a 100% on command, so on somedays you would get a rapid wake-up ;) .

Re: Wake up Light

Posted: Tuesday 20 October 2015 20:35
by havnegata
I've been looking for this script a long time (all I do is steal and modify :oops: ) and so was very glad when I found yours . I change the names of the switches and I get this error:

Error: EventSystem: C:\Domoticz\scripts\lua\script_time_wakeup.lua:27: syntax error near '=='

Any clues?

-- ~/domoticz/scripts/lua/script_time_wakeup.lua
-- If the wakeup switch is on
-- Then increases the lighting by 1 level every 60 seconds
-- After 8 minutes turns the wakeup switch off

function timedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end

commandArray = {}

if (otherdevices['Soverom Wakeup'] == 'On') then
wake_time = timedifference(otherdevices_lastupdate['Soverom Wakeup'])
level = 1 + wake_time % 60
if(level <= 16) then
commandArray['Dimmer soverom'] = 'Set Level '..level
else
commandArray['Soverom Wakeup'] == 'Off'
end
end

return commandArray

Re: Wake up Light

Posted: Tuesday 20 October 2015 21:11
by G3rard
Remove one = from line 27 so it is like below.

Code: Select all

commandArray['Soverom Wakeup'] = 'Off'
The error message gives the line number on which the error occurs, so 27 in this case. That makes finding the error a bit easier.

Re: Wake up Light

Posted: Tuesday 20 October 2015 21:27
by havnegata
Ok, thanks, that makes sense :-)
When I try this, the error is gone, but the only thing that happens with the script is that the dummy switch is turned off.. Any more clues?

Re: Wake up Light

Posted: Tuesday 20 October 2015 22:49
by Pjedr
I created a wakeuplight also in bash, via dummy switch on action: script:///var/ramdrive/scripts/WakeUpLightStart.sh

WakeUpLightStart.sh (This script calls the other script, so no 10 seconds limit)

Code: Select all

#!/bin/bash
if [ -e "/var/ramdrive/domoticz" ]; then SCRIPTSDIR="/var/ramdrive/scripts"; else SCRIPTSDIR="/opt/scripts-test"; fi;
echo Starting Ambilight script; $SCRIPTSDIR/WakeUpLight.sh >/dev/null &
WakeUpLight.sh

Code: Select all

#!/bin/bash
if [ -e "/var/ramdrive/domoticz" ]; then TOOLSDIR="/var/ramdrive/tools"; else TOOLSDIR="/home/peter/milight"; fi
# switch ON, WHITE, but to current state, that might be too high...  switch to lowest dim
zone2=2; $TOOLSDIR/milight $zone2 W; $TOOLSDIR/milight $zone2 B 1; sleep 0.2s; zone3=3; $TOOLSDIR/milight $zone3 W; $TOOLSDIR/milight $zone3 B 1
# change brightness from 1 to 19 in 20 steps of 5 seconds
for i in `seq 10`; do $TOOLSDIR/milight $zone2 B $i; sleep 0.2s; $TOOLSDIR/milight $zone3 B $i; sleep 30s; done
sleep 300; $TOOLSDIR/milight $zone2 B 1; $TOOLSDIR/milight $zone2 OFF; sleep 0.2s
$TOOLSDIR/milight $zone3 B 1; $TOOLSDIR/milight $zone3 OFF # lowering dim for the next morning and switching off after 300s
echo -e "\nVersturen van statuswijziging naar Domoticz server via json:"; sDomoticzServer="192.168.192.68:8080"
echo curl -s -i -m 5 -H "Accept: application/json" "http://$sDomoticzServer/json.htm?type=command&param=switchlight&idx=617&switchcmd=Off"
strCurl=`nice -n 10 curl -s -i -m 5 -H "Accept: application/json" "http://$sDomoticzServer/json.htm?type=command&param=switchlight&idx=617&switchcmd=Off"`
Same trick i use in lua scripts (the & starts a new console to run that command in, the current console ends in the 10 seconds limit):

Code: Select all

os.execute('nohup '..sScriptsDir..'/TelegramBot.sh "'..sMsgTo..' '..sMsg..'" 2>/dev/null &')

Re: Wake up Light

Posted: Thursday 18 February 2016 11:49
by Mrrodz
Hi, did you get this working? Ive tried my self with no joy.
Fancy using it to cycle each light through 0-100-0% as a test. I currently have a lightwave system but struggling to get it working. Thanks


Sent from my iPhone using Tapatalk - now Free

Re: Wake up Light

Posted: Thursday 18 February 2016 14:51
by Evelen
It is well possible in principle to make in Blockly.
Extremely many OPEN JSON URL blocks consecutively
With the same or just a little bit more dim every time.
Tested with 10-20 blocks and it works, but it's over in seconds.
One can, however, make it extremely long, but it might get out of the performance in Domoticz or?
I'm running on a real server though, not Pi.

Re: Wake up Light

Posted: Tuesday 01 March 2016 15:17
by maomanna
I created a wiki several weeks ago how to make a wakeuplight with blockly.
Pretty long, but not an issue for a rpi!

Http://domoticz.com/wiki/wakeuplight

Re: Wake up Light

Posted: Tuesday 01 March 2016 19:04
by Evelen
maomanna wrote:I created a wiki several weeks ago how to make a wakeuplight with blockly.
Pretty long, but not an issue for a rpi!

Http://domoticz.com/wiki/wakeuplight
super.
btw. Can't other events run at the same time this is running?

Re: Wake up Light

Posted: Wednesday 02 March 2016 12:00
by maomanna
Yes they can, but if you use the same devices, it can overrun each other.

Re: Wake up Light

Posted: Friday 06 May 2016 0:06
by Derik
Dear all...
What is the way/script to use a wake up and a sleep down light?
I see multiple options..
The best and simple way....