Page 1 of 1

Help needed with LUX script

Posted: Thursday 28 September 2017 8:30
by Boredcat
I am trying to create my first DzVents script. It should turn on the lights when the LUX is below 21. A very simple script. But it doesn't work..

Below is the error and script.
It looks likes the if then statement is comparing a number with a string. But according to the documentation the Device.lux is a number and not a string.

Appreciate any help.

<br/>
2017-09-28 08:18:00.322 dzVents: Info: ------ Start internal script: Lux_Meter:, trigger: every 1 minutes<br/>
2017-09-28 08:18:00.347 dzVents: Info: 3.62<br/>
2017-09-28 08:18:00.348 dzVents: Info: On<br/>
2017-09-28 08:18:00.348 Error: dzVents: Error: An error occured when calling event handler Lux_Meter<br/>
2017-09-28 08:18:00.348 Error: dzVents: Error: ...m Files (x86)\Domoticz\scripts\dzVents\runtime/Utils.lua:61: attempt to compare string with number<br/>
2017-09-28 08:18:00.348 dzVents: Info: ------ Finished Lux_Meter<br/>
2017-09-28 08:18:00.349 EventSystem: Script event triggered: y:\Program Files (x86)\Domoticz\scripts\dzVents\runtime\dzVents.lua</r>

Code: Select all

</s>return {

	-- 'active' controls if this entire script is considered or not
	active = true, -- set to false to disable this script

	-- trigger
	-- can be a combination:
	on = {
	

		timer = {
			-- timer triggers.. if one matches with the current time then the script is executed
		
			'every 1 minutes'
		}
	},

	-- actual event code
	-- in case of a timer event or security event, device == nil
	execute = function(domoticz, device)
     
		local myDevice1 = domoticz.devices('LUX Meter')
		local myDevice2 = domoticz.devices('ALLES AAN REMOTE')
        domoticz.log(myDevice1.lux)
        domoticz.log(myDevice2.state)
		if (myDevice1.lux < 21 ) then
			myDevice2.switchOn()
			domoticz.log('Light info', 'The light ' .. myDevice2.name .. ' will be switched on due to LUX')
		end
	end
}

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 8:54
by DewGew
Trigger line seems to be wrong:

Code: Select all

timer = {
	'every minute'
		}

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 9:02
by DewGew
I rewrite your script. my suggest:

Code: Select all

return {

	active = true,
	
	on = {
	

		timer = {
			'every minute'
		}
	},

	execute = function(domoticz)
     
		local myDevice1 = domoticz.devices('LUX Meter')
		local myDevice2 = domoticz.devices('ALLES AAN REMOTE')
        domoticz.log(myDevice1.lux)
        domoticz.log(myDevice2.state)
		if (myDevice1.lux <= 21 ) then
			myDevice2.switchOn()
			domoticz.log('Light info', 'The light ' .. myDevice2.name .. ' will be switched on due to LUX')
		else 
			myDevice2.switchOff()
		end
	end
}
//Pierre

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 11:28
by Boredcat
@DewGew Thnx, Adjusted the script as suggested. No luck same error on string vs number compare

Code: Select all

attempt to compare string with number

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 11:57
by DewGew
This might work better:

Code: Select all

return {

	active = true,
	
	on = {
		timer = {
			'every minute'
		}
	},

	execute = function(domoticz)
     
		local myDevice1 = domoticz.devices('LUX Meter')
		local myDevice2 = domoticz.devices('ALLES AAN REMOTE')
                local LUX = myDevice1.lux
        domoticz.log(myDevice1.name .. ' ' .. LUX, domoticz.LOG_INFO)
        domoticz.log(myDevice2.name .. ' ' .. myDevice2.state, domoticz.LOG_INFO)
		if (LUX <= 21 ) then
			myDevice2.switchOn()
			domoticz.log('Light info, The light ' .. myDevice2.name .. ' will be switched on due to LUX', domoticz.LOG_INFO)
		else 
			myDevice2.switchOff()
		end
	end
}

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 12:03
by EdwinK
Thanks, was looking to make something like this.

But, now I get this error:

Code: Select all

2017-09-28 12:01:00.636 Error: dzVents: Error: An error occured when calling event handler Low Lux
2017-09-28 12:01:00.636 Error: dzVents: Error: /usr/local/domoticz/var/scripts/dzVents/runtime/Utils.lua:61: attempt to compare string with number
edit: since updating to script in post above this one, it works without errors.

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 12:18
by DewGew
Boredcat wrote: Thursday 28 September 2017 8:30
2017-09-28 08:18:00.322 dzVents: Info: ------ Start internal script: Lux_Meter:, trigger: every 1 minutes
2017-09-28 08:18:00.347 dzVents: Info: 3.62
2017-09-28 08:18:00.348 dzVents: Info: On
2017-09-28 08:18:00.348 Error: dzVents: Error: An error occured when calling event handler Lux_Meter
2017-09-28 08:18:00.348 Error: dzVents: Error: ...m Files (x86)\Domoticz\scripts\dzVents\runtime/Utils.lua:61: attempt to compare string with number
2017-09-28 08:18:00.348 dzVents: Info: ------ Finished Lux_Meter
2017-09-28 08:18:00.349 EventSystem: Script event triggered: y:\Program Files (x86)\Domoticz\scripts\dzVents\runtime\dzVents.lua

Is your lux meter showing with decimals?
Mine always shows without decimals. Are you running beta version? if you do look at this thread viewtopic.php?f=59&t=19581
// Pierre

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 13:53
by Boredcat
The LUX meter is an BH1750 connected to a Wemos D1 (with ESPEASY) and configured to give the Lux value to domoticz with 2 decimals. I will change ESPEASY to just give the number to Domoticz and use your script (I already used a script like that, I think the decimals are causing the issue).

It works, even with the extra decimals.. Great so adding the extra variable did the job.

Thanks for all your help. Now expanding the script with some more checks.

Re: Help needed with LUX script

Posted: Thursday 28 September 2017 19:42
by felix63
I was just wondering, why do you use a time trigger? Why not trigger on lux device?

Re: Help needed with LUX script

Posted: Friday 29 September 2017 9:35
by Boredcat
Hmm trigger on the LUX device. Nice idea.. doing almost the same then since my LUX sensor is updated every 60 seconds. But when the LUX value is the same as the last LUX I could have lesser events.

Not sure if the timer should be every minute (did is just for testing), using it to switch on the lights in the living room so the interval can be 5 minutes.
I like you idea of the LUX trigger, going to test that also.

As already said these are my first (baby)-steps into Domoticz scripting (I used to use blocky)

Re: Help needed with LUX script

Posted: Friday 24 November 2017 16:01
by EdwinK
I've been using the script for a few months, but after reinstalling some scripts, it doesn't work anymore :(

Code: Select all

return {

	active = true,
	
	on = {
		timer = {
			'every minute'
		}
	},

	execute = function(domoticz)
     
		local myDevice1 = domoticz.devices('Lux')
		local myDevice2 = domoticz.devices('Berging')
                local LUX = myDevice1.lux
        domoticz.log(myDevice1.name .. ' ' .. LUX, domoticz.LOG_INFO)
        domoticz.log(myDevice2.name .. ' ' .. myDevice2.state, domoticz.LOG_INFO)
		if (LUX <= 750 ) then
			myDevice2.switchOn()
			domoticz.log('Light info, The light ' .. myDevice2.name .. ' will be switched on due to LUX', domoticz.LOG_INFO)
		else 
			myDevice2.switchOff()
		end
	end
}
2017-11-24 15:59:45.244 Error: EventSystem: Lua script Low Lux did not return a commandArray
I copied and pasted it again from the post a couple posts above me. So, I am at a lost what went wrong.

Re: Help needed with LUX script

Posted: Friday 24 November 2017 17:26
by EdwinK
When I add return commandArray a little 'x' is shown in the internal editor, so that is also wrong.

Re: Help needed with LUX script

Posted: Friday 24 November 2017 17:33
by EdwinK
Aaarrghh.. /me feels stupid now. I was running this now as a LUA script instead of dzVents... :facepalm: