Script examples (Lua)

Moderator: leecollings

Joost-Brw
Posts: 4
Joined: Monday 16 December 2013 22:47
Target OS: Linux
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by Joost-Brw »

Keptenkurk wrote:This example switches my Woonveilig alarmpanel (which is actually a rebranded CTC-1716 by Climax Tech) according to the state of a Domoticz switch.

The LUA script is triggered by the device MASTERSWITCH. When switched on, the Woonveilig panel will be armed. When switched off, the panel will be disarmed.
An additional light (Lampje bij Thermostaat) confirms the alarmpanel state visually.
Application: automatic arm/disarm based on being at home (mobile phone's WiFi or Bluetooth - see other examples in this forum) or scheduled.

file: ~/domoticz/scripts/lua/script_device_alarmpanel.lua

Code: Select all

commandArray = {}
if (devicechanged['MASTERSWITCH'] == 'On') then
        os.execute('/home/pi/domoticz/scripts/alarmpanel.sh arm')
        print('Alarmpanel armed')
        commandArray['Lampje bij thermostaat']='On'
end

if (devicechanged['MASTERSWITCH'] == 'Off') then
        os.execute('/home/pi/domoticz/scripts/alarmpanel.sh disarm')
        print('Alarmpanel disarmed')
        commandArray['Lampje bij thermostaat']='Off'
end
return commandArray


Switching of the Alarmpanel is performed by the shell script "alarmpanel.sh" which can take the argument 'arm', 'armhome', 'disarm', 'sirenon' or 'sirenoff'.
First a login is made to a general page. Then the requested POST is made to the web page where the alarmpanel arming state can be set.
When the login expires, every first POST command will fail with an autentication request page, therefore we first login before every POST command.

tip: To find out the URL and POST values of the page use Firefox with the Firebug plugin enabled to browse to the page. The debug output will show
exactly which GET and POST messages and parameters were called. This will enable you to control or read from virtually any device which offers a web interface.

EDIT: Additionally an extra argument is available: checkstate
This reads the current state of the panel (Arm, Home or Disarm) and sets/resets a dummy switch in Domoticz.
I run this feature every minute by cron. This now reflects my "at home" state and consequently switch my close-in boiler off when not at home.
(need to build a businesscase for this hobby :roll: )

file: ~/domoticz/scripts/alarmpanel.sh

Code: Select all

#!/bin/bash

########################################################################################
### Woonveilig script v1.2 beta added checkstate option to request current armed state
###
### Domoticz <--> Woonveilig interface script
###
### Usage:
### alarmpanel arm
### puts the Woonveilig alarmpane is the Armed state
###
### alarmpanel armhome
### puts the Woonveilig alarmpanel in the Armed & at Home state
###
### alarmpanel disarm
### puts the Woonveilig alarmpanel in the Disarmed state
###
### alarmpanel sirenon
### sounds the siren (and maybe the remote siren too) of the Woonveilig alarmpanel
###
### alarmpanel sirenoff
### silences the siren (and maybe the remote siren too) of the Woonveilig alarmpanel
###
### alarmpanel checkstate
### reads the current armed state of the Woonveilig alarmpanel and changes a Domoticz switch
### to On when disarmed or armed & home and to Off when armed.
###
########################################################################################

if [ $#  -lt 1 ]
then
        echo "Usage: alarmpanel {arm | armhome | disarm | sirenon | sirenoff | checkstate}"
        exit
fi

### USER CONFIGURABLE PARAMETERS
PANEL="192.168.1.133:80"                # Alarm panel IP:PORT (find this in your router DHCP scope)
PANEL_USR="admin"                       # Alarm panel username (default admin)
PANEL_PASS="admin1234"                  # Alarm panel password (default admin1234)
DZSERVER="192.168.1.95:8080"            # Domoticz server IP:PORT
AT_HOME="28"                            # Idx of dummy switch which reflects at home state
                                        # (find this in Domoticz|Devices|Idx column)
### END OF USER CONFIGURABLE PARAMETERS

### First login to alarmpanel
/usr/bin/curl -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/setting/index.htm >/dev/null

### Then issue appropriate command
case "$1" in

arm)            echo "Arm Alarmpanel"
                /usr/bin/curl -d "mode=0" -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/action/panelCondPost >/dev/null
                ;;

armhome)        echo "Armhome Alarmpanel"
                /usr/bin/curl -d "mode=1" -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/action/panelCondPost >/dev/null
                ;;

disarm)         echo "Disarm Alarmpanel"
                /usr/bin/curl -d "mode=2" -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/action/panelCondPost >/dev/null
                ;;

sirenon)        echo "Siren On"
                /usr/bin/curl -d "sndsiren_onoff=0" -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/action/sndSirenPost >/dev/null
                ;;

sirenoff)       echo "Siren Off"
                /usr/bin/curl -d "sndsiren_onoff=1" -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/action/sndSirenPost >/dev/null
                ;;

### Read current state webpage
### And watch for Arm, Home or Disarm in the answer
### Set (dummy) switch to On or Off as required

checkstate)     strPanelState=`/usr/bin/curl -u $PANEL_USR:$PANEL_PASS -s http://$PANEL/action/panelCondGet`
                if [[ $strPanelState == *\"Arm\"* ]]
                then
                        echo "State is Armed"
                        /usr/bin/curl "http://$DZSERVER/json.htm?type=command&param=switchlight&idx=$AT_HOME&switchcmd=Off&level=0"
                else
                  if [[ $strPanelState == *\"Disarm\"* ]]
                  then
                        echo "State is Disarmed"
                        /usr/bin/curl "http://$DZSERVER/json.htm?type=command&param=switchlight&idx=$AT_HOME&switchcmd=On&level=0"
                  else
                    if [[ $strPanelState == *\"Home\"*  ]]
                    then
                        echo "State is Armed Home"
                        /usr/bin/curl "http://$DZSERVER/json.htm?type=command&param=switchlight&idx=$AT_HOME&switchcmd=On&level=0"
                    else
                        echo "No matched state found"
                    fi
                  fi
                fi

                ;;

*)              echo "Invalid argument. Usage: alarmpanel {arm | armhome | disarm | sirenon | sirenoff | checkstate}"
                ;;

esac

### Done

I'm not able to make this script work on my PI. I have changed all the settings and tested the URL's to my Woonveilig
system. Is there any possibility to check where it's going wrong?

EDIT: This is the first time i'm using script on Domoticz so maybe I forgot to do some 'first time' practical changes. I really hope anyone can help me.
User avatar
Keptenkurk
Posts: 103
Joined: Wednesday 21 August 2013 17:24
Target OS: -
Domoticz version:
Location: Waalre, The Netherlands
Contact:

Re: Script examples (Lua)

Post by Keptenkurk »

Hi Joost,
The way to proceed is:
1. Check if your alarmpanel is accessible from a browser. Can you login and change the panel state from your browser and see it state?
2. If so: Can you run the shell script manually from the commandline? Does

Code: Select all

./alarmpanel.sh arm 
put your panel in armed state? You might need to make the script executable if it doesn't.
3. If that works: The problem is somewhere in the lua script. Could you then show a copy of your lua script?
/paul
Joost-Brw
Posts: 4
Joined: Monday 16 December 2013 22:47
Target OS: Linux
Domoticz version:
Contact:

Re: Script examples (Lua)

Post by Joost-Brw »

Paul,

Really thanks for your quick response. There was indeed a problem with the permissions of the script. The second problem was because I have made script on Windows. After using dos2unix the script was working perfectly! Later I have changed the script a little bit because i'm not using 'at home' status. The button is now corresponding with the status 'armed' or 'disarmed'.

Really thanks for the help.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Script examples (Lua)

Post by D'rMorris »

mbliek wrote:change SERVER="192.168.1.5" to SERVER="192.168.1.5:8080" (if your port is 8080)
And perhaps try to use single (') quotes in stead of double (")quotes.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Script examples (Lua)

Post by D'rMorris »

Below a script I found on this blog, so all credits go there (or to the one this person has copied it from ;) ).

I modified the script a little:
- changed all variables to English
- changed some threshold values
- corrected 2 code errors

This script can be used to control automatic blinds, curtains, etc. The first part of the script is used to read weather data from a weather station. Since I don't have a weather station yet, I used Weather Underground. This script then grabs the needed value from the string of numbers given by the weather sensors. For example: the wind meter gives as output: 235.00;SW;7;31;12.5;12.5. The script now takes the "7", which is the wind speed.

The script works with a normal blinds / sunscreen switch, of course combined with an electrical screen.

The part after the reading of the values and the info about the thresholds I used, is the logic which controls the sunscreen.

Only thing I can't get to work is for the script to output the wind figures correctly. It now outputs for example: Windgust: 31 and Windspeed: 7, whereas in fact these figures are 3.1 and 0.7. Besides that, the Windgust, Windspeed and actual rainfall are without decimals, whereas in the dashboard of Domoticz they are with decimals.
I guess that the ELSEIF with the wind threshold also doesn't work OK because of this. Now, the blinds close when windspeed = 7, although I've set the threshold to 50. Any ideas?

I hope you find it useful!

Code: Select all

commandArray = {}

--Extract Wind Gust speed from SValue Wind
wind = otherdevices_svalues['Windmeter']
print("Wind: "..wind)

--Filter the third digitsequence from SValue
a = string.sub(wind,1,string.find(wind,';',1,true)-1)
--print("a: "..a)

b = string.sub(wind,string.find(wind,';',1,true)+1)
--print("b: "..b)

winddirection = string.sub(b,1,string.find(b,';',1,true)-1)
--print("Winddirection: "..winddirection)

d = string.sub(b,string.find(b,';',1,true)+1)
--print("d: "..d)

windspeed = string.sub(d,1,string.find(d,';',1,true)-1)
print("Windspeed: "..windspeed)

f = string.sub(d,string.find(d,';',1,true)+1)
--print("f: "..f)

windgust = string.sub(f,1,string.find(f,';',1,true)-1)
print("Windgust: "..windgust)

g = string.sub(f,string.find(f,';',1,true)+1)
--print("g: "..g)

temperature = string.sub(g,1,string.find(g,';',1,true)-1)
print("Temperature: "..temperature)

--Extract UV radiation from SValue Wind
solar = otherdevices_svalues['UV']
print("Solar Radiation: "..solar)
--Filter the first digit from SValue
uv = string.sub(solar,1,string.find(solar,';',1,true)-1)
print("UV: "..uv)

--Extract actual rainfall from Rain_SValue
rain = otherdevices_svalues['Rainmeter']
print("Rain: "..rain)

--Filter the first digit from SValue
currentrain = string.sub(rain,1,string.find(rain,';',1,true)-1)
print("Current rain: "..currentrain)

--Filter the last digit from SValue
totalrain = string.sub(rain,string.find(rain,';',1,true)+1)
print("Total rain: "..totalrain)

--Sunscreen thresholds:
        -- Windspeed            = 50 (value is multiplied by 10)
        -- Rain                 =  0.0
        -- UV                   =  4.0
        -- Temperature          = 15.0
        -- Closed       = Down  = On
        -- Open         = Up    = Off

if (timeofday['Daytime'])
        then
                print('It is past sunrise, monitoring variables for sunscreen')
                if (otherdevices['Sunscreen'] == 'Open' ) then
                print ('Sunscreen is up')
        else
                print ('Sunscreen is down')
end

if (otherdevices['Sunscreen']   == 'Open'
        and currentrain         == '0'
        and temperature         > '15.0'
        and uv                  > '4.0'
        and windspeed           < '50')

then
        print ('Sun is shining, all thresholds OK, lowering sunscreen')
        commandArray['SendNotification']='Sunscreen#Sunscreen down --> Sun is shining'
        commandArray['Sunscreen']='On'

        elseif (otherdevices['Sunscreen'] == 'Closed' and currentrain > '0') then
                print ('It is raining, raising sunscreen')
                commandArray['SendNotification']='Sunscreen#Sunscreen up --> It is raining'
                commandArray['Sunscreen']='Off'

        elseif (otherdevices['Sunscreen'] == 'Closed' and temperature < '15.0') then
                print ('Temperature too low, raising sunscreen')
                commandArray['SendNotification']='Sunscreen#Sunscreen up --> It is too cold'
                commandArray['Sunscreen']='Off'

        elseif (otherdevices['Sunscreen'] == 'Closed' and uv < '4.0') then
                print ('Sun not shining too bright, raising sunscreen')
                commandArray['SendNotification']='Sunscreen#Sunscreen up --> Sunshine not too bright'
                commandArray['Sunscreen']='Off'

        elseif (otherdevices['Sunscreen'] == 'Closed' and windspeed > '50') then
                print ('Windspeed too high, raising sunscreen')
                commandArray['SendNotification']='Sunscreen#Sunscreen up --> Windspeed too high'
                commandArray['Sunscreen']='Off'
else
        print('Sunscreen status OK --> No action')
end

elseif (timeofday['Nighttime']
        and otherdevices['Sunscreen'] == 'Closed')
        then
                print('It is night, raising sunscreen')
                commandArray['SendNotification']='Sunscreen#Sunscreen up --> It is night'
                commandArray['Sunscreen']='Off'
        else
                print('Sunscreen already up --> No action')
end

return CommandArray
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

I am a noob in lua but i'm trying to make a lua script that is uploading my energy values to PVoutput. For this i think i need a http socket but when I add:

Code: Select all

local http = require("socket.http")
I get the following error

Code: Select all

Tue Nov 25 22:50:54 2014 Error: /home/pi/domoticz/scripts/lua/script_device_test.lua:9: module 'socket.http' not found:
no field package.preload['socket.http']
no file '/usr/local/share/lua/5.2/socket/http.lua'
no file '/usr/local/share/lua/5.2/socket/http/init.lua'
no file '/usr/local/lib/lua/5.2/socket/http.lua'
no file '/usr/local/lib/lua/5.2/socket/http/init.lua'
no file './socket/http.lua'
no file '/usr/local/lib/lua/5.2/socket/http.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket/http.so'
no file '/usr/local/lib/lua/5.2/socket.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './socket.so'
As far as i know the library is installed but it looks like it's in the directory below.

Code: Select all

/usr/share/lua/5.1/socket
Somebody an help in his?
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: Script examples (Lua)

Post by simonrg »

You need to install the socket library and supporting code somewhere that Lua 5.2 can find.

It is explained on this page http://www.domoticz.com/wiki/Philips_Hu ... ng_Colours and the necassary supporting libraries are attached to the linked forum message.
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
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

Found it already but the libraries in the tar.gz files seems to be corrupt as I'm not able to untar them. And as this Philips Hue is supported now I thought also the socket library should be included in domoticz now. But I think my logic isn't correct on this.
Can I download the socket libraries somewhere else?
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

When I install through

Code: Select all

sudo apt-get install liblua5.2-0
Found in debian.org

Then it installs but not in the directory where domoticz is searching (don't know which directory it actually is installed but not in "/usr/local/share/lua/5.2").
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: Script examples (Lua)

Post by simonrg »

The native implementation of Philips Hue is coded in C++ so does not use the same libraries as Lua uses.

The tar files shouldn't be corrupted as a number of people appear to have downloaded them ok. Could you have corrupted the tar files on downloading, transferring from one computer to another etc.? Could you try another way of getting them to your Raspberry Pi?

If you have found a package that has the correct libraries then that is great, but I don't think that is what you have installed. The package would have socket in its name.

Unfortunately nobody has packaged the socket library for Lua 5.2, hence the need to install it directly - I followed the instructions from here,
http://www.domoticz.com/forum/viewtopic ... ket#p21554
but quite a lot of work involved, hence why I uploaded the compiled versions for the Raspberry Pi.
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
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

I've downloaded it multiple times from different machines but even when I try to extract it on my local machine it tells me that the files in damaged.
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: Script examples (Lua)

Post by simonrg »

The files are not corrupted, I copied the links from the forum and used these with wget on my Raspberry Pi to download the files all works perfectly as shown box below:

Code: Select all

pi@Lightwavepi ~/t $ wget http://www.domoticz.com/forum/download/file.php?id=1454
--2014-11-27 23:28:14--  http://www.domoticz.com/forum/download/file.php?id=1454
Resolving www.domoticz.com (www.domoticz.com)... 62.84.241.110
Connecting to www.domoticz.com (www.domoticz.com)|62.84.241.110|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 119926 (117K) [application/octet-stream]
Saving to: `file.php?id=1454'
100%[======================================>] 119,926     --.-K/s   in 0.07s   
2014-11-27 23:28:14 (1.74 MB/s) - `file.php?id=1454' saved [119926/119926]
pi@Lightwavepi ~/t $ lsfile.php?id=1454
pi@Lightwavepi ~/t $ mv file.php\?id\=1454 usrlocalsharelua5p2.tar.gz
pi@Lightwavepi ~/t $ tar -xvf usrlocalsharelua5p2.tar.gz 
./
./ltn12.lua
./socket.lua
./ansicolors.lua
./mobdebug.lua
./socket/
./socket/ftp.lua
./socket/headers.lua
./socket/smtp.lua
./socket/tp.lua
./socket/http.lua
./socket/url.lua
./socket/core.so
./mime.lua
./luacolors.lua
pi@Lightwavepi ~/t $ ls
ansicolors.lua  luacolors.lua  mobdebug.lua  socket.lua
ltn12.lua       mime.lua       socket        usrlocalsharelua5p2.tar.gz
It is most likely you are corrupting the files on downloading them, copying from one machine to another, or they are not being downloaded and you are saving an error page due to your local security setting, check the files are correct lengths.

These are the links:
http://www.domoticz.com/forum/download/file.php?id=1453
http://www.domoticz.com/forum/download/file.php?id=1454
If you do it on the Raspberry Pi, then moving the downloaded files to the correct location and using sudo tar would probably be easiest.
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
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

For some reason the tar.gz went corrupted when I place them on a windows machine. Download them directly to the pi works indeed. Thanks for that.
The upload to PVoutput works also, I only have to figure out how to do it with the right interval.
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: Script examples (Lua)

Post by thorbj »

mbliek wrote:It work great.

The script will set al lights Off and Arm my alarm between 22:00 and 05:00.

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
time = os.date("*t")
commandArray = {}

if (otherdevices['Huiskamer'] == 'No Motion' and time.hour >= 22 and time.hour < 05) then
   if (timedifference(otherdevices_lastupdate['Huiskamer']) > 3600 and timedifference(otherdevices_lastupdate['Huiskamer']) < 3780) then
      commandArray['Scene:Alle lichten']='Off'
      commandArray['Alarm']='Arm Home'
      print('Geen beweging, alle lichten uit en alarm ingeschakeld.')
   end   
end

return commandArray
Menno
Hi, I borrowed your script to try to turn off my livingroom lights if it was empty after 1 hour. But I can't get it to work.
Here is my modified script:

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
 time = os.date("*t")
commandArray = {}

if (otherdevices['Motion Sensor'] == 'Off') then
   if (timedifference(otherdevices_lastupdate['Motion Sensor']) > 3600 and timedifference(otherdevices_lastupdate['Motion Sensor']) < 3780) then
      commandArray['Scene:Stue Av']='Off'
      print('Ingen bevegelse, lys slått av.')
   end   
end

return commandArray
What have I done wrong here?

Thanks
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

Does the motion sensor gives an "off" signal?
What is the name of the script?
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: Script examples (Lua)

Post by thorbj »

I suppose it gives an Off signal. It is an Aeon Labs Multisensor.

The script is called script_time_stueav.lua
I also tried naming it script_device_stueav.lua
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
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: Script examples (Lua)

Post by simonrg »

Most motion sensors don't send Off only On, as they are always Off unless triggered, only sensors with internal timers etc send Off signal.

You would probably be better off working from these PIR scripts - http://www.domoticz.com/wiki/Smart_Lua_ ... _Smart_PIR.
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
roblom
Posts: 402
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: Script examples (Lua)

Post by roblom »

It should be script_device_stueav.lua
You can try a print command to see if the motion sensor=='off' is true

Code: Select all

if (otherdevices['Motion Sensor'] == 'Off') then
      print('Motion Sensor fra')
   if (timedifference(otherdevices_lastupdate['Motion Sensor']) > 3600 and timedifference(otherdevices_lastupdate['Motion Sensor']) < 3780) then
      commandArray['Scene:Stue Av']='Off'
      print('Ingen bevegelse, lys slått av.')
   end   
end
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: Script examples (Lua)

Post by simonrg »

roblom wrote:It should be script_device_stueav.lua
You can try a print command to see if the motion sensor=='off' is true

Code: Select all

if (otherdevices['Motion Sensor'] == 'Off') then
      print('Motion Sensor fra')
   if (timedifference(otherdevices_lastupdate['Motion Sensor']) > 3600 and timedifference(otherdevices_lastupdate['Motion Sensor']) < 3780) then
      commandArray['Scene:Stue Av']='Off'
      print('Ingen bevegelse, lys slått av.')
   end   
end
Assuming that the motion sensor does change state to Off, then this script will run 10's of times over the 3 minutes that the time statement is true turning the scene off multiple times, as every time a device is changed, this script will be activated.

Normally you would want a device script to use the devicechanged variable, but this won't be appropriate for a motion sensor, as you want the action to happen an hour after the last time it turned off.

If you name the script script_time_stueav.lua then Domoticz will check it every minute and just run it 3 times after 1 hour as written.

However, check in the Motion Sensor's log on the switches page to see if the sensor is ever set to an Off state, if not then have a look at the Wiki page.
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
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: Script examples (Lua)

Post by thorbj »

Thanks for the overwhelming response! :)

I see this in the motion-sensor's log:

Code: Select all

2015-01-26 10:50:11	Off
2015-01-26 10:35:10	Off
2015-01-26 10:30:40	On, Level: 100 %
2015-01-26 10:24:10	Off
2015-01-26 10:15:47	On, Level: 100 %
2015-01-26 10:03:10	Off
2015-01-26 09:52:12	On, Level: 100 %
Also I can set a parameter in the sensors settings saying

Code: Select all

Timeout period of no-motion detected before the Multisensor sends the OFF state
after being triggered. 
So I suppose it sends an Off signal?

Which means that the script should work if I name it script_time_stueav.lua and add the print command to check if sensor is off?

EDIT:
I can now see the message stating that the sensor is off in the Domoticz log. I also found a caps-error in the scenario-name, so now I just need to wait and see if the lights turns off after an hour of inactivity.

QUESTION: Is it right to use Scene:Stue AV, or Scenario:Stue AV? Or is both wrong?

EDIT2: Got it to work now. Have postet final script in next post.
Last edited by thorbj on Friday 06 February 2015 13:49, edited 1 time in total.
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests