I like to have all my scripts in the same language and stored in the database.
I created a powershell script that is started by a batch-file. I find it a really 'messy' way of scripting this.
So I want to convert it into 1 LUA/dzEvents script.
The powershell script looks like this:
Code: Select all
# Hue Bridge URL
$hueBridge = "link to api"
# HUE Username
$username = 'username'
#Stop trigger
$filestop = "D:\path\scenerelax.stop"
# Time between light changes (seconds)
$waittime = 2
# Remove stop trigger
IF((Test-Path $filestop)) {
Remove-Item -Path $filestop -Force
}
function LightRandom {
param($lightID,$Sat,$maxBri,$minBri,$transTm)
IF((Test-Path $filestop)) {
break
}
# hue 65535 = 360 degree hsv
$colorhue=(Get-Random -Minimum 37 -Maximum 50)*1000
$brival=((Get-Random -Minimum $minBri -Maximum $maxBri)*10)-9
if ($transTm -gt 0) {
$strTransTm = '"transitiontime":' + $transTm
}
$body = '{'+ $($strTransTm) +', "hue":'+ $($colorhue) +', "sat":' + $Sat + ', "bri":' + $brival +'}'
$result=Invoke-RestMethod -Method PUT -Uri "$($hueBridge)/$($username)/lights/$($lightID)/state" -Body $body
Start-Sleep -Milliseconds 100
}
# FUNCTION: Turn light off
function LightOff {
param($lightID,$transTm)
if ($transTm -gt 0) {
$strTransTm = ', "transitiontime":' + $transTm
}
$body = '{"on":false'+ $($strTransTm) +'}'
$result=Invoke-RestMethod -Method PUT -Uri "$($hueBridge)/$($username)/lights/$($lightID)/state" -Body $body
Start-Sleep -Milliseconds 100
}
# Main loop
while (1)
{
LightRandom -lightID 8 -Sat 96 -minBri 1 -maxBri 20 -transTm 80
Start-Sleep -s $waittime
LightRandom -lightID 1 -Sat 228 -minBri 10 -maxBri 15 -transTm 20
LightRandom -lightID 9 -Sat 96 -minBri 1 -maxBri 20 -transTm 80
Start-Sleep -s $waittime
LightRandom -lightID 2 -Sat 228 -minBri 10 -maxBri 15 -transTm 20
LightRandom -lightID 10 -Sat 96 -minBri 1 -maxBri 20 -transTm 80
Start-Sleep -s $waittime
LightRandom -lightID 5 -Sat 96 -minBri 1 -maxBri 20 -transTm 80
Start-Sleep -s $waittime
LightRandom -lightID 12 -Sat 228 -minBri 10 -maxBri 15 -transTm 20
LightRandom -lightID 6 -Sat 96 -minBri 1 -maxBri 8 -transTm 80
Start-Sleep -s $waittime
LightRandom -lightID 11 -Sat 228 -minBri 10 -maxBri 15 -transTm 20
LightRandom -lightID 7 -Sat 96 -minBri 1 -maxBri 20 -transTm 80
Start-Sleep -s $waittime
}
# Main loop stopped
IF((Test-Path $filestop)) {
Remove-Item -Path $filestop -Force
}
# Turn lights off
LightOff -lightID 1 -transTm 300
LightOff -lightID 2 -transTm 300
LightOff -lightID 11 -transTm 300
LightOff -lightID 12 -transTm 300
LightOff -lightID 5 -transTm 0
LightOff -lightID 6 -transTm 0
LightOff -lightID 7 -transTm 0
LightOff -lightID 8 -transTm 0
LightOff -lightID 9 -transTm 0
LightOff -lightID 10 -transTm 0
# Clear Database logs
#$sqlstring = "DELETE FROM LightingLog WHERE LightingLog.DeviceRowID in (SELECT distinct LightingLog.DeviceRowID FROM LightingLog,DeviceStatus WHERE LightingLog.DeviceRowID = DeviceStatus.ID AND DeviceStatus.Used = 1 AND DeviceStatus.Type = 241);"
#& 'D:\Domoticz\sqlite3.exe' 'D:\Domoticz\domoticz.db' $sqlstring
Beside it uses direct access to the bridge i like to avoid, it also uses the IDs of the lights in the bridge.
Not very flexible.
A batch file starts the powershell script or it created an empty file called scenerelax.stop to stop the loop.
So I like to make a LUA/dzEvents script to get the light IDs by name, and loop them and set color and intensity.
However initial scripting already fails on the loop. I made this test script:
Code: Select all
return {
on = {
scenes = {
'Relax'
}
},
execute = function(domoticz, item)
-- FUNCTION : Sleep Timer
local function LoopSleep(a)
local sec = tonumber(os.clock() + a);
while (os.clock() < sec) do
end
end
-- FUNCTION : Do something with light
local function ChangeLight(deviceIDX)
domoticz.log('Device : ' .. deviceIDX)
domoticz.log('Device : ' .. domoticz.devices(deviceIDX).name)
--domoticz.devices(deviceIDX).dimTo(20)
return
end
-- Run main script
if (item.state == 'On') then
while (item.state == 'On') do
-- loop through configured lights in scene
item.devices().forEach(function(device)
ChangeLight(device.idx)
os.execute('ping -n 1 -w 2000 1.2.3.4')
end)
end
else
domoticz.log('clearing DB ')
end
end
}
All fails because I get in the log that the script ran too long.
Can anyone give me an example of a loop execution?
Thanks.