Script examples (Lua)
Moderator: leecollings
- mbliek
- Posts: 194
- Joined: Friday 12 July 2013 14:08
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: the Netherlands
- Contact:
-
- Posts: 612
- Joined: Wednesday 07 August 2013 19:09
- Target OS: -
- Domoticz version:
- Location: UK
- Contact:
Re: Script examples (Lua)
Hi, great scripts!!
I am looking to open and close my curtains at sunrise and sunset. I have an iTach flex to do this as the curtains are infrared. I have been given a script which is python based to send the required codes to the flex.
Can I get Domoticz to run this script at sunrise, and a different one at sunset (one with close commands and one with one commands. The script is below:-
#!/usr/bin/python
import socket
HOST2 = '<addressOfGCUnit>'
PORT2 = <portOfGCUnit>
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST2, PORT2))
s.sendall("<commandCode>\r")
response = s.recv(24) #This is the echo back from iTach
print "Response from send(<whateverStringYouWantToGetBack-OPTIONAL>):", response
Cheers people
I am looking to open and close my curtains at sunrise and sunset. I have an iTach flex to do this as the curtains are infrared. I have been given a script which is python based to send the required codes to the flex.
Can I get Domoticz to run this script at sunrise, and a different one at sunset (one with close commands and one with one commands. The script is below:-
#!/usr/bin/python
import socket
HOST2 = '<addressOfGCUnit>'
PORT2 = <portOfGCUnit>
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST2, PORT2))
s.sendall("<commandCode>\r")
response = s.recv(24) #This is the echo back from iTach
print "Response from send(<whateverStringYouWantToGetBack-OPTIONAL>):", response
Cheers people

- Dynamic
- Posts: 109
- Joined: Friday 12 July 2013 14:50
- Target OS: -
- Domoticz version:
- Location: Enschede
- Contact:
Re: Script examples (Lua)
I did that alreadymbliek wrote:Than you need to change 8080 to 80

As workaround I renamed my iPhone, so it doesn't have a name with spaces in it. The script is working now!
Now i'm struggling to create a cronjob. I did cron -e and added this line:
Code: Select all
1 * * * * /ipresence/bluepresence.sh
- Keptenkurk
- Posts: 103
- Joined: Wednesday 21 August 2013 17:24
- Target OS: -
- Domoticz version:
- Location: Waalre, The Netherlands
- Contact:
Re: Script examples (Lua)
@simon
The way of IP communication in the python example is pretty low level and i wouldn't know how to integrate that (not saying that it cannot be done).
I'm not familiar with the iTach products (or your specific model) but i got this http API description from their site (scroll down to IR command transmission and IR Learning).
If you can use these to talk to your iTach device from your browser then scripting this is no issue.
The way of IP communication in the python example is pretty low level and i wouldn't know how to integrate that (not saying that it cannot be done).
I'm not familiar with the iTach products (or your specific model) but i got this http API description from their site (scroll down to IR command transmission and IR Learning).
If you can use these to talk to your iTach device from your browser then scripting this is no issue.
-
- Posts: 612
- Joined: Wednesday 07 August 2013 19:09
- Target OS: -
- Domoticz version:
- Location: UK
- Contact:
Re: Script examples (Lua)
Keptenkurk
Thank you for your reply. I have never script before. I'm currently waiting for a replacement flex, once I have it back up and running I'll attempt to write a new script.
Thanks again.

Thank you for your reply. I have never script before. I'm currently waiting for a replacement flex, once I have it back up and running I'll attempt to write a new script.
Thanks again.

- Keptenkurk
- Posts: 103
- Joined: Wednesday 21 August 2013 17:24
- Target OS: -
- Domoticz version:
- Location: Waalre, The Netherlands
- Contact:
Re: Script examples (Lua)
Neither did i before last month. But this forum and Google provide a lot! Take your time and have fun!
Re: Script examples (Lua)
Prescence detection using two motion detectors:
This is a script I use to determine if anyone is still upstairs, it is a time based script that runs every minute and it uses two motion detectors to determine if there is any movement upstairs, and which direction (up or down) the last person was heading, if there is no motion upstairs, and the last person was heading downstairs it turns off all lights in the group "scene:lights_upstairs" when 30 minutes have passed.
One motion sensor is located on the top floor, and the other one in the staircase and the timedifference between their lastupdated timestamps determines direction.
there is also a manual switch downstairs that turns on the ceiling light on the top floor, and to allow time for someone to make it up the stairs I never turm off the lights if the ceiling light was switched on witin the last two minutes (120s), if you turn on the ceiling light upstairs, but never go up, it will automatically turn off the next time the script runs after two minutes.
this script can sometimes be annoying, if I am in my reading chair upstairs without moving for more than 30minutes, and my wife goes downstairs all lights will be turned off.
I therefore have another script that checks if there is motion upstairs and if the lights where switched within the last 30s, and then turns them on again, that way I don't have to get up, I just wave my hand and the lights come back on.
This is a script I use to determine if anyone is still upstairs, it is a time based script that runs every minute and it uses two motion detectors to determine if there is any movement upstairs, and which direction (up or down) the last person was heading, if there is no motion upstairs, and the last person was heading downstairs it turns off all lights in the group "scene:lights_upstairs" when 30 minutes have passed.
One motion sensor is located on the top floor, and the other one in the staircase and the timedifference between their lastupdated timestamps determines direction.
there is also a manual switch downstairs that turns on the ceiling light on the top floor, and to allow time for someone to make it up the stairs I never turm off the lights if the ceiling light was switched on witin the last two minutes (120s), if you turn on the ceiling light upstairs, but never go up, it will automatically turn off the next time the script runs after two minutes.
Code: Select all
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 = {}
print('evaluating if someone is upstairs')
if (otherdevices['topfloor_motion'] == 'Off') and (timedifference(otherdevices_lastupdate['topfloor_ceiling_lamp']) > 120) then
if (timedifference(otherdevices_lastupdate[topfloor_motion']) > 1800) then
if timedifference(otherdevices_lastupdate['top_stairs_motion']) < timedifference(otherdevices_lastupdate['topfloor_motion']) then
commandArray['Scene:topfloor_lights']='Off'
print('turning off, no movement upstairs for more than 30m')
end
end
end
this script can sometimes be annoying, if I am in my reading chair upstairs without moving for more than 30minutes, and my wife goes downstairs all lights will be turned off.
I therefore have another script that checks if there is motion upstairs and if the lights where switched within the last 30s, and then turns them on again, that way I don't have to get up, I just wave my hand and the lights come back on.
-
- Posts: 110
- Joined: Friday 20 September 2013 18:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2.3530
- Location: Finland
- Contact:
Re: Script examples (Lua)
This is maybe more like bash related thing, but I wrote script to check once in a minute if my phone is in range of domoticz server bluetooth. Now I get event once in a minut. I liked to get event only when state changes. Do I have to write current status into file and read it back from file before checking my bluetooth so I could send JSON only when status is different than previous?
Sorry about bad english.
-Jussi-
Sorry about bad english.

-Jussi-
- l0gic
- Posts: 107
- Joined: Tuesday 08 October 2013 9:35
- Target OS: Linux
- Domoticz version: Latest
- Contact:
Re: Script examples (Lua)
Hi Jussi,
I don't know what phone you have but I use an Android phone and run the app called "AutomateIT"
This app runs tasks when certain criteria are met.
I set one up such that when I log on to my home wi-fi connect to a web site.
The web site I connect to is my local Domoticz server with a json command string to operate a dummy switch.
e.g.
http://192.168.2.100:8080/json.htm?type ... On&level=0
You can then wrap some logic around the switch to do what functions you require.
You can easily find the switch number by running this json command
http://192.168.2.100:8080/json.htm?type ... htswitches
HTH
Kevin
I don't know what phone you have but I use an Android phone and run the app called "AutomateIT"
This app runs tasks when certain criteria are met.
I set one up such that when I log on to my home wi-fi connect to a web site.
The web site I connect to is my local Domoticz server with a json command string to operate a dummy switch.
e.g.
http://192.168.2.100:8080/json.htm?type ... On&level=0
You can then wrap some logic around the switch to do what functions you require.
You can easily find the switch number by running this json command
http://192.168.2.100:8080/json.htm?type ... htswitches
HTH
Kevin
Non credus crepitus
- l0gic
- Posts: 107
- Joined: Tuesday 08 October 2013 9:35
- Target OS: Linux
- Domoticz version: Latest
- Contact:
Re: Script examples (Lua)
Hi
this is a script I've written to gradually turn off the lights at bedtime;
it relies on a dummy switch called Bedtime which is operated when it is time to go to bed.
It scrolls around to check of the Bedtime switch is on and if it is it will then keep checking for any lights left on, the time since the Bedtime switch was operated and then power off the lights at set intervals.
There are a number of print commands that I have left in as they are useful for debugging while setting the script up.
I also have a final email once all lights are off for the night, this can also be removed.
I have set up a link on my iPad home screen that sends a json command to turn on the Bedtime switch, other methods are available
Hope this helps someone.
Kevin
this is a script I've written to gradually turn off the lights at bedtime;
it relies on a dummy switch called Bedtime which is operated when it is time to go to bed.
It scrolls around to check of the Bedtime switch is on and if it is it will then keep checking for any lights left on, the time since the Bedtime switch was operated and then power off the lights at set intervals.
Code: Select all
t1 = os.time()
s = otherdevices_lastupdate['Bedtime']
-- returns a date time like 2013-07-11 17:23:12
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)
commandArray = {}
-- *** Get the difference between last Bedtime switch operate time and now
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
-- *** Check to see if Bedtime switch is on
if (otherdevices['Bedtime'] == 'On') then
td = "Time Difference = " .. difference
print (td)
print ("Device - Bedtime Switch is On")
-- *** Check to see if all lights are off, if so reset the Bedtime switch
if (otherdevices['Kevin Lamp'] == 'Off') and (otherdevices['Sharon Lamp'] == 'Off') and (otherdevices['Kitchen Main'] == 'Off') and (otherdevices['Kitchen Kettle'] == 'Off') and (otherdevices['Conservatory Lamp'] == 'Off') and (otherdevices['Outside Lights'] == 'Off') then
print ("All the lamps are Off")
commandArray['Bedtime']='Off'
commandArray['SendNotification']='Bedtime Alert#All lights Off - Resetting Bedtime switch'
end
-- *** Turn off Front Room lights after 60 seconds [1 min] (check duration to 180 seconds [3 min])
if difference > 60 and difference < 180 then
commandArray['Kevin Lamp']='Off'
commandArray['Sharon Lamp']='Off'
print ("Front room lights Off - 1 minute after activation")
end
-- *** Turn off Conservatory light and Kitchen Main after 300 seconds [5 min] (check duration to 420 seconds [7 min])
if difference > 300 and difference < 420 then
commandArray['Conservatory Lamp']='Off'
commandArray['Kitchen Main']='Off'
print ("Conservatory and Kitchen Main lights Off - 5 minutes after activation")
end
-- *** Turn off Kitchen Kettle lights after 600 seconds [10 min] (check duration to 720 seconds [12 min])
if difference > 600 and difference < 720 then
commandArray['Kitchen Kettle']='Off'
print ("Kitchen Kettle light Off - 10 minutes after activation")
end
-- *** Turn off Outside lights after 10800 seconds [3 hrs] (check duration to 10920 seconds [3 hrs 2 min])
if difference > 10800 and difference < 10920 then
commandArray['Outside Lights']='Off'
print ("Outside lights Off - 3 hours after activation")
end
end
return commandArray
I also have a final email once all lights are off for the night, this can also be removed.
I have set up a link on my iPad home screen that sends a json command to turn on the Bedtime switch, other methods are available

Hope this helps someone.
Kevin
Non credus crepitus
Re: Script examples (Lua)
I'm trying to update a gas (m3) style meter from a lua-script.
this however, doesn't work:
commandArray['OpenURL']="192.168.1.24:8080/json.htm?type=command¶m=udevice&hid=3&did=gas2&dunit=1&dsubtype=0&dtype=113&nvalue=0&svalue=500"
but if I execute the url separately from the browser it's fine. What is wrong or are there more elegant ways to do this (kinda strange using json from within domoticz)
this however, doesn't work:
commandArray['OpenURL']="192.168.1.24:8080/json.htm?type=command¶m=udevice&hid=3&did=gas2&dunit=1&dsubtype=0&dtype=113&nvalue=0&svalue=500"
but if I execute the url separately from the browser it's fine. What is wrong or are there more elegant ways to do this (kinda strange using json from within domoticz)
- gizmocuz
- Posts: 2394
- Joined: Thursday 11 July 2013 18:59
- Target OS: Raspberry Pi / ODroid
- Domoticz version: beta
- Location: Top of the world
- Contact:
Re: Script examples (Lua)
@maxtrash,
There are probably two problems with that last OpenURL call:
1. The IP address should be: 127.0.0.1:8080 if you want to update a value on that machine, if you send it to another domoticz, then please check your IP/Port, because i verified this, and this is working and not blocking
2. I do not know why you use all parameters but if your Gas device has index (idx) 10 for example (this is shown in the devices tab) you could do:
commandArray['OpenURL']="127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=10&nvalue=0&svalue=12.3;45;2"
(^Please not this nValue/sValue is for a Temp+Hum sensor, you need to change this)
New in version #1113
Instead of using OpenURL you can now issue:
commandArray['UpdateDevice']='10|0|23.4;44;3'
(It is separated by a Pipe, [idx][nvalue][svalue])
Hope this solves your problem!
With kind regards,
Rob
There are probably two problems with that last OpenURL call:
1. The IP address should be: 127.0.0.1:8080 if you want to update a value on that machine, if you send it to another domoticz, then please check your IP/Port, because i verified this, and this is working and not blocking
2. I do not know why you use all parameters but if your Gas device has index (idx) 10 for example (this is shown in the devices tab) you could do:
commandArray['OpenURL']="127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=10&nvalue=0&svalue=12.3;45;2"
(^Please not this nValue/sValue is for a Temp+Hum sensor, you need to change this)
New in version #1113
Instead of using OpenURL you can now issue:
commandArray['UpdateDevice']='10|0|23.4;44;3'
(It is separated by a Pipe, [idx][nvalue][svalue])
Hope this solves your problem!
With kind regards,
Rob
Quality outlives Quantity!
Re: Script examples (Lua)
Now we're talking! Great, I'll try it tonight using update device.
You're right about 127.0.0.1 probably, didn't think of it. However not relevant anymore I guess
You're right about 127.0.0.1 probably, didn't think of it. However not relevant anymore I guess

Re: Script examples (Lua)
something strange is happening.
I found out my original try didn't work because I was using " instead of '
out of these options everything works, except the new UpdateDevice though ?
update: OpenUrl works fine, except I can only send one URL in the script. If I put in two it will only execute the last one.
I found out my original try didn't work because I was using " instead of '

out of these options everything works, except the new UpdateDevice though ?
Code: Select all
if ((devicechanged['Elektriciteit'])) then
print ("Devicechanged elektriciteit")
print (otherdevices_svalues['Graaddagen'])
print (otherdevices_svalues['Gas'])
--commandArray['OpenURL']='192.168.1.24:8080/json.htm?type=command¶m=udevice&hid=3&did=gas2&dunit=1&dsubtype=0&dtype=113&nvalue=0&svalue=501'
commandArray['OpenURL']='192.168.1.24:8080/json.htm?type=command¶m=udevice&idx=64&nvalue=0&svalue=502'
--commandArray['OpenURL']='127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=64&nvalue=0&svalue=503'
--commandArray['UpdateDevice']='64|0|504'
end
- Keptenkurk
- Posts: 103
- Joined: Wednesday 21 August 2013 17:24
- Target OS: -
- Domoticz version:
- Location: Waalre, The Netherlands
- Contact:
Re: Script examples (Lua)
I want to share this script example. Not because it is really useful to many (and not to show that i know programming because i don't) but is contains some Lua things which might serve as an example in other scripts. At least i had fun in googeling it together and make it work.
Goal: I wanted to graph the value of the sun's x-ray activity in Domoticz. To have it on my dashboard knowing what has been going on the past 24h. I do this by reading a web page containing this value every minute, extracting the data and posting it into a Domoticz dummy temperature sensor.
and it works:
Goal: I wanted to graph the value of the sun's x-ray activity in Domoticz. To have it on my dashboard knowing what has been going on the past 24h. I do this by reading a web page containing this value every minute, extracting the data and posting it into a Domoticz dummy temperature sensor.
Code: Select all
-------------------------------------------------------------------------------
--- Lua Time script v1.0
--- Runs every minute
---
--- Purpose:
--- Reads solar xray flux value from website and graphs it in temperature graph
--- Background: http://en.wikipedia.org/wiki/Solar_flare
-------------------------------------------------------------------------------
--- returns num rounded up to idp decimals
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
commandArray = {}
--- To have a 1MB /var/tmp directory in RAM add the line
--- tmpfs /var/tmp tmpfs nodev,nosuid,size=1M 0 0
--- to the /etc/fstab file
---
--- Read Solar data from http://sol24.net/m/ into xray_data_string
tmp_filename = '/var'..os.tmpname()
os.execute('curl "http://sol24.net/m/" > '..tmp_filename)
infile = io.open(tmp_filename,'r')
xray_data_str=infile:read('*all')
--and clean up temp file
infile:close()
os.remove( tmp_filename )
-- Process xray_data_string to retrieve current x-ray value near label "flux:" which looks like C3.7
xray_pos = string.find(xray_data_str, "X-ray flux:")
xray_power10 = string.sub(xray_data_str,xray_pos+88, xray_pos+88)
xray_value= string.sub(xray_data_str,xray_pos+89, xray_pos+91)
if xray_power10 == 'B' then xray_value_powered = xray_value * 10
elseif xray_power10 == 'C' then xray_value_powered = xray_value * 100
elseif xray_power10 == 'M' then xray_value_powered = xray_value * 1000
elseif xray_power10 == 'X' then xray_value_powered = xray_value * 10000
elseif xray_power10 == 'Y' then xray_value_powered = xray_value * 100000
end
--transform this value to be presented on a logaritmic 0~50 scale
xray_index = 10*round(math.log(xray_value_powered,10),2)
--show what we are doing in the log
print(string.format("Current GOES Xray flux is %s%s indexed to %s.",xray_power10,xray_value,xray_index))
--and put the value in the graph
commandArray['UpdateDevice']='66|0|'..xray_index
--done
return commandArray
and it works:
- l0gic
- Posts: 107
- Joined: Tuesday 08 October 2013 9:35
- Target OS: Linux
- Domoticz version: Latest
- Contact:
Double Click
Hi guys,
after seeing a discussion on another thread about controlling two separate lights using one switch unit I thought I'd give it a go.
I don't particularly need it but I guess it could be useful.
The code is not elegant but sort of works.
The switch, Spare Byron Switch (B1), operates Ceiling Unit 15 (H15) on a single click i.e. normal conditions but also operates Ceiling Unit 16 (H16) using double click.
A double click is defined as two events within 5 seconds.
A file (SwitchB1Time.txt) is expected to exist in the Domoticz directory (not in the Domoticz\scripts or Domoticz\scripts\lua directories) and have data in 3 lines which is read and modified.
Line 1 holds the last switch On time
Line 2 holds the last switch Off time
Line 3 holds the last state of Ceiling Unit 15 (H15)
To seed the file enter;
12345
12346
Off
or similar. Real data will replace these once the code has run.
Note - this code is not perfect, if double clicking the single click unit will operate for the duration of the double click before being switched back to it's original state.
I may revisit this in a few days if I have some time.
Hope this helps someone.
Kevin
after seeing a discussion on another thread about controlling two separate lights using one switch unit I thought I'd give it a go.
I don't particularly need it but I guess it could be useful.
The code is not elegant but sort of works.

The switch, Spare Byron Switch (B1), operates Ceiling Unit 15 (H15) on a single click i.e. normal conditions but also operates Ceiling Unit 16 (H16) using double click.
A double click is defined as two events within 5 seconds.
A file (SwitchB1Time.txt) is expected to exist in the Domoticz directory (not in the Domoticz\scripts or Domoticz\scripts\lua directories) and have data in 3 lines which is read and modified.
Line 1 holds the last switch On time
Line 2 holds the last switch Off time
Line 3 holds the last state of Ceiling Unit 15 (H15)
To seed the file enter;
12345
12346
Off
or similar. Real data will replace these once the code has run.
Note - this code is not perfect, if double clicking the single click unit will operate for the duration of the double click before being switched back to it's original state.
I may revisit this in a few days if I have some time.
Code: Select all
commandArray = {}
-- ***** Switch On Section *****
if (devicechanged['Spare Byron Switch (B1)'] == 'On') then
tNew = os.time()
-- Get current state of single click light
if (otherdevices['Ceiling Unit 15 (H15)'] == 'Off') then
singleclicklamp = "Off"
else
singleclicklamp = "On"
end
-- Get last time switch was operated
local file = io.open("SwitchB1Time.txt", "r")
TimeOnOld = file:read()
TimeOffOld = file:read()
LightStateOld = file:read()
file:close()
-- Compare this on time with last on time and get the difference
On_Difference = tNew - TimeOnOld
Off_Difference = tNew - TimeOffOld
DoubleClick = "Null"
pton = "Time difference in seconds since last On operation = " .. On_Difference
print(pton)
ptoff = "Time difference in seconds since last Off operation = " .. Off_Difference
print(ptoff)
-- Check for Double Click
if (On_Difference < 5) then -- switched on twice within 5 seconds
DoubleClick = "True"
else
DoubleClick = "False"
end
pdc = "Double click event = " .. DoubleClick
print(pdc)
if (DoubleClick == "False") then
commandArray['Ceiling Unit 15 (H15)']='On'
end
if (DoubleClick == "True") then
commandArray['Ceiling Unit 16 (H16)']='On'
if (LightStateOld == "Off") then
commandArray['Ceiling Unit 15 (H15)']='Off'
end
end
-- save new time to file
local file = io.open("SwitchB1Time.txt", "w")
file:write(tNew)
file:write("\n")
file:write (TimeOffOld)
file:write("\n")
file:write(singleclicklamp)
file:close()
print ("File SwitchB1Time.txt written")
end
-- ***** Switch Off Section *****
if (devicechanged['Spare Byron Switch (B1)'] == 'Off') then
tNew = os.time()
-- Get current state of single click light
if (otherdevices['Ceiling Unit 15 (H15)'] == 'Off') then
singleclicklamp = "Off"
else
singleclicklamp = "On"
end
-- Get last time switch was operated
local file = io.open("SwitchB1Time.txt", "r")
TimeOnOld = file:read()
TimeOffOld = file:read()
LightStateOld = file:read()
file:close()
-- Compare this on time with last on time and get the difference
On_Difference = tNew - TimeOnOld
Off_Difference = tNew - TimeOffOld
DoubleClick = "Null"
pton = "Time difference in seconds since last On operation = " .. On_Difference
print(pton)
ptoff = "Time difference in seconds since last Off operation = " .. Off_Difference
print(ptoff)
-- Check for Double Click
if (Off_Difference < 5) then -- switched on twice within 5 seconds
DoubleClick = "True"
else
DoubleClick = "False"
end
pdc = "Double click event = " .. DoubleClick
print(pdc)
if (DoubleClick == "False") then
commandArray['Ceiling Unit 15 (H15)']='Off'
end
if (DoubleClick == "True") then
commandArray['Ceiling Unit 16 (H16)']='Off'
if (LightStateOld == "On") then
commandArray['Ceiling Unit 15 (H15)']='On'
end
end
-- save new time to file
local file = io.open("SwitchB1Time.txt", "w")
file:write(TimeOnOld)
file:write("\n")
file:write (tNew)
file:write("\n")
file:write(singleclicklamp)
file:close()
print ("File SwitchB1Time.txt written")
end
return commandArray
Kevin
Non credus crepitus
- l0gic
- Posts: 107
- Joined: Tuesday 08 October 2013 9:35
- Target OS: Linux
- Domoticz version: Latest
- Contact:
Script examples (Lua) - Getting date time variables
Hi,
I needed to pull some real time date and time variables while scripting so I knocked up a test script to investigate what is available.
In case anyone else needs to get the same type of info here is the script;
Hope this helps someone.
cheers
Kevin
I needed to pull some real time date and time variables while scripting so I knocked up a test script to investigate what is available.
In case anyone else needs to get the same type of info here is the script;
Code: Select all
-- Script to print out the various time and date variables available in Domoticz for reference
-- Output is in the log file [Setup -> Log from the menu]
-- also loads the values into variables for use elsewhere in the script if needed
-- If using this script delete the lines that you do not need to save resources
-- Not all variables have been presented but the full available list is below;
-- Variable details (from http://www.lua.org/pil/22.1.html)
-- %a abbreviated weekday name (e.g., Wed)
-- %A full weekday name (e.g., Wednesday)
-- %b abbreviated month name (e.g., Sep)
-- %B full month name (e.g., September)
-- %c date and time (e.g., 09/16/98 23:48:10)
-- %d day of the month [01-31]
-- %H hour, using a 24-hour clock [00-23]
-- %I hour, using a 12-hour clock [01-12]
-- %M minute [00-59]
-- %m month [01-12]
-- %p either "am" or "pm"
-- %S second [00-61]
-- %w weekday [0-6 = Sunday-Saturday]
-- %x date (e.g., 09/16/98)
-- %X time (e.g., 23:48:10)
-- %Y full year (1998)
-- %y two-digit year [00-99]
-- %% the character `%´
print(" ")
print ("*******************")
print(" ")
-- Year
nowyear = os.date("%Y")
print(os.date("Current Year - %Y"))
-- Month
nowmonth = os.date("%m")
print(os.date("Current Month - %m and %b and %B"))
--Day
nowday = os.date("%d")
nowdow = os.date("%a") -- day of week truncated (%A full)
print(os.date("Current day - %d and %a and %A"))
-- Hour
nowhour = os.date("%H")
print(os.date("Current hour (24h) - %H and (12h) - %I"))
-- Minute
nowminute = os.date("%M")
print(os.date("Current minute - %M"))
-- Second
nowsecond = os.date("%S")
print(os.date("Current second - %S"))
-- Full Date
nowdate = os.date("%x")
print(os.date("Current full date - %x"))
-- Full Time
nowtime = os.date("%X")
print(os.date("Current full time - %X"))
-- Full Date and Time
nowdatetime = os.date("%c")
print(os.date("Current full date and time - %c"))
print(" ")
print ("*******************")
print(" ")
commandArray = {}
-- nothing to see here
return commandArray
cheers
Kevin
Non credus crepitus
-
- Posts: 3
- Joined: Monday 09 December 2013 13:23
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: Script examples (Lua)
Hi,alfred_j_kwak wrote:This is maybe more like bash related thing, but I wrote script to check once in a minute if my phone is in range of domoticz server bluetooth. Now I get event once in a minut. I liked to get event only when state changes. Do I have to write current status into file and read it back from file before checking my bluetooth so I could send JSON only when status is different than previous?
Sorry about bad english.![]()
-Jussi-
Did you solve this? Trying to do the same thing.
-Ode
-
- Posts: 110
- Joined: Friday 20 September 2013 18:49
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 2.3530
- Location: Finland
- Contact:
Re: Script examples (Lua)
No - I give up. 
I still get events once in minute. With android phone you could use Tasker to send JSON, but still there could be 'misfires'. Tasker is able to trigger both ways. First when you enter into radius and another time when you left area.
-Jussi-

I still get events once in minute. With android phone you could use Tasker to send JSON, but still there could be 'misfires'. Tasker is able to trigger both ways. First when you enter into radius and another time when you left area.
-Jussi-
Who is online
Users browsing this forum: No registered users and 0 guests