Script examples (Lua)

tozzke
Posts: 131
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: 756
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: 756
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: 131
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: 4
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: 4
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: 120
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: 52
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

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests