'dzVentsitize' powershell script.

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
thonal
Posts: 3
Joined: Thursday 29 August 2019 15:38
Target OS: Windows
Domoticz version:
Contact:

'dzVentsitize' powershell script.

Post by thonal »

Hi,

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
The script loops through a list of lights and randomize the color and intensity between set values.
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
}
The loopsleep is not functioning, so as a test I used the ping methode.
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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: 'dzVentsitize' powershell script.

Post by waaren »

thonal wrote: Thursday 29 August 2019 15:59 Can anyone give me an example of a loop execution?
The domoticz event system is not designed to deal with these kind of loops. Every script does need to end within 10 seconds, mainly because the event system is single threaded, so one script can block the complete event system if it takes too long.
So you will have to rewrite your script to deal with that.
It is also not clear to me what you try to achieve. Can you please elaborate a bit on this ?

examples of loops in Lua / dzVents

Code: Select all

table = {a=1,b=true,c="string"}
for key, value in pairs(table) do
     print(key .. '  ' .. tostring(value) )
end

table={"dev_1","dev_2"}
for index, value in ipairs(table) do
     print(tostring(index) .. '  ' ..  value)
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
thonal
Posts: 3
Joined: Thursday 29 August 2019 15:38
Target OS: Windows
Domoticz version:
Contact:

Re: 'dzVentsitize' powershell script.

Post by thonal »

Hi,

I like to create a random loop of changing colors and intensities on multiple lamps. Kind of a meditation/relaxing animated atmospheric room. However if the dzEvents is single threaded, it is basically impossible?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: 'dzVentsitize' powershell script.

Post by waaren »

If you could make a predefined scene in the Hue hub that will create such a changing colors sequence, dzVents can send the required commands. The looping is then inside the hub and not in domoticz.

btw. It is not dzVents that is single threaded but the domoticz event system. So Lua, Blockly, Python or dzVents could block the eventSystem
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
thonal
Posts: 3
Joined: Thursday 29 August 2019 15:38
Target OS: Windows
Domoticz version:
Contact:

Re: 'dzVentsitize' powershell script.

Post by thonal »

Ok, I'm going to look into that. Thanks..
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest