Error in dzVents Script  [Solved]

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

Moderator: leecollings

Post Reply
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Error in dzVents Script

Post by gschmidt »

Hi,

I have created a dzVents script which activates a selftest for 3 Smoke detector switches (zigbee2Mqtt devices).
But I get a "nil" valuefor the second device...what am I missing here?

Code: Select all

-- Rookmelders Test  with Notification

local Rookmelders = {'Rookmelder_beneden (Test)','Rookmelder_boven (Test)','Rookmelder_zolder (Test)'}

return {
    
    on =
    {
        timer =
        {
            --'at 17:30 on 15/*',
            'at 11:44',
        },

        devices =
        { 
            Rookmelders,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Rookmelders Test',
    },
   
    execute = function(dz, timer)
        
        if timer.isTimer then
            for index,Name in ipairs(Rookmelders) do
                dz.devices(Name).cancelQueuedCommands()    
                dz.devices(Name).switchOn()
                dz.log(Name .. ' has been pushed to test the device monthly!', dz.LOG_INFO)
                dz.notify(Name, 'Maandelijkse test van ' .. Name .. ' !', dz.PRIORITY_HIGH,'','',dz.NSS_TELEGRAM)
            end
        end 
    end
}
This is the LOG:
2020-11-14 11:44:00.728 Notification sent (telegram) => Success
2020-11-14 11:44:00.305 Status: dzVents: Info: Rookmelders Test: ------ Start internal script: Rookmelder Test:, trigger: "at 11:44"
2020-11-14 11:44:00.319 Status: dzVents: Debug: Rookmelders Test: Processing device-adapter for Rookmelder_beneden (Test): Switch device adapter
2020-11-14 11:44:00.319 Status: dzVents: Debug: Rookmelders Test: Constructed timed-command: On
2020-11-14 11:44:00.319 Status: dzVents: Info: Rookmelders Test: Rookmelder_beneden (Test) has been pushed to test the device monthly!
2020-11-14 11:44:00.319 Status: dzVents: Info: Rookmelders Test: ------ Finished Rookmelder Test
2020-11-14 11:44:00.322 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-11-14 11:44:00.353 Status: Notification: Rookmelder_beneden (Test)
2020-11-14 11:44:00.319 Error: dzVents: Error: (3.0.15) Rookmelders Test: There is no device with that name or id: Rookmelder_boven (Test)
2020-11-14 11:44:00.319 Error: dzVents: Error: (3.0.15) Rookmelders Test: An error occurred when calling event handler Rookmelder Test
2020-11-14 11:44:00.319 Error: dzVents: Error: (3.0.15) Rookmelders Test: ...cz/scripts/dzVents/generated_scripts/Rookmelder Test.lua:31: attempt to index a nil value
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in dzVents Script

Post by waaren »

gschmidt wrote: Saturday 14 November 2020 11:50 But I get a "nil" valuefor the second device...what am I missing here?

Code: Select all

2020-11-14 11:44:00.319 Error: dzVents: Error: (3.0.15) Rookmelders Test: There is no device with that name or id: Rookmelder_boven (Test)
According to this Error, no device named "Rookmelder_boven (Test)" exists on your system. Have you double checked the name for missed spaces, case etc ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error in dzVents Script  [Solved]

Post by gschmidt »

waaren wrote: Saturday 14 November 2020 12:36
gschmidt wrote: Saturday 14 November 2020 11:50 But I get a "nil" valuefor the second device...what am I missing here?

Code: Select all

2020-11-14 11:44:00.319 Error: dzVents: Error: (3.0.15) Rookmelders Test: There is no device with that name or id: Rookmelder_boven (Test)
According to this Error, no device named "Rookmelder_boven (Test)" exists on your system. Have you double checked the name for missed spaces, case etc ?
Yep, you’re right...there were 2 spaces in the device name "Rookmelder_boven (Test)", the script had one space.

I have another question, sometimes the test failes in zigbee2mqtt for these Xiaomi MiJia Honeywell devices, because of a timeout error.
How difficult is it to program a new attempt, if this time out error happens?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in dzVents Script

Post by waaren »

gschmidt wrote: Saturday 14 November 2020 16:31 How difficult is it to program a new attempt, if this time out error happens?
Where / how do you recognize this timeout ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error in dzVents Script

Post by gschmidt »

In the logs of zigbee2mqtt and domoticz. Next time I have this issue I will post the domoticz log error. The script runs once a month...the test sound of the smoke alarm is to annoying (angry wife :D ) to test frequently. When I am alone I will test it manually.
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error in dzVents Script

Post by gschmidt »

waaren wrote: Saturday 14 November 2020 16:49
gschmidt wrote: Saturday 14 November 2020 16:31 How difficult is it to program a new attempt, if this time out error happens?
Where / how do you recognize this timeout ?
Hi I can report a time error error now:
When performing a selftest on the (zigbee2mqtt) domoticz device e.g. "Rookmelder_zolder (Test)", the domoticz log returns the following time out error:

Code: Select all

2020-11-28 12:36:27.812 Error: (Zigbee2MQTT) A Zigbee publish error occured for device 'Rookmelder_zolder' with error message: Publish 'set' 'sensitivity' to 'Rookmelder_zolder' failed: 'Error: Write 0x00158d000358480c/1 ssIasZone({"65521":{"value":67239936,"type":35}}, {"timeout":35000,"disableResponse":false,"disableRecovery":false,"disableDefaultResponse":true,"direction":0,"srcEndpoint":null,"reservedBits":0,"manufacturerCode":4447,"transactionSequenceNumber":null}) failed (Timeout - 21785 - 1 - 9 - 1280 - 4 after 35000ms)'
The error occurs when the smoke alarm has a bad link quality in the zigbee network.
This link quality fluctuates, so sometimes, if the link is very low, a timeout error occurs.

Is it possible to re-run the selftest let's say 3 times, if still a timeout occurs then notify this?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in dzVents Script

Post by waaren »

gschmidt wrote: Saturday 28 November 2020 13:06 Is it possible to re-run the selftest let's say 3 times, if still a timeout occurs then notify this?
I would not know how to do this in dzVents or any other type script. Have you tried the zigbee2mqtt issue list ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Error in dzVents Script

Post by gschmidt »

waaren wrote: Saturday 28 November 2020 13:12
gschmidt wrote: Saturday 28 November 2020 13:06 Is it possible to re-run the selftest let's say 3 times, if still a timeout occurs then notify this?
I would not know how to do this in dzVents or any other type script. Have you tried the zigbee2mqtt issue list ?
The Timeout time is 35000ms (35s) and These are 3 domoticz devices (push buttons):

Code: Select all

local Rookmelders = {'Rookmelder_beneden (Test)','Rookmelder_boven (Test)','Rookmelder_zolder (Test)'}
With a foreach loop these devices are switched on: "dz.devices(Name).switchOn()"

Is it possible to wait with the next device in list until the first device is finished:
If first device is pushed, wait for 40s, if an error occurs and returned in the log then
repeat the push wait 40sec, etc. Try 3 times, then notify the error and continue with the next device in list

else continue with the next device in list

Is this not possible?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in dzVents Script

Post by waaren »

gschmidt wrote: Saturday 28 November 2020 14:32 Is it possible to re-run the selftest let's say 3 times, if still a timeout occurs then notify this?
Can you share the script you have so far ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in dzVents Script

Post by waaren »

gschmidt wrote: Saturday 28 November 2020 14:32 If first device is pushed, wait for 40s, if an error occurs and returned in the log then
repeat the push wait 40sec, etc. Try 3 times, then notify the error and continue with the next device in list
Can you try this one?

Code: Select all

--[[
        Script to (self)test smoke detectors once a month.

        test is repeated for a user defined times if a timeout error message is in the log file after the test.

        test API calls
        http://<domoticz IP:domoticz port>/json.htm?type=command&param=addlogmessage&message=Rookmelder_beneden%20%28Test%29%20failed&level=4
        http://<domoticz IP:domoticz port>/json.htm?type=command&param=addlogmessage&message=Rookmelder_boven%20%28Test%29%20failed&level=4
        http://<domoticz IP:domoticz port>/json.htm?type=command&param=addlogmessage&message=Rookmelder_zolder%20%28Test%29%20failed&level=4

        history:
        20201114 Initial code from gschmidt
        20201128 Add log check
        20201129 Loop over devices

]]--

local scriptVar = 'Monthly smokedetector selftest'
return
{
    on =
    {
        timer =
        {
            'at 17:30 on 15/*',
            -- 'at 21:21', -- Just for test. Can be removed
        },
        httpResponses =
        {
            scriptVar .. '*',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = scriptVar,
    },

    execute = function(dz, item)

        --- Your settings below this line

        local smokeDetectors = {'Rookmelder_beneden (Test)','Rookmelder_boven (Test)','Rookmelder_zolder (Test)'}
        local maxRetries = 3
        local timeOut = 45

        local myNotificationTable =
        {
             -- table with one or more notification systems.
             -- uncomment the notification systems that you want to be used
             -- Can be one or more of

             -- dz.NSS_FIREBASE_CLOUD_MESSAGING,
             dz.NSS_PUSHOVER,
             -- dz.NSS_HTTP,
             -- dz.NSS_KODI,
             -- dz.NSS_LOGITECH_MEDIASERVER,
             -- dz.NSS_NMA,
             -- dz.NSS_PROWL,
             -- dz.NSS_PUSHALOT,
             -- dz.NSS_PUSHBULLET,
             -- dz.NSS_PUSHOVER,
             -- dz.NSS_PUSHSAFER,
             dz.NSS_TELEGRAM,
        }

        --- No changes required below this line

        local logLevel = { all = 268435455, normal = 1, status = 2, error = 4 }
        local deviceName, callback
        local delay = timeOut + 5

        local function getIndex(t, v)
            for key, value in pairs(t) do
               if v == value then return key end
            end
        end

        local function askDomoticzForLogLines(callback, lastLogTime, delay)
            local jsonString = '/json.htm?type=command' .. '&param=getlog' .. '&lastlogtime=' .. lastLogTime .. '&loglevel=' ..  logLevel.error
            local logURL      =  dz.settings['Domoticz url'] .. jsonString
            dz.openURL  (
            {
                url      = logURL,
                callback = callback,
            }).afterSec(delay)
        end

        local function triggerTestControl(index, try)
            if index > #smokeDetectors then return end
            local callback = scriptVar .. ' (' .. smokeDetectors[index] .. ') ' .. try
            dz.devices(smokeDetectors[index]).switchOn()
            askDomoticzForLogLines(callback, os.time() , delay )
        end

        if item.isTimer then
            triggerTestControl(1,1)
        elseif item.isHTTPResponse and item.isJSON then
            local deviceName = item.trigger:match("%b()"):sub(2,-2):gsub('%s*$','') -- remove () and trailing space
            local timeoutCount = tonumber( item.trigger:sub(-1,-1) )
            local found = false
            local withoutMagic = true
            local timedOut = 'failed'

            if item.json.result then
                for _, logLine in ipairs(item.json.result) do
                    if logLine.message:find(deviceName, 1, withoutMagic ) and logLine.message:find(timedOut, 1, withoutMagic) then
                        found = true
                        break
                    end
                end

                if found then
                    if timeoutCount < maxRetries then
                        triggerTestControl(getIndex(smokeDetectors, deviceName), timeoutCount + 1)
                    else
                        local msg = 'Selftest for ' .. deviceName .. ' timed out for ' .. timeoutCount .. ' times; Giving up now!'
                        dz.log(msg, dz.LOG_ERROR)
                        dz.notify(scriptVar, msg, dz.PRIORITY_HIGH, nil, nil, myNotificationTable)
                        triggerTestControl(getIndex(smokeDetectors, deviceName) + 1, 1)
                    end
                    return
                end
            end
            local msg = 'Selftest for ' .. deviceName .. ' OK'
            dz.log(msg, dz.LOG_DEBUG)
            dz.notify(scriptVar, msg, dz.PRIORITY_NORMAL, nil, nil, myNotificationTable)
            triggerTestControl(getIndex(smokeDetectors, deviceName) + 1, 1)
        else
            dz.log('Access problem to log', dz.LOG_ERROR)
            dz.log(item, dz.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
Post Reply

Who is online

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