Script examples (Lua)

tozzke
Posts: 134
Joined: Friday 02 January 2015 10:22
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script examples (Lua)

Post by tozzke » Friday 28 April 2017 11:29

Nautilus wrote:
ToneStrife wrote: I edit myslef, I have to select that is a Device Event not All. Anywhere I can find which should I select there?
Choose device script if you have a "devicechanged" trigger on the script ( = you wan the script to run on each device change) and select time script if you only have "otherdevices" conditions ("devicechanged is not available in time scripts) and you want to run the script once a minute. I see no reason to select "All" option in any circumstance but probably there is some special cases...:)
tozzke wrote:try changing it to:

Code: Select all

commandArray = {}
if (otherdevices['Gala'] == 'On' or otherdevices['Carlos'] == 'On') and otherdevices['Familia'] == 'Off' then
    commandArray['Familia'] = 'On'

elseif otherdevices['Gala'] == 'Off' and otherdevices['Carlos'] == 'Off' and otherdevices['Familia'] == 'On' then
    commandArray['Familia'] = 'Off'
end
return commandArray
This is a good option (except I think the first condition needs to be otherdevices['Gala'] == 'On' and otherdevices['Carlos'] == 'On' and second (otherdevices['Gala'] == 'Off' or otherdevices['Carlos'] == 'Off) => requirement for "Familia" was that both are home) if it is enough that "Familia" switch is updated with up to one minute delay. The above would need to be defined as time script. But I think the original should work as well as long as it is defined as device script...
No, you want the combined switch 'Familia' to turn on when one of both is at home and turn off when both aren't at home anymore.
If you's use this 'Familia' switch in another script to turn lights on and off, the light will go on when one of both is at home and will turn off when nobody is at home anymore. At least that's the way it usually needs to work, a combined switch which has all the family members in it.

What you suggest will result in that the lights will go on when both are at home and will go off when one of both left home, leaving the one at home in the dark.

Nautilus
Posts: 774
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Script examples (Lua)

Post by Nautilus » Friday 28 April 2017 11:45

tozzke wrote: No, you want the combined switch 'Familia' to turn on when one of both is at home and turn off when both aren't at home anymore.
If you's use this 'Familia' switch in another script to turn lights on and off, the light will go on when one of both is at home and will turn off when nobody is at home anymore. At least that's the way it usually needs to work, a combined switch which has all the family members in it.

What you suggest will result in that the lights will go on when both are at home and will go off when one of both left home, leaving the one at home in the dark.
I'm not saying whether this is clever or not, actually that is something we cannot comment as long as we don't know how ToneStrife wants to use the switch :) But I'm sure he has a good reason for this as the exact quote from the first post is:
I have also made one called Familia (family) to be turned On if both are at home

ToneStrife
Posts: 21
Joined: Sunday 16 April 2017 17:02
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by ToneStrife » Friday 28 April 2017 19:14

Yes, the point about the Both home. So I finally make it work.

My final question is that I make it work when I change from all to devices in the Lua config. How do I know which one I have to use everytime?

Nautilus
Posts: 774
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Script examples (Lua)

Post by Nautilus » Friday 28 April 2017 22:47

ToneStrife wrote:Yes, the point about the Both home. So I finally make it work.

My final question is that I make it work when I change from all to devices in the Lua config. How do I know which one I have to use everytime?
Was there something unclear in my first reply? :) A recap:
Choose device script if you have a "devicechanged" trigger on the script ( = you wan the script to run on each device change) and select time script if you only have "otherdevices" conditions ("devicechanged is not available in time scripts) and you want to run the script once a minute. I see no reason to select "All" option in any circumstance but probably there is some special cases...:)
In short:
All: usually not needed (triggered always?)
Device: triggered on all device changes
Time: triggered once a minute

ToneStrife
Posts: 21
Joined: Sunday 16 April 2017 17:02
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by ToneStrife » Tuesday 02 May 2017 9:35

Thanks!

tozzke
Posts: 134
Joined: Friday 02 January 2015 10:22
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Philips Hue bridge update

Post by tozzke » Monday 14 August 2017 2:56

Perhaps there are more people interested in whether or not there is an update available for your Philips Hue bridge ;)

Code: Select all

-- script_time_Philips Hue bridge update.lua

-- config ---------------------------------------------------------
local hue_update = 'Philips Hue bridge'                                         -- selector switch (with states 'Off' and 'Level 10' with the name 'Update available!'
local update_available = 'Set Level: 10'
local url = 'http://<IP HUE BRIDGE>/api/<API KEY>/config'

local tempfilename = '/home/pi/domoticz/scripts/philips_hue_update.tmp'         -- can be anywhere as long as it's writeable
-- config ---------------------------------------------------------
 
commandArray = {}

local function runEvery60min()
    print('<font color="blue">Philips Hue update status: ')
    os.execute('curl -s -o "'..tempfilename..'" '..url)

    local line = ''
    
    for line in io.lines(tempfilename) do
        if      string.find(line,'"updatestate":0') then
                    print('<font color="grey">Philips Hue: No update available</font>')
                if  otherdevices[hue_update] ~= 'Off' then
                    commandArray[hue_update] = 'Off'
                    print('<font color="grey">Philips Hue: Update installed</font>')
                    commandArray['SendNotification']='Philips Hue bridge#The update has been installed'
                end
        elseif  string.find(line,'"updatestate":1') then
                    print ('<font color="green">Philips Hue: Update is downloading</font>')
                if  otherdevices[hue_update] ~= 'Update available!' then
                    commandArray[hue_update] = update_available
                    commandArray['SendNotification']='Philips Hue bridge#There is an update available!'
                end
        elseif  string.find(line,'"updatestate":2') then
                    print('<font color="green">Philips Hue: Update is downloaded and ready to install</font>')
                if  otherdevices[hue_update] ~= 'Update available!' then
                    commandArray[hue_update] = update_available
                    commandArray['SendNotification']='Philips Hue bridge#There is an update available!'
                end
        elseif  string.find(line,'"updatestate":3') then
                    print('<font color="green">Philips Hue: Update is installing</font>')
                if  otherdevices[hue_update] ~= 'Update available!' then
                    commandArray[hue_update] = update_available
                    commandArray['SendNotification']='Philips Hue bridge#There is an update available!'
                end
        end
    end
end

local m = os.date('%M')

if (m % 60 == 0) then
	runEvery60min()
end

return commandArray
Probably not the prettiest script (a bit of copy/paste work), but it seems to work just fine :)
If someone can make it more efficient, feel free to do so ;)

Kivi
Posts: 6
Joined: Wednesday 15 April 2015 20:54
Target OS: Linux
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by Kivi » Sunday 28 January 2018 17:32

can some1 help me out whit this simple lua script. the user var is never readed and writed when using like so and the script wont work at all :(

Code: Select all

commandArray = {}

 if (otherdevices['alarm'] == 'Off' and otherdevices['IsDark']== 'Off' and uservariables["Coming_home_light"] == "0" ) then
        commandArray['Sofa & paint light']='On'
        commandArray['Lamp livingroom']='On'
        commandArray['bed light]='On'
        commandArray['Variable:Coming_home_light'] = 1
 elseif (otherdevices['alarm'] == 'Off' and uservariables["Coming_home_light"] == "1" ) then
                commandArray['Variable:Coming_home_light'] = 0
 end

User avatar
Siewert308SW
Posts: 607
Joined: Monday 29 December 2014 16:47
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands - Zoutkamp (gn)
Contact:

Re: Script examples (Lua)

Post by Siewert308SW » Sunday 28 January 2018 18:10

Kivi wrote:
Sunday 28 January 2018 17:32
can some1 help me out whit this simple lua script. the user var is never readed and writed when using like so and the script wont work at all :(

Code: Select all

commandArray = {}

 if (otherdevices['alarm'] == 'Off' and otherdevices['IsDark']== 'Off' and uservariables["Coming_home_light"] == "0" ) then
        commandArray['Sofa & paint light']='On'
        commandArray['Lamp livingroom']='On'
        commandArray['bed light]='On'
        commandArray['Variable:Coming_home_light'] = 1
 elseif (otherdevices['alarm'] == 'Off' and uservariables["Coming_home_light"] == "1" ) then
                commandArray['Variable:Coming_home_light'] = 0
 end
You mismatched those characters.

Code: Select all

if (otherdevices['alarm'] == 'Off' and otherdevices['IsDark']== 'Off' and uservariables['Coming_home_light'] == 0 ) then
        commandArray['Sofa & paint light']='On'
        commandArray['Lamp livingroom']='On'
        commandArray['bed light]='On'
        commandArray['Variable:Coming_home_light'] = '1'
 elseif (otherdevices['alarm'] == 'Off' and uservariables['Coming_home_light'] == 1) then
                commandArray['Variable:Coming_home_light'] = '0'
 end
Setup:
- RPi3 - PIco HV3.0A / Domo Beta 3.9XXX / RFXtrx433E / Aeotec Gen5
- RPi3 - PIco HV3.0A / PiHole / PiVPN / NAS / Print Server
- Youless Elec&Gas
- FI9803P Cams
- KD101 detectors
- Zwave & KaKu

MyDomoticz scripts: @GitHub
PIco UPS Toolkit: @GitHub

Kivi
Posts: 6
Joined: Wednesday 15 April 2015 20:54
Target OS: Linux
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by Kivi » Sunday 28 January 2018 22:36

After editing the script and testing, I still cant get this work. If I remove second if statement then the script works fine, but whit if uservariables the script wont do nothing, why is that? I also made test blocky script and still dident succseed.

Code: Select all

commandArray = {}
if (devicechanged['Alarm'] == 'Off' and otherdevices['IsDark'] == 'On') then
   if ( uservariables["Coming_home_light"] == 1 ) then
                print('<font color="blue">debug: statement works</font>')
                commandArray[light_switch]= 'On'
                commandArray['Sofa & paint light']= 'On'
        end
end
return commandArray

User avatar
cyberclwn
Posts: 124
Joined: Thursday 20 August 2015 22:53
Target OS: Raspberry Pi
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Script examples (Lua)

Post by cyberclwn » Monday 05 February 2018 22:37

Hey,

Can you try change the line:
Kivi wrote:
Sunday 28 January 2018 22:36

Code: Select all

if ( uservariables["Coming_home_light"] == 1 ) then
to

Code: Select all

if (tonumber(uservariables["Coming_home_light"]) == 1 ) then

And probably also the other way around:

Code: Select all

commandArray['Variable:Coming_home_light'] = 1

Code: Select all

commandArray['Variable:Coming_home_light'] = tostring('1')
3xPi 2B (Domoticz "live", Domoticz "sandbox", PhotoFrame)
RFXCom433(E), KaKu, Oregon Scientific, Keyes 2-relay, Logitech Media Server, MiLight, Smartwares heating controller(2x), IR Send/Receive, Keyes PIR, XH-M131 DuskSensor, DHT22/11

snuiter
Posts: 67
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Script examples (Lua)

Post by snuiter » Wednesday 02 May 2018 19:45

Hi,
need your help on lua/dZvent script to get output back in domoticz either to dummy switch/script to process.

I can check the tv status via domoticz raspberry(rPi1). TV is connected to remote raspberry pi(rPi2)
this works on the console but not via a script in domoticz.

Need some help to point me in the right direction.

This is the command I run on the command line

Code: Select all

ssh 192.168.x.x 'echo pow 0 | cec-client -s -d 1 | grep "power status:" | sed "s/..............//" '
output is either "on" or "standby"

Now I am trying with os.execute command in script, but not able to get the response from the command.

Any suggestion how to do that.
In a dZvent script I put these two line just for testing purposes

Code: Select all

	local t = os.execute('ping -c 192.168.x.x')
	print(t)
tried several other things but don't seem to be able to get the output I see from the same command on the console.

Any help is appreciated

bertus
Posts: 3
Joined: Friday 21 September 2018 16:25
Target OS: Raspberry Pi
Domoticz version:
Contact:

cannot update user variable from Lua script

Post by bertus » Friday 21 September 2018 16:55

The LUA script below is meant to increment a user variable within domoticz; access to the variable works but updating it does not. please advice

counter = tonumber(uservariables["SWITCH"])
Commandarray = {}
if (devicechanged['SWITCH'] == 'On') then
counter = counter + 1
commandarray['variable:SWITCH'] = tostring(counter)
end
return commandarray

DutchHans
Posts: 330
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: Script examples (Lua)

Post by DutchHans » Friday 21 September 2018 18:57

It's commandArray... Mind the capital A

Cheers,Hans

bertus
Posts: 3
Joined: Friday 21 September 2018 16:25
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by bertus » Saturday 22 September 2018 10:13

Hi Hans,
thanks for the hint.
unfortunately it does not fix the issue...
Cheers, Bertus

bertus
Posts: 3
Joined: Friday 21 September 2018 16:25
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by bertus » Saturday 22 September 2018 10:18

Hi Hans,
indeed caused by na uppercase misspell : variable
now it works
Cheers

User avatar
Petturik
Posts: 2
Joined: Friday 13 July 2018 13:05
Target OS: Raspberry Pi
Domoticz version:
Location: NL
Contact:

Re: Script examples (Lua)

Post by Petturik » Sunday 30 September 2018 14:08

Just wanted to share my Gardenlighting script.

Seeing I wanted a dimmer connected to the lighting and a standalone button to control the lighting aswell, I made this script.
Well, made, I compiled it from several examples.

Comments and feedback are welcome :

Code: Select all

-- If PIR detects motion (turns on) 
-- it's 10 minutes before sunset
-- AND the garden lights are off
-- turn them on for 2 minutes on level 50 

-- I don't want to override the "Scare" lighting by switching my kitchen switch, 
-- so I'll make a timer which runs for 2 minutes (see 'Timer Tuin').
-- During those 2 minutes, the lights can't be turned off by the kitchen switch

-- I want to receive a notification when movement is detected in the garden, but only when lights won't be turned on due to not meeting the 'ss' requirement

-- If the Kitchen switch is turned on,
-- AND the gardenlights are off,
-- turn them on at level 20.
-- But if the Kitchen switch is turned on and the Gardentimer is on,
-- turn off Kitchen switch

-- If the Kitchen switch is turned off
-- AND the gardenlights are not off
-- AND the timertest is not on,
-- turn off gardenlights

commandArray = {}

 -- get current time
    tn = os.date("*t")
    mn = tn.min + tn.hour * 60
 
 -- set sunset time
    ss = timeofday['SunsetInMinutes'] - 10
 
 -- devices
    n1 = 'nep'
    b1 = 'Burglar - Tuin'               -- Motion detection garden
    d1 = 'Tuin - Dim'                  	-- Dimmer for gardenlights
    s1 = 'Tuin'                      	-- Kitchen switch for gardenlights
    tt = 'Tuin - Timer'                	-- Dummy switch on a 3 minute "off" timer
 
 -- variables and commands
    dc = devicechanged
    otd = otherdevices
    ca = commandArray
    doff = (otd[d1]=="Off")
    dnoff = (otd[d1]~="Off")
    don = (otd[d1]=="On")
    soff = (dc[s1] == 'Off')
    son = (dc[s1] == 'On')
    ott = otd[tt]
  
if (dc[b1] == 'On' )                                          
    then
    	if (mn >= ss) 
            and doff
    	        then ca[d1]= 'Set Level 50 FOR 2'
                     ca[tt]= 'On'
                    print("Beweging, '".. d1 .."' aan voor 2 minuten")

        elseif (mn <= ss)
            and doff    
                then 
                    -- ca['SendNotification']= '#Beweging in de tuin, maar het is niet donker genoeg om de verlichting in te schakelen####telegram'
                    print("Beweging, maar niet in het donker")
end
    
elseif son 
    then
    	if doff 
    		    then ca[2]={[d1]= 'Set Level 20'}
                    print("'".. s1 .."' ingeschakeld, ik ga aan")
        elseif (ott=="On")
                then ca[3]={[s1]= 'Off'}
                    print("'".. d1 .."' staat aan, '".. s1 .."' gaat weer uit")
end
 
elseif soff 
    then
    	if dnoff
    		and (ott~="On") 
    		    then ca[4]={[d1]= 'Off'}
                    print("'".. s1 .."' uitgeschakeld, ik ga uit") 
end

end

return commandArray
p.s. I commented out the notification part, seeing it would spam me on a sunny day with the kids running around the garden all day :D
RPi3b+
Z-Wave(+)
Zigbee
ESP8266

Post Reply

Who is online

Users browsing this forum: accel and 5 guests