Lua scripts for newbies

Moderator: leecollings

Post Reply
d3rax
Posts: 14
Joined: Thursday 25 February 2016 20:16
Target OS: -
Domoticz version:
Contact:

Lua scripts for newbies

Post by d3rax »

Hello guys,

I've been looking around the forum and the wiki for info on lua scripts. I looked at numerous examples, but I'm missing the basic starting steps. Really hope somebody can give me some step by step directions because I can't figure out where to start?

I want to make the following script:

I have a philips hue lamp that I want to control with a philips hue tap and an coco wall switch. The hue lamp and hue tap are already directly connected through the hue bridge and don't make part of the script. What I want is that after I have turned the hue lamp on with the hue tap I can turn the hue lamp off with coco switch with 1 flick of the switch. So the lamp has to be turned off after it receives a off OR a on command of the coco switch (because the coco switch can be in a off or a on position).

Without the script it can be possible you have to flick the switch twice to sent the off command because the switch was already in the off positon


I think this has to be done with a dummy switch. I was thinking of the following scripts:

---turn dummy switch on for a few seconds when coco switch is turned on or off
if ((otherdevices['coco switch'] == 'On') or (otherdevices['coco switch'] == 'Off')) then
(otherdevices['dummy switch'] == 'On') for 3 seconds
end

--check status of the hue lamp and turn it on or off after dummy switched has turned on
if ((otherdevices['hue lamp'] == 'On') and (otherdevices['dummy switch'] == 'On')) then
(otherdevices['hue lamp'] == 'Off')
else if ((otherdevices['hue lamp'] == 'Off') and (otherdevices['dummy switch'] == 'On')) then
(otherdevices['hue lamp'] == 'On')
end

Is this the rightway? And how do I implement this correctly in a lua script that is recognized by Domoticz?

Really hope you can point me in the right directions!

Best regards,

d3rax
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua scripts for newbies

Post by simonrg »

You need to read the wiki throughly to understand the mechanism by which Domoticz takes commands from Lua and look at some of the examples on the wiki - http://www.domoticz.com/wiki/Events, a good source to understand Lua is http://www.lua.org where you can find the difference between == and =.

In terms of your scripts, I am not quite sure about the logic of your script as I don't know how a Hua Tap works, but have adapted your scripts to be syntactically correct for Lua:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_hue1.lua
commandArray = {}
---turn dummy switch on for a few seconds when coco switch is turned on or off
if ((otherdevices['coco switch'] == 'On') or (otherdevices['coco switch'] == 'Off')) then
    commandArray['dummy switch'] = 'On FOR 3'
 end
 return commandArray

Code: Select all

-- ~/domoticz/scripts/lua/script_device_hue2.lua
commandArray = {}
--check status of the hue lamp and turn it on or off after dummy switched has turned on
if ((otherdevices['hue lamp'] == 'On') and (otherdevices['dummy switch'] == 'On')) then
    commandArray['hue lamp'] = 'Off'
else if ((otherdevices['hue lamp'] == 'Off') and (otherdevices['dummy switch'] == 'On')) then
    commandArray['hue lamp'] = 'On'
 end
 return commandArray
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
mrf68

Re: Lua scripts for newbies

Post by mrf68 »

As far as I understand the use of LUA, there are a few problems with the scripts mentioned.

There are 2 types of lua scripts: script_device_xyz.lua and script_time_xyz.lua. Script_device_xyz will run whenever a status is updated within Domoticz, it doesn't matter which device updated its status. Script_time_xyz will run every minute.

Then there is a difference between 'otherdevices' and 'devicechanged'. When you want to run the script when a device changes its status, you use 'devicechanged'. The 'otherdevices' statement checks the status of the device and you can let the script do something when the status is what you want it to be.

So, in the first script

Code: Select all

if ((otherdevices['coco switch'] == 'On') or (otherdevices['coco switch'] == 'Off')) then
  
.. the problem is that you ask Domoticz if the status of the switch is on or off. That will always be true, because it is on or it is off. So from the first moment you let Domoticz use this script, after whatever status is updated, this will be true. So it will act as follows:

Code: Select all

 commandArray['dummy switch'] = 'On FOR 3'
 
... it will turn the dummy switch on. Not for 3 seconds, but for 3 minutes (at least at my system, when I tested this). And because the status of this dummy switch is updated, the script will run again. And because the if statement is always true, it will fire again. And again, and again.....

So you need to change the first line to:

Code: Select all

if ((devicechanged['coco switch'] == 'On') or (devicechanged['coco switch'] == 'Off')) then
If it doesn't matter if it is switching from on to off or from off to on, you can simply state:

Code: Select all

if (devicechanged['coco switch']) then
So eventually the script looks like this:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_hue1.lua
commandArray = {}
---turn dummy switch on for a few seconds when coco switch is turned on or off
if (devicechanged['coco switch']) then
    commandArray['dummy switch'] = 'On FOR 1'
 end
 return commandArray
The second script also needs to be changed:

Code: Select all

otherdevices['dummy switch'] 
 
... needs to change to:

Code: Select all

devicechanged['dummy switch'] 
.. both in the IF and ELSEIF statement.

I did some testing and the second script still didn't work. I changed some things and then it works. So the second script now looks like:

Code: Select all

-- ~/domoticz/scripts/lua/script_device_hue2.lua
commandArray = {}
--check status of the hue lamp and turn it on or off after dummy switched has turned on
if devicechanged['dummy switch'] == 'On' and otherdevices['hue lamp'] == 'On' then
    commandArray['hue lamp'] = 'Off'
elseif devicechanged['dummy switch'] == 'On' and otherdevices['hue lamp'] == 'Off' then
    commandArray['hue lamp'] = 'On'
 end
 return commandArray
If I'm wrong, please tell me.
Eduard
Posts: 139
Joined: Monday 19 January 2015 9:14
Target OS: -
Domoticz version:

Re: Lua scripts for newbies

Post by Eduard »

Have a look at dzVents http://www.domoticz.com/forum/viewtopic ... 200#p77458

Easy to use LUA scripting...
d3rax
Posts: 14
Joined: Thursday 25 February 2016 20:16
Target OS: -
Domoticz version:
Contact:

Re: Lua scripts for newbies

Post by d3rax »

Thanx for the quick support! This really helps understanding the LUA scripting better. I will test the improved script tonight!
Thomasdc
Posts: 133
Joined: Wednesday 11 March 2015 19:13
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Lua scripts for newbies

Post by Thomasdc »

Hi everybody!

i am having some trouble with a lua script using strings and uservariables

i have a working IF- statement:

Code: Select all

if devicechanged['Call']=='On' and otherdevices_svalues['FritzBox'] == "Connected ID: 0 Number: 0496xxxxxx"  then
but now i want to split it up and use uservariables for the phonenumber like this:

Code: Select all

if devicechanged['Call']=='On' then 
    local text = tostring("Connected ID: 0 Number:"..uservariables['telnumber']..)
    if  otherdevices_svalues['FritzBox'] == text  then
but this is not working, I allready tried several things but so far without luck..

(i want to do it this way because i want to be able to add multiple phone numbers and im im hoping to make a for loop for it (checking the different uservariables)
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua scripts for newbies

Post by jvdz »

You probably are getting an error for this line in LUA ....right?:
local text = tostring("Connected ID: 0 Number:"..uservariables['telnumber']..)

You shouldn't end with the ".." as that expects another string to concatenate.
There is probably also a space missing after "Number:" ?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Thomasdc
Posts: 133
Joined: Wednesday 11 March 2015 19:13
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Lua scripts for newbies

Post by Thomasdc »

@ jvdz,

Thanks a lot! that was it indeed!

for people who also want to explore with uservariables this is the little code i wrote:

i have 10 user variables: t1, t2, ... t10
which each can contain a phonenumber
the scrip checks if one of these numbers calls the answeringmachine

commandArray = {}

Code: Select all

if devicechanged['Call']=='On' then 
  local i = 1
    for i =1 , 10 do
        local number = tostring("t"..i)   
        local FrizBoxText = tostring("Connected ID: 0 Number: "..uservariables[number])
        if  otherdevices_svalues['FritzBox'] == FrizBoxText  then
            print(FrizBoxText)
            commandArray['Bureau R 15'] = "On"
        end
        i = i+1
    end
end
return commandArray
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest