dzvents script on change selector switch didn't fire

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

Moderator: leecollings

Post Reply
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

dzvents script on change selector switch didn't fire

Post by JanvdW »

I have created some dzvents scripts to control my Xiaomi robot cleaner: when everyone is out and he didn't clean a couple of days he will start cleaning automatically. That works fine, but of course in some cases I want to start the cleaner manually. In that case I want to update the global variable with the date of the last scan. I made a simple dzvents script to do the trick:

Code: Select all

return { on = { devices = { 97 } },                                         -- MI robot Control
	execute = function(domoticz, device)
		domoticz.log('Device ' .. device.name .. ' was changed', domoticz.LOG_INFO)
        if device.levelName == 'Clean' then
            domoticz.globalData.previousClean = tostring(dz.time.raw)       -- update global variabele previousClean
        end
	end
}
The script should be fired on a change of device 97, a selector switch that controls the robot; level 10 (Name: 'Clean') starts the robot. Unfortunately the robot reacts properly but the dzvents script to update the global variable didn't even start.

I checked the scripts by changing the 'on device' to an on/off switch, and that worked without problems.

What could be wrong? Is there an alternative, e.g. setting the global variable in some way with the level actions?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Thursday 23 April 2020 21:07 The script should be fired on a change of device 97, a selector switch that controls the robot; level 10 (Name: 'Clean') starts the robot. Unfortunately the robot reacts properly but the dzvents script to update the global variable didn't even start.
What could be wrong? Is there an alternative, e.g. setting the global variable in some way with the level actions?
No idea why the script does not start / show anything in the log but you used dz.time.raw in the script but dz is not defined.
What dzVents version are you on and how did you declare previousClean in global_data.lua ?

Tested this script and it works as expected in dzVents 3.0.2 but should also work in earlier versions ( >= 2.4.0 )

Code: Select all

return 
{ 
    on = 
    { 
        devices = 
        { 
            97,          -- MI robot Control
        }, 
    }, 

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'cleaner',
    },
    
	execute = function(dz, device)
        local lodash = dz.utils._
        
		dz.log('Device ' .. device.name .. ' was updated', dz.LOG_DEBUG)
        dz.log('LevelNames: ' .. lodash.str(device.levelNames), dz.LOG_DEBUG)
        
        if device.levelName == 'Clean' then
            dz.globalData.previousClean = dz.time.raw       -- update dzVents global variabele previousClean
        end
        
        dz.log(lodash.str(dz.globalData),dz.LOG_DEBUG)
	end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

You are right about 'dz'; should be 'domoticz' in my code. Was caused by copy/paste actions to try to solve my issue. I changed this, but script was not fired either. I have tested your code: same result. In all case the only log line is 'Status: User: Admin initiated a switch command (97/Stofzuiger - Control/Set Level)'

dzVents Version: 3.0.2
previousClean is used in other scripts as well and works fine.The global_data.lua declaration:

Code: Select all

return {
	helpers = {},
	data = {
			previousClean = { initial = "2020-01-14 07:25:00" }
	}
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Friday 24 April 2020 10:31 I have tested your code: same result. In all case the only log line is 'Status: User: Admin initiated a switch command (97/Stofzuiger - Control/Set Level)'
I cannot replicate this with the exact same device name (but off course different idx) as selector type device with and without off state hidden.

Do you use the internal editor to write and save your dzVents script?
If so; do you see anything in the log if you change something (an extra space will do) in the script and save it again?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

Yes, I use the internal editor and when I change the script a message is logged 'Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/MI Robot test.lua'
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Friday 24 April 2020 12:11 Yes, I use the internal editor and when I change the script a message is logged 'Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/MI Robot test.lua'
Can you try this ? If that does not work either it could be that the event system .dzVents are not active at all. Do you see other scripts being executed ?

Code: Select all

return 
{ 
    on = 
    { 
        devices = 
        { 
            'Stofzuiger*',
            97,          -- MI robot Control
        }, 
    }, 

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'cleaner',
    },
    
	execute = function(dz, device)
        local lodash = dz.utils._
        
        device.dump()
        
		dz.log('Device ' .. device.name .. ' was updated', dz.LOG_DEBUG)
        dz.log('LevelNames: ' .. lodash.str(device.levelNames), dz.LOG_DEBUG)
        
        if device.levelName == 'Clean' then
            dz.globalData.previousClean = dz.time.raw       -- update dzVents global variabele previousClean
        end
        
        dz.log(lodash.str(dz.globalData),dz.LOG_DEBUG)
	end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

Same result, same message in the log :-(
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Friday 24 April 2020 12:59 Same result, same message in the log :-(
Did you check if eventsystem / dzVents is active at all? Do you see other scripts being activated ?
Is device 97 set to active in the devices tab ?

If all answered with yes then maybe you can create another virtual selector device and try with that one ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

I can solve it by creating an additional on/off switch and a script that enables the robot selector switch (which start the robot) and update the variable:

Code: Select all

return { on = {  devices = { 116, }, },
    execute = function(dz, device)

        if (device.state == 'On') then
            dz.devices(97).switchSelector(10)
            dz.globalData.previousClean = tostring(dz.time.raw)
            device.switchOff()
        end
    
	end
}
But in this case I need two switches to control the robot, which is not the most beautiful solution.
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

All answers 'yes' and this was indeed exactly what I did.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Friday 24 April 2020 13:10 All answers 'yes' and this was indeed exactly what I did.
can you add dz.devices(97).dump() to this extra script. I really would like to understand why your selector switch does not trigger. It could help me in making dzVents more robust if needed. Thx !

What I meant was to create another virtual selector.
How is the original device set to the "Çlean" level ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

The selector was created by the installation process of the robot as described on https://gadget-freakz.com/adding-xiaomi ... -domoticz/ and https://www.domoticz.com/forum/viewtopi ... 20#p156581.

Creating a new selector switch (replacement of the current one) is an option, but I am not sure how that interferes with the plugin.

The updated script results in the following output:

Code: Select all

2020-04-24 15:11:08.269 Status: dzVents: > daysAgo: 3 
2020-04-24 15:11:08.269 Status: dzVents: > yday: 112 
2020-04-24 15:11:08.269 Status: dzVents: > hour: 10 
2020-04-24 15:11:08.269 Status: dzVents: > rawTime: 10:24:14 
2020-04-24 15:11:08.269 Status: dzVents: > min: 24 
2020-04-24 15:11:08.269 Status: dzVents: > hoursAgo: 76 
2020-04-24 15:11:08.269 Status: dzVents: > time: 10:24 
2020-04-24 15:11:08.269 Status: dzVents: > millisecondsAgo: 276414198 
2020-04-24 15:11:08.269 Status: dzVents: > year: 2020 
2020-04-24 15:11:08.269 Status: dzVents: > addMinutes() 
2020-04-24 15:11:08.269 Status: dzVents: > ruleIsAtCivilDayTime() 
2020-04-24 15:11:08.269 Status: dzVents: > compare() 
2020-04-24 15:11:08.269 Status: dzVents: > ruleIsAfterCivilTwilightStart() 
2020-04-24 15:11:08.269 Status: dzVents: > ruleMatchesTime() 
2020-04-24 15:11:08.269 Status: dzVents: > getISO() 
2020-04-24 15:11:08.269 Status: dzVents: > isUTC: false 
2020-04-24 15:11:08.269 Status: dzVents: > ruleIsAtSunrise() 
2020-04-24 15:11:08.269 Status: dzVents: > week: 17 
2020-04-24 15:11:08.269 Status: dzVents: > addHours() 
2020-04-24 15:11:08.269 Status: dzVents: > utils: 
2020-04-24 15:11:08.269 Status: dzVents: > stringSplit() 
2020-04-24 15:11:08.269 Status: dzVents: > DZVERSION: 3.0.2 
2020-04-24 15:11:08.269 Status: dzVents: > print() 
2020-04-24 15:11:08.269 Status: dzVents: > urlDecode() 
2020-04-24 15:11:08.269 Status: dzVents: > inTable() 
2020-04-24 15:11:08.269 Status: dzVents: > LOG_FORCE: 0.5 
2020-04-24 15:11:08.269 Status: dzVents: > sceneExists() 
2020-04-24 15:11:08.269 Status: dzVents: > toBase64() 
2020-04-24 15:11:08.269 Status: dzVents: > fromJSON() 
2020-04-24 15:11:08.269 Status: dzVents: > fromBase64() 
2020-04-24 15:11:08.269 Status: dzVents: > LOG_INFO: 3 
2020-04-24 15:11:08.269 Status: dzVents: > urlEncode() 
2020-04-24 15:11:08.269 Status: dzVents: > numDecimals() 
2020-04-24 15:11:08.269 Status: dzVents: > toCelsius() 
2020-04-24 15:11:08.269 Status: dzVents: > setLogMarker() 
2020-04-24 15:11:08.269 Status: dzVents: > toJSON() 
2020-04-24 15:11:08.269 Status: dzVents: > log() 
2020-04-24 15:11:08.269 Status: dzVents: > stringToSeconds() 
2020-04-24 15:11:08.269 Status: dzVents: > rgbToHSB() 
2020-04-24 15:11:08.269 Status: dzVents: > fromXML() 
2020-04-24 15:11:08.269 Status: dzVents: > centerPad() 
2020-04-24 15:11:08.270 Status: dzVents: > dumpTable() 
2020-04-24 15:11:08.270 Status: dzVents: > cameraExists() 
2020-04-24 15:11:08.270 Status: dzVents: > round() 
2020-04-24 15:11:08.270 Status: dzVents: > variableExists() 
2020-04-24 15:11:08.270 Status: dzVents: > groupExists() 
2020-04-24 15:11:08.270 Status: dzVents: > deviceExists() 
2020-04-24 15:11:08.270 Status: dzVents: > rightPad() 
2020-04-24 15:11:08.270 Status: dzVents: > toXML() 
2020-04-24 15:11:08.270 Status: dzVents: > leftPad() 
2020-04-24 15:11:08.270 Status: dzVents: > fileExists() 
2020-04-24 15:11:08.270 Status: dzVents: > leadingZeros() 
2020-04-24 15:11:08.270 Status: dzVents: > hsbToRGB() 
2020-04-24 15:11:08.270 Status: dzVents: > LOG_MODULE_EXEC_INFO: 2 
2020-04-24 15:11:08.270 Status: dzVents: > LOG_DEBUG: 4 
2020-04-24 15:11:08.270 Status: dzVents: > osExecute() 
2020-04-24 15:11:08.270 Status: dzVents: > LOG_ERROR: 1 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsBeforeCivilTwilightEnd() 
2020-04-24 15:11:08.270 Status: dzVents: > milliSeconds: 0 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsAtNight() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleMatchesBetweenRange() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleMatchesTimeRange() 
2020-04-24 15:11:08.270 Status: dzVents: > isdst: true 
2020-04-24 15:11:08.270 Status: dzVents: > month: 4 
2020-04-24 15:11:08.270 Status: dzVents: > minutesAgo: 4606 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsAtCivilNightTime() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsAtDayTime() 
2020-04-24 15:11:08.270 Status: dzVents: > matchesRule() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsAfterSunset() 
2020-04-24 15:11:08.270 Status: dzVents: > addDays() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsBeforeCivilTwilightStart() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsOnDate() 
2020-04-24 15:11:08.270 Status: dzVents: > minutes: 24 
2020-04-24 15:11:08.270 Status: dzVents: > seconds: 14 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsAtCivilTwilightEnd() 
2020-04-24 15:11:08.270 Status: dzVents: > raw: 2020-04-21 10:24:14 
2020-04-24 15:11:08.270 Status: dzVents: > monthName: April 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsAfterSunrise() 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsBeforeSunrise() 
2020-04-24 15:11:08.270 Status: dzVents: > minutesSinceMidnight: 624 
2020-04-24 15:11:08.270 Status: dzVents: > sec: 14 
2020-04-24 15:11:08.270 Status: dzVents: > dayAbbrOfWeek: tue 
2020-04-24 15:11:08.270 Status: dzVents: > monthAbbrName: apr 
2020-04-24 15:11:08.270 Status: dzVents: > ruleIsInWeek() 
2020-04-24 15:11:08.270 Status: dzVents: > makeTime() 
2020-04-24 15:11:08.270 Status: dzVents: > dDate: 1587457454 
2020-04-24 15:11:08.270 Status: dzVents: > milliseconds: 0 
2020-04-24 15:11:08.270 Status: dzVents: > dayName: Tuesday 
2020-04-24 15:11:08.270 Status: dzVents: > msAgo: 276414198 
2020-04-24 15:11:08.270 Status: dzVents: > state: Find 
2020-04-24 15:11:08.270 Status: dzVents: > updateTempBaro() 
2020-04-24 15:11:08.270 Status: dzVents: > updateSoilMoisture() 
2020-04-24 15:11:08.270 Status: dzVents: > setHue() 
2020-04-24 15:11:08.270 Status: dzVents: > setKelvin() 
2020-04-24 15:11:08.270 Status: dzVents: > setDiscoMode() 
2020-04-24 15:11:08.270 Status: dzVents: > levelVal: 60 
2020-04-24 15:11:08.270 Status: dzVents: > changed: false 
2020-04-24 15:11:08.270 Status: dzVents: > dump() 
2020-04-24 15:11:08.271 Status: dzVents: Info: ------ Finished MI Robot manual start
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Friday 24 April 2020 15:26 The selector was created by the installation process of the robot as described on https://gadget-freakz.com/adding-xiaomi ... -domoticz/ and https://www.domoticz.com/forum/viewtopi ... 20#p156581.
My understanding from this process is that an On/Off switch is used to control the selector switch.

What do you use to set the selector to Clean ? And have you tried to switch it manually ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

Indeed, controlling the selector switch with the on/off switch works as expected.

I start cleaning automatically (dzvents script depending on presence - just checked it by faking absence and works still fine).
Manual use of the selector switch in Domoticz or Dashticz works fine.
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

I looked with a fresh view to this issue and saw that the device value doesn't change when a different level is selected. The value of the selector switch is always 'Off', but the level of the selector switch changes definitely and triggers the robot to do something, depending on the selected level. I suppose that dzvents doesn't recognize hat the selector has been changed, so that the event is not fired. Any idea which trigger I can use to recognize a level change of the selector switch to fire the dzvents script?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents script on change selector switch didn't fire

Post by waaren »

JanvdW wrote: Friday 15 May 2020 9:59 The value of the selector switch is always 'Off', but the level of the selector switch changes definitely
Sorry but I don't understand this.
How / where do you see the value Off and how / where do you see the changed selector level?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
JanvdW
Posts: 118
Joined: Saturday 21 December 2019 8:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzvents script on change selector switch didn't fire

Post by JanvdW »

The value 'Off' is listed in the 'Data' field of the Domoticz device list. And, of course, also as the status for the device on the 'LightSwitches' tab (right upper corner of the switch. I select the selector value on the device in Domoticz or in Dashticz; that doesn't matter. The robot command is executed by the robot, so that works apparently. But I was surprised that no history of values is shown when I select the log button on the switch.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest