Page 1 of 2
Time based Lua script
Posted: Thursday 29 January 2015 13:48
by cjnicholl
I have a time based Lua script that is working fine BUT I don't need it to update every minute, every 30 or 60 minutes would be fine.
Is it possible to change how often a time based Lua script will run?
Re: Time based Lua script
Posted: Thursday 29 January 2015 14:38
by Mince Inside
Can you not change it to a shell script with JSON and stick it in cron?
Re: Time based Lua script
Posted: Thursday 29 January 2015 14:50
by cjnicholl
Well I'd never written a script at all before an hour ago so anything's possible

Is it simple to convert one to another?
Re: Time based Lua script
Posted: Thursday 29 January 2015 15:36
by Mince Inside
Can you post the lua script here?
Re: Time based Lua script
Posted: Thursday 29 January 2015 16:46
by cjnicholl
Here you go:
Code: Select all
time = os.date("*t")
commandArray = {}
if (timeofday['Sunrise']) then
commandArray['Variable:Time-Variable']='Morning'
print("Time set to Morning")
end
if (time.hour >= 12) then
commandArray['Variable:Time-Variable']='Afternoon'
print("Time set to Afternoon")
end
if (timeofday['Sunset']) then
commandArray['Variable:Time-Variable']='Evening'
print("Time set to Evening")
end
if (time.hour >= 22) then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
end
return commandArray
Re: Time based Lua script
Posted: Thursday 29 January 2015 18:19
by cjnicholl
Nuts - The script isn't working.
Just checked the log and it hasn't set the Time-Variable to "Evening"
I assume this is because time.hour >= 12 is still true??
Re: Time based Lua script
Posted: Friday 30 January 2015 10:28
by jannl
Time >=12 is true between noon and midnight.
Re: Time based Lua script
Posted: Friday 30 January 2015 10:49
by cjnicholl
I tried Time = 12 and the script threw a wobbly. The only way I could get it to work was using Time >=12
What I am trying to achieve is:
Sunrise - Set Time-Variable to "Morning"
12.00 - Set Time-Variable to "Afternoon"
Sunset - Set Time-Variable to "Evening"
22.00 - Set Time-Variable to "Night"
Re: Time based Lua script
Posted: Friday 30 January 2015 12:02
by jannl
I think your script should work, as far as I can recall the last setting to commandArray is used.
Do you see the print statements in the logging?
May be you should use else if's (please check the syntax of else if, not sure and not behind my home system)
Re: Time based Lua script
Posted: Friday 30 January 2015 12:26
by cjnicholl
The print statements work (I log everything so I can see what is or isn't working).
The only two actions that are working are the change to afternoon at 12.00 and the change to night at 22.00 so it could be the sunrise/sunset syntax.
Re: Time based Lua script
Posted: Friday 30 January 2015 13:25
by jannl
It's timeofday['Nighttime'] and timeofday['Daytime'], not sunset/sunrise
See the wIKI
Re: Time based Lua script
Posted: Friday 30 January 2015 13:40
by cjnicholl
Ah - Ok.
Using daytime and nighttime has solved the original problem
Thanks!!
So far I have:
Code: Select all
time = os.date("*t")
commandArray = {}
if (timeofday['Daytime']) then
commandArray['Variable:Time-Variable']='Morning'
print("Time set to Morning")
end
if (time.hour >= 12) then
commandArray['Variable:Time-Variable']='Afternoon'
print("Time set to Afternoon")
end
if (timeofday['Nighttime']) then
commandArray['Variable:Time-Variable']='Evening'
print("Time set to Evening")
end
if (time.hour >= 22) then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
end
return commandArray
New problem is that the variable changes twice each time because (of course) both "if" statements are true

Re: Time based Lua script
Posted: Friday 30 January 2015 14:42
by D'rMorris
That's where the elseif statement comes in

, which exits at the first condition that is true. When using only IF's, then you can have indeed multiple true outcomes.
What you would want to use is:
IF
THEN
ELSEIF
THEN
END
I rewrote your code a bit, see below. Please note: you'll still have an issue for the time between 00:00 and the daytime the next day.
Now, this time falls in category "evening" (because it is nighttime and before 22:00). Your "time.hour >= 22" is only valid until 00:00, after that a new day starts and then the condition "nighttime" becomes true and that sets your variable to "evening". That should be fixed with the last elseif.
Code: Select all
time = os.date("*t")
commandArray = {}
if (timeofday['Daytime']) and (time.hour < 12)
then
commandArray['Variable:Time-Variable']='Morning'
print("Time set to Morning")
elseif (timeofday['Daytime']) and (time.hour >= 12)
then
commandArray['Variable:Time-Variable']='Afternoon'
print("Time set to Afternoon")
elseif (timeofday['Nighttime']) and (time.hour < 22)
then
commandArray['Variable:Time-Variable']='Evening'
print("Time set to Evening")
elseif (timeofday['Nighttime']) and (time.hour >= 22)
then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
elseif (timeofday['Nighttime'])
then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
end
return commandArray
Re: Time based Lua script
Posted: Friday 30 January 2015 15:16
by cjnicholl
That's great - Thanks very much

I haven't found any reference to "ELSEIF" in the various Wikis...
What would happen if you just replaced:
Code: Select all
elseif (timeofday['Nighttime']) and (time.hour >= 22) then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
With:
Code: Select all
elseif (timeofday['Nighttime']) then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
Does it need both?
Re: Time based Lua script
Posted: Friday 30 January 2015 15:28
by D'rMorris
Well, that depends on what you want to do. timeofday['Nighttime'] and timeofday['Daytime'] are system-filled variables and they depend on the cosmos.
So, "nighttime" starts earlier during wintertime and later during summertime. The opposite goes for daytime of course.
In the summer, sometimes the "nighttime" only starts around 22:15 CET. This would render this statement useless (simply because it will never be true):
Code: Select all
elseif (timeofday['Nighttime']) and (time.hour < 22)
then
commandArray['Variable:Time-Variable']='Evening'
print("Time set to Evening")
So indeed, you could replace the last 2 ones with just 1. However, in the summer it might be the case that it switches from afternoon to night, simply because "nighttime" can start later than 22:00 during summer.
New script:
Code: Select all
time = os.date("*t")
commandArray = {}
if (timeofday['Daytime']) and (time.hour < 12)
then
commandArray['Variable:Time-Variable']='Morning'
print("Time set to Morning")
elseif (timeofday['Daytime']) and (time.hour >= 12)
then
commandArray['Variable:Time-Variable']='Afternoon'
print("Time set to Afternoon")
elseif (timeofday['Nighttime']) and (time.hour < 22)
then
commandArray['Variable:Time-Variable']='Evening'
print("Time set to Evening")
elseif (timeofday['Nighttime'])
then
commandArray['Variable:Time-Variable']='Night'
print("Time set to Night")
end
return commandArray
Re: Time based Lua script
Posted: Friday 30 January 2015 15:49
by jannl
Slimste is om allebei te controleren. Of goed checken of op jou locatie ook de 22;00 gehaald wordt.
Als ik goed zoek, wordt het in Geleen bijvoorbeeld net niet 22:00 voor wat betreft zonsondergang, ligt een beetje eraan hoe Domoticz dat doet.
Gebruikte site:
http://www.zonsondergang-tijden.nl/
Re: Time based Lua script
Posted: Friday 30 January 2015 15:53
by cjnicholl
The idea was for "Morning" & "Evening" to be flexible based on sunrise & sunset times (the variables are going to form the backbone for some automated outdoor lighting).
In the UK sunset doesn't get any later than about 21:20 in the summer so the "Evening" setting would still work, however briefly
I had this working in Blockly but decided it was high time I learned some basic scripting.
Thanks for all your help.
Re: Time based Lua script
Posted: Friday 30 January 2015 16:19
by D'rMorris
jan_nl wrote:Slimste is om allebei te controleren. Of goed checken of op jou locatie ook de 22;00 gehaald wordt.
Als ik goed zoek, wordt het in Geleen bijvoorbeeld net niet 22:00 voor wat betreft zonsondergang, ligt een beetje eraan hoe Domoticz dat doet.
Gebruikte site:
http://www.zonsondergang-tijden.nl/
Just an example. In Sittard (hi, neighbor

) I have the same cosmos times as you, but in other countries (we have users in Sweden for example), the times may be different.
By the way, I think I had some sunsets later than 22:00, but I'm not sure. Never mind, if you are sure that it'll never be later than 22:00, you're safe when using 22:00 as a hard deadline.
Is the script the way it is now OK for you? LUA is indeed the better option when you want to do more advanced processing. Blockly is very nice for the simple if-then-else statements, but complexer stuff resides better in LUA.
Tip: use Sublime Text 3 (or another editor) so that you can quickly check your code. Like this:

Re: Time based Lua script
Posted: Friday 30 January 2015 17:00
by cjnicholl
From what I can see the script is working fine (morning changed to afternoon as expected)
I will definitely have a look at Sublime Text, at the moment I'm using a text editor in a linux terminal!
Re: Time based Lua script
Posted: Friday 30 January 2015 17:11
by D'rMorris
Did that as well before, but typing in an editor that gives you visual feedback and even maybe syntax checks works so much better!
After you're done, simply copy paste to terminal and check Domoticz logs. If there are errors, it gives you the line number which you can then check in your editor.