Time based Lua script

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Time based Lua script

Post by cjnicholl » Thursday 29 January 2015 14:48

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?

User avatar
Mince Inside
Posts: 146
Joined: Wednesday 02 April 2014 20:59
Target OS: Raspberry Pi
Domoticz version:
Location: Scotland
Contact:

Re: Time based Lua script

Post by Mince Inside » Thursday 29 January 2015 15:38

Can you not change it to a shell script with JSON and stick it in cron?
Domoticz V2.2259
Raspberry Pi (B) with PiFace
RFXtrx433 USB
LighwaveRF relays, dimmer bulbs and light switches
Siemens On/Off 13A adaptors
Marmitek MS13E Motion (and Dusk) sensors

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Thursday 29 January 2015 15:50

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?

User avatar
Mince Inside
Posts: 146
Joined: Wednesday 02 April 2014 20:59
Target OS: Raspberry Pi
Domoticz version:
Location: Scotland
Contact:

Re: Time based Lua script

Post by Mince Inside » Thursday 29 January 2015 16:36

Can you post the lua script here?
Domoticz V2.2259
Raspberry Pi (B) with PiFace
RFXtrx433 USB
LighwaveRF relays, dimmer bulbs and light switches
Siemens On/Off 13A adaptors
Marmitek MS13E Motion (and Dusk) sensors

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Thursday 29 January 2015 17:46

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

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Thursday 29 January 2015 19:19

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??

User avatar
jannl
Posts: 955
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Geleen
Contact:

Re: Time based Lua script

Post by jannl » Friday 30 January 2015 11:28

Time >=12 is true between noon and midnight.

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Friday 30 January 2015 11:49

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"

User avatar
jannl
Posts: 955
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Geleen
Contact:

Re: Time based Lua script

Post by jannl » Friday 30 January 2015 13:02

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)

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Friday 30 January 2015 13:26

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.

User avatar
jannl
Posts: 955
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Geleen
Contact:

Re: Time based Lua script

Post by jannl » Friday 30 January 2015 14:25

It's timeofday['Nighttime'] and timeofday['Daytime'], not sunset/sunrise

See the wIKI

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Friday 30 January 2015 14:40

Ah - Ok.
Using daytime and nighttime has solved the original problem :D
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 8-)

D'rMorris
Posts: 314
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Time based Lua script

Post by D'rMorris » Friday 30 January 2015 15:42

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

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Friday 30 January 2015 16:16

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?

D'rMorris
Posts: 314
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Time based Lua script

Post by D'rMorris » Friday 30 January 2015 16:28

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

User avatar
jannl
Posts: 955
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Geleen
Contact:

Re: Time based Lua script

Post by jannl » Friday 30 January 2015 16:49

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/

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Friday 30 January 2015 16:53

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.

D'rMorris
Posts: 314
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Time based Lua script

Post by D'rMorris » Friday 30 January 2015 17:19

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:

Image

cjnicholl
Posts: 75
Joined: Thursday 06 November 2014 16:59
Target OS: Linux
Domoticz version: 2564
Contact:

Re: Time based Lua script

Post by cjnicholl » Friday 30 January 2015 18:00

From what I can see the script is working fine (morning changed to afternoon as expected) :D

I will definitely have a look at Sublime Text, at the moment I'm using a text editor in a linux terminal!

D'rMorris
Posts: 314
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Time based Lua script

Post by D'rMorris » Friday 30 January 2015 18:11

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests