dimTo not doing what I would expect.  [Solved]

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

Moderator: leecollings

Post Reply
bert
Posts: 17
Joined: Monday 27 October 2014 23:57
Target OS: Linux
Domoticz version:
Contact:

dimTo not doing what I would expect.

Post by bert »

V4.10717

Wrote a little script to switch on a dimmer at a certain level (50%) for a number of minutes when a doorbell is pressed.
If pressed again while the light is on, the light should be a tad brighter (50 + 25 = 75%).
if pressed again, the light must switch off.

For some reason though, dimTo seems to fail setting 50%...

Code: Select all

local INPUT_DEV='Elin drukknop'
local OUTPUT ='Hue Ikea lamp PC beneden'
local INIT_LEVEL = 50
local LEVEL_STEP = 25
local DURATION = 1

return {
	on = {
		devices = {
			INPUT_DEV                                                 
			}
		},
	execute = function(domoticz, switch)
	local dimmer = domoticz.devices(OUTPUT) 
	if (switch.state == 'Chime') then
		domoticz.log('dimmerlevel: ' .. dimmer.level .. '; INIT_LEVEL: '.. INIT_LEVEL ..'; dimmerstate: ' .. dimmer.state , domoticz.LOG_INFO)
		if (dimmer.level == INIT_LEVEL) then
			domoticz.log('!dimmerlevel = INIT_LEVEL. dimmerlevel: ' .. dimmer.level , domoticz.LOG_INFO)
			dimmer.dimTo(dimmer.level + LEVEL_STEP)
			domoticz.log('dimmerlevel = INIT_LEVEL+LEVEL_STEP. dimmerlevel: ' .. dimmer.level , domoticz.LOG_INFO)
		elseif (dimmer.state == 'Off') then
			domoticz.log('dimmerstate = off. dimmerlevel: ' .. dimmer.level , domoticz.LOG_INFO)
			dimmer.dimTo(INIT_LEVEL)
			domoticz.log('dimmerstate = off; dimto '.. INIT_LEVEL .. '. dimmerlevel: ' .. dimmer.level , domoticz.LOG_INFO) 
			--dimmer.dump()
			dimmer.switchOff().checkFirst().afterMin(DURATION)
			dimmer.switchOn().checkFirst().forMin(DURATION)
		else
			dimmer.cancelQueuedCommands()
			dimmer.switchOff()                                      
		end
	end
	domoticz.log('Device ' .. dimmer.name .. ' was changed', domoticz.LOG_INFO)
end
}
pressing the doorbell switch results in the following:

Code: Select all

2019-11-22 00:26:32.932  (rfxcom) Lighting 1 (Elin drukknop)
2019-11-22 00:26:33.016  Status: dzVents: Info:  Handling events for: "Elin drukknop", value: "Chime"
2019-11-22 00:26:33.016  Status: dzVents: Info:  ------ Start internal script: DzVents device Elin drukknop: Device: "Elin drukknop (rfxcom)", Index: 656
2019-11-22 00:26:33.017  Status: dzVents: Info:  dimmerlevel: 75; INIT_LEVEL: 50; dimmerstate: Off
2019-11-22 00:26:33.018  Status: dzVents: Info:  dimmerstate = off. dimmerlevel: 75
2019-11-22 00:26:33.018  Status: dzVents: Info:  dimmerstate = off; dimto 50. dimmerlevel: 75
2019-11-22 00:26:33.018  Status: dzVents: Info:  Device Hue Ikea lamp PC beneden was changed
2019-11-22 00:26:33.018  Status: dzVents: Info:  ------ Finished DzVents device Elin drukknop
And the light is at 75%!
Last edited by bert on Friday 22 November 2019 2:23, edited 2 times in total.
bert
Posts: 17
Joined: Monday 27 October 2014 23:57
Target OS: Linux
Domoticz version:
Contact:

Re: dimTo not doing what I would expect.

Post by bert »

Hmmm...
it seems to be caused by

Code: Select all

		
		dimmer.switchOff().checkFirst().afterMin(DURATION)
		dimmer.switchOn().checkFirst().forMin(DURATION)
removing the second line resolves the issue.. not sure why though.. just trying to follow the wiki :-/
Last edited by bert on Friday 22 November 2019 1:10, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dimTo not doing what I would expect.

Post by waaren »

bert wrote: Friday 22 November 2019 0:30 Wrote a little script to switch on a dimmer at a certain level (50%) for a number of minutes when a doorbell is pressed.
Please note that when you change the state or level of a device in a dzVents or Lua script, the change will only take place after the script finished. The command is stored in a table and that table is passed back to domoticz when the script finished.
So you cannot check the result of a command during the execution of the originating script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bert
Posts: 17
Joined: Monday 27 October 2014 23:57
Target OS: Linux
Domoticz version:
Contact:

Re: dimTo not doing what I would expect.  [Solved]

Post by bert »

https://www.domoticz.com/wiki/DzVents:_ ... _scripting
the wiki states:
..... And there you have it: the light is not switched off and never will be because future commands will always be checked against the current on-state.
That's just how it works and you will have to deal with it in your script. So, instead of simply re-issuing switchOn().forMin(5) you have to check the switch's state first:

if (light.active) then
light.switchOff().afterMin(5)
else
light.switchOn().forMin(5)
end

or issue these two commands both as they are mutually exclusive:

light.switchOff().checkFirst().afterMin(5)
light.switchOn().checkFirst().forMin(5)
For dimmers it seems a bit more tricky though; checking seems to go wrong..

anyway, the following seems to work as expected; works for me now..

Code: Select all

local INPUT_DEV='Elin drukknop'
local OUTPUT ='Hue Ikea lamp PC beneden'

local INIT_LEVEL = 50
local LEVEL_STEP = 25
local DURATION = 1

return {
	on = {
		devices = {
			INPUT_DEV                                                 
			}
		},
	execute = function(domoticz, switch)
	local dimmer = domoticz.devices(OUTPUT) 
	if (switch.state == 'Chime') then
		if (dimmer.level == INIT_LEVEL) then
		    dimmer.cancelQueuedCommands()
			dimmer.dimTo(LEVEL_STEP + INIT_LEVEL)
			dimmer.switchOff().checkFirst().afterMin(DURATION)
			
		elseif (dimmer.state == 'Off') then
			dimmer.dimTo(INIT_LEVEL).forMin(DURATION)

		else
			dimmer.cancelQueuedCommands()
			dimmer.switchOff()                                      
		end
	end
	domoticz.log('Device ' .. dimmer.name .. ' was changed', domoticz.LOG_INFO)
end
}
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest