Page 1 of 1

If does work for on but not for off

Posted: Sunday 14 October 2018 20:24
by niwreg
The goal: do some smart things with lighting when we are home or not.

Code: Select all

return {
	on = {
		timer = {
		    '60 minutes before sunset',
		     'at 22:45'
		}
	},
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "LightAtDusk: "
    },
	execute = function(d, triggers)
	    logtekst = ''
	    d.log('The rule that triggered the event was: ' .. triggers.trigger)
	    logtekst = logtekst .. 'The rule that triggered the event was: ' .. triggers.trigger .. '\r\n'
	    --check if somebody home so 
	    d.log("Somebody status: " .. d.devices(15).state);
	    logtekst = logtekst .. "Somebody status: >" .. d.devices(15).state .. '< \r\n'
	    
	    if triggers.trigger == '60 minutes before sunset'  then
	        d.notify('Remote Debug','Ik trigger wel maar negeer de status ')
	        d.notify('Remote Debug', d.devices(15).state)
	        logtekst = logtekst .. "Inside 60 minutes before " .. '\r\n'
    	    if d.devices(15).state == "On" then
    	        logtekst = logtekst .. "Switch on? somebody home " .. '\r\n'
    	        --somebody is home! Allrighty check for lights and switch them on. 
    	        --No using scenes because i now we can use the checkFirst function to see if some lights *kuch*  (433 mhz devices) all already on.       
    	        d.devices(36).switchOn()  
    	    --end   
 
            elseif d.devices(15).state == "Off" then
                d.notify('Remote debug','Er is niemand thuis  ')
                logtekst = logtekst .. "Switch on? nobody home " .. '\r\n'
                -- switch on the lights 
                d.variable('FullAutoLights').set('1')
                d.devices(36).switchOn().checkFirst()
                
            else 
                d.notify('Domoticz!!','Blinde paniek er is onbekend of er iemand thuis is  ')
    	    end
	    end
	    if triggers.trigger == 'at 22:45' and domoticz.variables('FullAutoLights').value == '1' then
	         logtekst = logtekst .. "Inside at22:45 Switch off? nobody home " .. '\r\n'
	        d.variable('FullAutoLights').set('0')
            d.devices(36).switchOff().checkFirst()

        end
        d.log(logtekst)
	   d.notify('Remote Debug!',logtekst)
	   
	end
}
Device with IDX 15 is a dummy on/off switch. When somebody is home and the switch is On everythings works ok.

But when there is nobody home it doesn't get through the "er is niemand thuis" part. It only gets into the 60 minutes before sunset part.

Am i making an huge beginners fault or is there something i'm complelty missing?

Re: If does work for on but not for off

Posted: Sunday 14 October 2018 22:21
by waaren
niwreg wrote: Sunday 14 October 2018 20:24 The goal: do some smart things with lighting when we are home or not.
Am I making an huge beginners fault or is there something i'm completely missing?
you pass the domoticz object to your execute = function as "d" but in line

Code: Select all

if triggers.trigger == 'at 22:45' and domoticz.variables('FullAutoLights').value == '1' then
you try to get the value from uservariable "FullAutolights" from "domoticz" I would not be surprised if that is nil, causing the evaluation of this if statement to be always false. Changing this line to

Code: Select all

if triggers.trigger == 'at 22:45' and d.variables('FullAutoLights').value == '1' then
might be the solution.

Re: If does work for on but not for off

Posted: Sunday 14 October 2018 22:41
by niwreg
waaren wrote: Sunday 14 October 2018 22:21
niwreg wrote: Sunday 14 October 2018 20:24 The goal: do some smart things with lighting when we are home or not.
Am I making an huge beginners fault or is there something i'm completely missing?
you pass the domoticz object to your execute = function as "d" but in line

Code: Select all

if triggers.trigger == 'at 22:45' and domoticz.variables('FullAutoLights').value == '1' then
you try to get the value from uservariable "FullAutolights" from "domoticz" I would not be surprised if that is nil, causing the evaluation of this if statement to be always false. Changing this line to

Code: Select all

if triggers.trigger == 'at 22:45' and d.variables('FullAutoLights').value == '1' then
might be the solution.
That was possible the next thing i was going run into.

But i keep having troubles with the at hour before sunset espacialy this line:

Code: Select all

elseif d.devices(15).state == "Off" then
This never validates as true although when i look in the current states list of the events this is the result:

Code: Select all

15	Somebody Home	Off	2018-10-14 16:56:07	0

Re: If does work for on but not for off

Posted: Sunday 14 October 2018 23:36
by waaren
Not sure if I understand but your timer is now telling dzVents to execute once at 60 minutes before sunset and at 22:45.
Do you want that or do you want: "every minute between 60 minutes before sunset and sunset" and "at 22:45" ?

Re: If does work for on but not for off

Posted: Monday 15 October 2018 8:48
by niwreg
No the timer is ok.

I want to switch on the light an hour before sunset and switch a few of at 22:45

However depening if we are at home or not i want to switch some other lights as wel. But thati can't get that if to get to work.

Edit:

I created an debug version of the script and will see of this does work or that i run into some other problems.

Code: Select all

return {
	on = {
		timer = {
		    '60 minutes before sunset',
		     'at 22:45'
		}
	},
	logging = {
        level = domoticz.LOG_DEBUG,
        marker = "DebugLight: "
    },
	execute = function(d, triggers)
	    d.log('The rule that triggered the event was: ' .. triggers.trigger)
	    --check if somebody home so 
	    d.log("Somebody status: " .. d.devices(15).state);
	    
	    if triggers.trigger == '60 minutes before sunset'  then
	        d.log('Inside 60Minutes if')
    	    if d.devices(15).state == "On" then
    	        d.log('And the device 15 is on')
            elseif d.devices(15).state == "Off" then
                d.log('But the device 15 is off')
                -- switch on the lights 
            else 
                d.log('But the device 15 is not known')
    	    end
	    end
        d.log(logtekst)
	   
	end
}
I made sure that the somebody home switch is manualy controlled so that is off at the moment

Re: If does work for on but not for off

Posted: Monday 15 October 2018 18:16
by niwreg
Ok i found the error.

it's variables instead of variable

The program would crash on this line so there would be no ligts switched on