Schript react very strange

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

Moderator: leecollings

Post Reply
dutchronnie
Posts: 46
Joined: Thursday 15 August 2013 22:01
Target OS: -
Domoticz version:
Contact:

Schript react very strange

Post 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?
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Schript react very strange

Post 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
}
dutchronnie
Posts: 46
Joined: Thursday 15 August 2013 22:01
Target OS: -
Domoticz version:
Contact:

Re: Schript react very strange

Post by dutchronnie »

Thanks,
I try the extra brackets. Maybe it helps.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Schript react very strange

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dutchronnie
Posts: 46
Joined: Thursday 15 August 2013 22:01
Target OS: -
Domoticz version:
Contact:

Re: Schript react very strange

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Schript react very strange

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest