Script examples (Lua)

tozzke
Posts: 124
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: 701
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: 701
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: 124
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 ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests