Page 1 of 1

Schript react very strange

Posted: Wednesday 10 June 2020 8:43
by dutchronnie
I am running the following script:

Code: Select all

return {
	active = true,
	on = {
	    timer = {
	        'at 07:00',
	        'at 19:20',
	        'at 11:30',
	        },
	    },
	
	execute = function(domoticz, myDevice, triggerInfo)
		local myDevice1 = domoticz.devices('Espressomachine')
		local myDevice2 = domoticz.devices('iDetect - Ronald')
		local myDevice3 = domoticz.devices('iDetect - Regina')

		if (triggerInfo.trigger == 'at 07:00') and (myDevice2.state == 'On') or (triggerInfo.trigger == 'at 19:20') and (myDevice2.state == 'On') or (myDevice3.state == 'On') then
		    myDevice1.switchOn().checkFirst()
		elseif (triggerInfo.trigger == 'at 11:30') then
            myDevice1.switchOff().checkFirst()
		end
	end
}
The script has to turn on my Espresso machine at 7:00 in the morning when i am at home (Ronald)
And at 19:20 when i at home or my wife (Regina)is at home.
And turn of the Espresso machine at 11:30 in the morning

But many often it turns on the machine on at 7:00 and an half hour later it turns of the machine.
Or in the evening when domoticz turns on the lights in the house, it turns on the espresso machine, but in that script there is no action for the espresso machine.

Is there a error in my script?

Re: Schript react very strange

Posted: Wednesday 10 June 2020 8:59
by felix63
Hi Ron,

When creating complex conditions I always throw in some extra brackets to make sure it works as I expect. For 'and' has higer precedence than 'or'. Haven't checked if this is truly the case here but you could check this version.

Code: Select all

return {
	active = true,
	on = {
	    timer = {
	        'at 07:00',
	        'at 19:20',
	        'at 11:30',
	        },
	    },
	
	execute = function(domoticz, myDevice, triggerInfo)
		local myDevice1 = domoticz.devices('Espressomachine')
		local myDevice2 = domoticz.devices('iDetect - Ronald')
		local myDevice3 = domoticz.devices('iDetect - Regina')

		if (triggerInfo.trigger == 'at 07:00' and myDevice2.state == 'On') or 
			(triggerInfo.trigger == 'at 19:20' and (myDevice2.state == 'On' or myDevice3.state == 'On')) then
		    		myDevice1.switchOn().checkFirst()
			elseif (triggerInfo.trigger == 'at 11:30') then
            			myDevice1.switchOff().checkFirst()
			end
		end
}

Re: Schript react very strange

Posted: Wednesday 10 June 2020 10:17
by dutchronnie
Thanks,
I try the extra brackets. Maybe it helps.

Re: Schript react very strange

Posted: Wednesday 10 June 2020 10:37
by waaren
dutchronnie wrote: Wednesday 10 June 2020 10:17 Thanks,
I try the extra brackets. Maybe it helps.
This script is only trigered at 07:00, 19:20 and 11:30. If you see unexpected behavior at other times it cannot be caused by this script.

If you search for bugs or logic errors it is always good to add some log statements so you can see what happens. When everything is OK you can set the log level to LOG_ERROR which will show only errors if and when they occur.

Code: Select all

return
{
    active = true,

    on =
    {
        timer =
        {
            'at 07:00',
            'at 19:20',
            'at 11:30',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when everything works as expected
        marker = 'Espresso',
    },

    execute = function(domoticz, myDevice, triggerInfo)
        local myDevice1 = domoticz.devices('Espressomachine')
        local myDevice2 = domoticz.devices('iDetect - Ronald')
        local myDevice3 = domoticz.devices('iDetect - Regina')

        domoticz.log(myDevice2.name .. ' state is ' .. myDevice2.state ,domoticz.LOG_DEBUG)
        domoticz.log(myDevice3.name .. ' state is ' .. myDevice3.state ,domoticz.LOG_DEBUG)

        if (triggerInfo.trigger == 'at 07:00' and myDevice2.state == 'On') or
            (triggerInfo.trigger == 'at 19:20' and (myDevice2.state == 'On' or myDevice3.state == 'On')) then

            myDevice1.switchOn().checkFirst()
            domoticz.log(myDevice1.name .. ' will be switched on (if not already on)' ,domoticz.LOG_DEBUG)

        elseif (triggerInfo.trigger == 'at 11:30') then

                myDevice1.switchOff().checkFirst()
                domoticz.log(myDevice1.name .. ' will be switched off (if stil on)' ,domoticz.LOG_DEBUG)

            end
        end
}

Re: Schript react very strange

Posted: Wednesday 10 June 2020 12:23
by dutchronnie
Thanks,
I add the Log statements.

Maybe the errors are caused by another script, but till now i can't find de cause.
I hope the log statements do clear something for me

Re: Schript react very strange

Posted: Wednesday 10 June 2020 12:31
by waaren
dutchronnie wrote: Wednesday 10 June 2020 12:23 Thanks,
I add the Log statements.

Maybe the errors are caused by another script, but till now i can't find de cause.
I hope the log statements do clear something for me
I forgot to add the 2 and 3 in the log statements after myDevice. Now corrected it in my previous post.