Convert Lua to dzvents

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

Moderator: leecollings

Post Reply
anasazi
Posts: 43
Joined: Saturday 06 August 2016 9:53
Target OS: Windows
Domoticz version:
Location: Sweden
Contact:

Convert Lua to dzvents

Post by anasazi »

Hi,

I have for some days trying to convert my Lua script to a dzVents script which I use to control my light at home based on time and lux.
Have been trying with all possible combination but I can't figure out how to solve it.
Is there someone who know how to re-write this Lua code into dzvents code?

Thanks!

Code: Select all

commandArray = {}

time                = os.date('%H:%M')

lowLightValue       = tonumber(otherdevices['Luxsensor'])
lowLightSetPoint    = 12
offTime             = 'Off AFTER 1800' 


--============================== Köksfönster ==============================--

if (time == '05:30' and lowLightValue < lowLightSetPoint and uservariables['Weekend'] == 'False') then
	commandArray['Köksfönstret']='On'
	
elseif (time == '07:30' and lowLightValue < lowLightSetPoint and uservariables['Weekend'] == 'True') then
	commandArray['Köksfönstret']='On'
	
elseif (lowLightValue > lowLightSetPoint and uservariables['lowLightKöksfönster'] == '0' and time > '05:30' and time < '22:30') then
	commandArray['Köksfönstret']=offTime
	
	commandArray['Variable:lowLightKöksfönster'] = '1'
elseif (lowLightValue < lowLightSetPoint and uservariables['lowLightKöksfönster'] == '1' and time > '05:30' and time < '22:30') then
    commandArray['Köksfönstret']='On'
    commandArray['Variable:lowLightKöksfönster'] = '0'
    
elseif (time == '22:30') then
   commandArray['Köksfönstret']='Off'
   
end

return commandArray
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Convert Lua to dzvents

Post by emme »

try with:

Code: Select all

local timer1 = 'at 05:30'
local timer2 = 'at 07:30'
local timer3 = 'between 05:30 and 22:30'
local timer4 = 'at 22:30'
local offTime = 1800
local lowLightSetPoint = 12


return {
	on = { 
	    timer = { 
	                timer1,
	                timer2,
	                timer3,
	                timer4
	    }
	},
    execute = function(dz,devNil,trgInfo)
        local lowLightValue = dz.devices('Luxsensor')
   
        if trgInfo.trigger == timer1 and lowLightValue < lowLightSetPoint and dz.variables('Weekend') == 'False' then
            dz.devices('Köksfönstret').switchOn().checkFirst()
            
        elseif trgInfo.trigger == timer2 and lowLightValue < lowLightSetPoint and dz.variables('Weekend') == 'True' then
            dz.devices('Köksfönstret').switchOff().checkFirst()
            
        elseif trgInfo.trigger == timer3 
            if (lowLightValue > lowLightSetPoint and dz.variables('lowLightKöksfönster') == '0') then
                dz.devices('Köksfönstret').switchOff().afterSec(offTime)  --better to convert 1800 secs to mins and use .afterMin(offTime)
	        dz.variables('lowLightKöksfönster').set('1') -- if variable is a number/integer, remove the ' '
	    elseif (lowLightValue < lowLightSetPoint and dz.variables('lowLightKöksfönster')  == '1' then
	         dz.devices('Köksfönstret').switchOn()
	         dz.variables('lowLightKöksfönster').set('0') -- if variable is a number/integer, remove the ' '
	     end
	elseif trgInfo.trigger == timer4 then 
	    dz.devices('Köksfönstret').switchOff().checkFirst()
	end
    end
}
a note: I use timern because I prefer to put the timer string just once... in case you want to reschedule you just need to edit 1 row
ciao
M
Last edited by emme on Tuesday 09 January 2018 8:16, edited 1 time in total.
The most dangerous phrase in any language is:
"We always done this way"
anasazi
Posts: 43
Joined: Saturday 06 August 2016 9:53
Target OS: Windows
Domoticz version:
Location: Sweden
Contact:

Re: Convert Lua to dzvents

Post by anasazi »

Thank you so much, I will try this later today! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest