Jamming error  [Solved]

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

Moderator: leecollings

Post Reply
Knibor
Posts: 112
Joined: Sunday 20 May 2018 12:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Jamming error

Post by Knibor »

Hi,

I found a Lua Script for Jamming detection.
This script gives me an Error.
Can someone help me with this.

Z-Wave usb stick has IDX=4
Virtual device = Jamming gedetecteerd
Ip adres Domoticz 192.168.1.28:8080 No password in local network

Error: EventSystem: in Jamming gedetecteerd: [string "commandArray = {}..."]:2: attempt to call a nil value/quote]

Code: Select all

commandArray = {}
json = (loadfile "/domoticz/scripts/lua/JSON.lua")()
local url = string.format("http://192.168.1.28:8080/json.htm?type=openzwavenodes&idx=4")
url = string.format("%s %q", "curl", url)
local zwavenodes=assert(io.popen(url))
local nodes = zwavenodes:read('*all')
zwavenodes:close()
local jsonZwave = json:decode(nodes)
jammingdetected = false
for i, zwavedevice in ipairs(jsonZwave['result']) do
 if (jsonZwave['result'][i]['PollEnabled'] == 'true') then
    s = jsonZwave['result'][i]['LastUpdate']
    year = string.sub(s, 1, 4)
    month = string.sub(s, 6, 7)
    day = string.sub(s, 9, 10)
    hour = string.sub(s, 12, 13)
    minutes = string.sub(s, 15, 16)
    seconds = string.sub(s, 18, 19)
    t1 = os.time()
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    difference = (os.difftime (t1, t2))
    if (difference > 60) then
        print('Mogelijke jamming! Z-wave product niet bereikbaar: ' .. jsonZwave['result'][i]['Description'])
        jammingdetected = true
    end
 end
end
if (jammingdetected == true and otherdevices['Jamming gedetecteerd'] == 'Off') then
        commandArray['Jamming gedetecteerd']='On'
elseif (jammingdetected == false and otherdevices['Jamming gedetecteerd'] == 'On') then
        commandArray['Jamming gedetecteerd']='Off'
end    
return commandArray/code]
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Jamming error

Post by waaren »

Knibor wrote: Saturday 23 November 2019 14:07
Error: EventSystem: in Jamming gedetecteerd: [string "commandArray = {}..."]:2: attempt to call a nil value
It looks like that the JSON.lua file cannot be found by this script.

This dzVents script does hide these kind of details from you.

Code: Select all

local scriptVar = 'Jamming'

return 
{
    on = 
    {
        timer = { 'every minute' },
        httpResponses = { scriptVar }
    },

    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel
        
        local openZwaveHardwareID = 4
        local jammingDetected = false
        local jammingDetectionDevice = dz.devices('Jamming gedetecteerd')
        local timeOut = 60
        
        local function getZwaveNode(idx)
            local url =  dz.settings['Domoticz url'] .. '/json.htm?type=openzwavenodes&idx=' .. idx
            dz.openURL({ url = url, callback = scriptVar})
        end
    
        if item.isJSON then 
            local Time = require('Time')
            rt = item.json.result
               for device = 1, #rt  do
                local lastUpdatedSecondsAgo = dz.time.compare(Time(rt[device].LastUpdate)).secs
                dz.log(rt[device].Name .. '; lastUpdated: ' .. rt[device].LastUpdate .. ' (' .. lastUpdatedSecondsAgo .. ' seconds ago).' ,dz.LOG_DEBUG)
                jammingDetected = lastUpdatedSecondsAgo > timeOut 
            end
            if jammingDetected then
                jammingDetectionDevice.switchOn().checkFirst()
            else
                jammingDetectionDevice.switchOff().checkFirst()
            end
        elseif not(item.isHTTPResponse) then
            getZwaveNode(openZwaveHardwareID) 
        else
            dz.log('Problem reading result from API call. The call Returned ' .. item.data )
        end    
    end
}



Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Knibor
Posts: 112
Joined: Sunday 20 May 2018 12:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Re: Jamming error

Post by Knibor »

Hi,

I change this part of the dzVentz script with the IP address from Domoticz and it gives me "Jamming" detection alarm all the time, even when i enable polling of all my devices.

Code: Select all

local url =  dz.settings['Domoticz url'] .. '/json.htm?type=openzwavenodes&idx=' .. idx
         local url =  '192.168.1.28:8080' .. '/json.htm?type=openzwavenodes&idx=' .. idx/code]
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Jamming error

Post by waaren »

Knibor wrote: Saturday 23 November 2019 19:41 I change this part of the dzVentz script with the IP address from Domoticz and it gives me "Jamming" detection alarm all the time, even when i enable polling of all my devices.
Please see below. I add the check on pollingEnabled .

Code: Select all

local scriptVar = 'Jamming'

return 
{
    on = 
    {
        timer = { 'every minute' },
        httpResponses = { scriptVar }
    },

    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel
        
        local openZwaveHardwareID = 4
        local jammingDetected = false
        local jammingDetectionDevice = dz.devices('Jamming gedetecteerd')
        local timeOut = 60
        
        local function getZwaveNode(idx)
            local url =  dz.settings['Domoticz url'] .. '/json.htm?type=openzwavenodes&idx=' .. idx
            dz.openURL({ url = url, callback = scriptVar})
        end
    
        if item.isJSON then 
            local Time = require('Time')
            rt = item.json.result
               for device = 1, #rt  do
                local lastUpdatedSecondsAgo = dz.time.compare(Time(rt[device].LastUpdate)).secs
                dz.log(rt[device].Name .. '; lastUpdated: ' .. rt[device].LastUpdate .. ' (' .. lastUpdatedSecondsAgo .. ' seconds ago)' .. 
                       '; Polling enabled is ' .. rt[device].PollEnabled ..'.' ,dz.LOG_DEBUG)
                               
                jammingDetected = rt[device].PollEnabled == 'true' and lastUpdatedSecondsAgo > timeOut 
            end
            if jammingDetected then
                jammingDetectionDevice.switchOn().checkFirst()
            else
                jammingDetectionDevice.switchOff().checkFirst()
            end
        elseif not(item.isHTTPResponse) then
            getZwaveNode(openZwaveHardwareID) 
        else
            dz.log('Problem reading result from API call. The call Returned ' .. item.data )
        end    
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Knibor
Posts: 112
Joined: Sunday 20 May 2018 12:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Re: Jamming error

Post by Knibor »

Hi

I tried your latest script, but I get not a "Jamming gedetecteerd"
I tried to simulate with a device "Tussen versterker" I removed this device but no succes "Jamming gedetecteerd"
I don't know exact what IDX for the USB Wave stick can use. Is it 4 or 1 ? See below pictures.
Attachments
2.png
2.png (167.38 KiB) Viewed 1056 times
1.png
1.png (58.16 KiB) Viewed 1056 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Jamming error

Post by waaren »

Knibor wrote: Saturday 23 November 2019 21:32 I tried your latest script, but I get not a "Jamming gedetecteerd"
I tried to simulate with a device "Tussen versterker" I removed this device but no succes "Jamming gedetecteerd"
Can you please share what's in the log ?
I don't know exact what IDX for the USB Wave stick can use. Is it 4 or 1 ? See below pictures.
It is 4
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Knibor
Posts: 112
Joined: Sunday 20 May 2018 12:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Re: Jamming error

Post by Knibor »

Hi Waaren,

The virtual switch 'Jamming gedetecteerd' didn't switch on or off.

Is "local timeOut = 60" in the script 60 minutes ? So if I unplug the Z-Wave device "Tussen versterker, the device "Jamming gedetecteerd" switches on after 60 minutes?


Hereby the Log
2019-11-24 17:06:00.317 Status: dzVents: Info: ------ Start internal script: Jamming detection:, trigger: every minute
2019-11-24 17:06:00.337 Status: dzVents: Debug: Processing device-adapter for Jamming gedetecteerd: Switch device adapter
2019-11-24 17:06:00.337 Status: dzVents: Debug: OpenURL: url = http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4
2019-11-24 17:06:00.337 Status: dzVents: Debug: OpenURL: callback = Jamming
2019-11-24 17:06:00.337 Status: dzVents: Info: ------ Finished Jamming detection
2019-11-24 17:06:00.337 Status: dzVents: Debug: - OpenURL = {["_trigger"]="Jamming", ["method"]="GET", ["URL"]="http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4"}
2019-11-24 17:06:00.493 Status: dzVents: Debug: - HTTPResponse: Jamming
2019-11-24 17:06:00.522 Status: dzVents: Info: Handling httpResponse-events for: "Jamming
2019-11-24 17:06:00.522 Status: dzVents: Info: ------ Start internal script: Jamming detection: HTTPResponse: "Jamming"
2019-11-24 17:06:00.673 Status: dzVents: Debug: Processing device-adapter for Jamming gedetecteerd: Switch device adapter
2019-11-24 17:06:00.682 Status: dzVents: Info: ------ Finished Jamming detection

2019-11-24 17:08:00.269 Status: dzVents: Info: ------ Start internal script: Jamming detection:, trigger: every minute
2019-11-24 17:08:00.287 Status: dzVents: Debug: Processing device-adapter for Jamming gedetecteerd: Switch device adapter
2019-11-24 17:08:00.287 Status: dzVents: Debug: OpenURL: url = http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4
2019-11-24 17:08:00.287 Status: dzVents: Debug: OpenURL: callback = Jamming
2019-11-24 17:08:00.287 Status: dzVents: Info: ------ Finished Jamming detection
2019-11-24 17:08:00.288 Status: dzVents: Debug: - OpenURL = {["_trigger"]="Jamming", ["URL"]="http://127.0.0.1:8080/json.htm?type=ope ... odes&idx=4", ["method"]="GET"}
2019-11-24 17:08:00.428 Status: dzVents: Debug: - HTTPResponse: Jamming
2019-11-24 17:08:00.457 Status: dzVents: Info: Handling httpResponse-events for: "Jamming
2019-11-24 17:08:00.457 Status: dzVents: Info: ------ Start internal script: Jamming detection: HTTPResponse: "Jamming"
2019-11-24 17:08:00.583 Status: dzVents: Debug: Processing device-adapter for Jamming gedetecteerd: Switch device adapter
2019-11-24 17:08:00.587 Status: dzVents: Info: ------ Finished Jamming detection

nlfva1
Posts: 3
Joined: Saturday 04 January 2020 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Jamming error  [Solved]

Post by nlfva1 »

I have made some adjustments to the script, so that it will not reset the status, when the last zwave device has polling==false. It will also check all devices that have polling==true, so that one failing device will not immediately lead to an alarm.

Code: Select all

local scriptVar = 'Jamming'
-- --------------------------------------------------------------------------------------------------------
-- This script can be used when you have zwave door and window sensors as an alarm system. 
-- It will detect if people are trying to jam the signal.
-- The script will check all sensors with polling enabled, which you should only do on 
-- 220v powered devices, as it may drain the battery of door sensors. 
-- It will detect jamming even if you do not poll door sensors, as the jamming will block all communication. 
-- Create a virtual device "Jamming gedetecteerd" and add a notification when set to "on"
-- --------------------------------------------------------------------------------------------------------
-- History
-- 23-Nov-2019	- Knibor	- Original script
-- 23-Nov-2019	- Waaren	- Re-written in dzVents
-- 23-Nov-2019	- Waaren	- Added check on polling==true
-- 19-Feb-2020  - nlfva1	- changed polling as it would always return a false if the last device has polling=false
return 
{
    on = 
    {
        timer = { 'every minute' },
        httpResponses = { scriptVar }
    },

    logging = { level = domoticz.LOG_DEBUG },

    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel

	----------- Changes below this line       
        local openZwaveHardwareID = 20 -- Hardware ID of the Z-wave device
        local jammingDetectionDevice = dz.devices('Jamming gedetecteerd') -- Switch actived when Jamming is detected
        local timeOut = 60 -- Seconds is the time before Jamming will be declared
	----------- No changes required below this line

        local jammingDetected = false -- Start with the assumption no Jamming is happening        
        local function getZwaveNode(idx)
            local url =  dz.settings['Domoticz url'] .. '/json.htm?type=openzwavenodes&idx=' .. idx
            dz.openURL({ url = url, callback = scriptVar})
        end
    
        if item.isJSON then 
            local Time = require('Time')
            rt = item.json.result
               for device = 1, #rt  do
                local lastUpdatedSecondsAgo = dz.time.compare(Time(rt[device].LastUpdate)).secs
                dz.log(rt[device].Name .. '; lastUpdated: ' .. rt[device].LastUpdate .. ' (' .. lastUpdatedSecondsAgo .. ' seconds ago)' ..  '; Polling enabled is ' .. rt[device].PollEnabled ..'.' ,dz.LOG_DEBUG)

		-- Only check if a device has polling set to true in the zwave config
                if rt[device].PollEnabled == 'true' then 
               		jammingDetected = lastUpdatedSecondsAgo > timeOut 
                end
            end
            if jammingDetected then
                jammingDetectionDevice.switchOn().checkFirst()
            	dz.log('Switching Jamming Detection on')
            else
                jammingDetectionDevice.switchOff().checkFirst()
            end
        elseif not(item.isHTTPResponse) then
            getZwaveNode(openZwaveHardwareID) 
        else
            dz.log('Problem reading result from API call. The call Returned ' .. item.data )
        end    
    end
}

Enjoy.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest