Author Topic: Anyway to detect what tripped a device or see all the scene it belongs to?  (Read 363 times)

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
I have one particular light that comes on a couple times a day and I can't figure out what caused it to do so. Is there a way to detect what triggered it?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #2 on: November 09, 2017, 08:42:53 pm »
This might help...

http://forum.micasaverde.com/index.php/topic,15360.msg116759.html#msg116759

followed your instruction, installed datamine modified first line to json-dm lua ran just fine but my text come out with only scene: and blank from there on.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #3 on: November 10, 2017, 03:05:39 am »
Ah, it may be that recent firmware encodes the response. I'll try and take a look.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #4 on: November 10, 2017, 12:25:47 pm »
I forgot to mention I'm on ui5

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #5 on: November 11, 2017, 08:07:14 am »
followed your instruction, installed datamine modified first line to json-dm lua ran just fine but my text come out with only scene: and blank from there on.

I can replicate this error, although the code runs fine under openLuup. 

Probably something, as expected, with encoded strings.  Will dig further if you're still interested.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #6 on: November 11, 2017, 08:11:52 am »
OK, so this error arises from not finding the json module, so you need to make sure that you're requiring it correctly.

You could run a little test code to ensure that you can find and use json.decode()
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #7 on: November 11, 2017, 12:03:09 pm »
Thanks for looking into it, I also upload the dkjson file as well but that didn't work either. How do I test for the json file?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #8 on: November 11, 2017, 12:51:16 pm »
Are you using AltUI?  If not, why not?  (It makes it easier to debug code, because you can use print statements.)
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #9 on: November 11, 2017, 01:07:42 pm »
Are you using AltUI?  If not, why not?  (It makes it easier to debug code, because you can use print statements.)

I had it installed but found that I never used it so I deleted it. I'll reinstall it and see if there's any useful things I can do to troubleshoot it.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #10 on: November 11, 2017, 01:33:58 pm »
installed alt ui and tried again and got this error

?      11/11/2017, 10:32:11 AM   Code execution failed   
?      11/11/2017, 10:32:11 AM   Lua Command execution on vera failed.()

below is what I have. Basically a copy and paste of your script with the added json-dm. Tried this on the code from the first post as well


Code: [Select]
-- Build list of scenes with trigger devices and actioned devices
--
-- version = "2013.11.14 @akbooer"
--

json = require "json-dm"

-- sceneBlog (), build list of scenes with trigger devices and actioned devices
-- as posted to http://forum.micasaverde.com/index.php/topic,15360.msg116759.html#msg116759
local function sceneBlog (filename)

-- classic map utility - cf. pairs
local function map (Xs , fct)   -- map function to each item in table, returns {} if none
local table = {}
for i,x in pairs (Xs or {}) do table[i] = fct(x) end
return table
end

-- classic list flatten
local function flatten (array)
local l = {}
local function add_item (x) l[#l+1] = x; end
for _,x in ipairs(array) do
if type(x) == "table" then
map(flatten(x), add_item )
else
add_item (x)
end
end
return l
end

-- set operations, only what we need here: add, list
local function set() -- create new empty set
local s = {} -- holder for set
return {
add  = function (x) s[x] = x; end, -- add element to the set
list = function ( ) -- return sorted list of set elements
local l = {};
for i in pairs(s) do l[#l+1] = i; end;
table.sort (l);
return l; end
}
end

-- formatting functions for printing data structure
local function format_devices(d)
return map (d, function (x) return string.format ('[%d] %s', x, (luup.devices[tonumber(x)] or {description = ''}).description); end)
end

local function format_names(d)
return map (d, function (x) return x.name or ''; end)
end

local function format_timers (t)
t = t or {}
if #t == 0 then return '' end
return table.concat {'   schedules = {', table.concat(format_names(t), ', '), '}\n' }
end

local function format_triggers (t)
if #t == 0 then return '' end
return table.concat {'   triggers = {', table.concat(format_devices(t), ', '), '}\n' }
end

local function format_actions (t)
if #t == 0 then return '' end
return table.concat {'   actions = {', table.concat(format_devices(t), ', '), '}\n' }
end

local function format_lua (lua)
if not lua then return '' end
return '   Lua code: \n' .. lua
end

local function scene_tostring (s)
return  ("\n[%d] '%s' \n%s%s%s%s "): format (s.id, s.name,
format_timers(s.timers), format_triggers(s.triggers), format_actions(s.actions), format_lua(s.lua) )
end

-- functions to build data structure of devices triggering scenes, and actioned by scenes
local function get_device (x) return x.device end

local function trigger_devices (t)
local trigger_set = set()  -- create a new empty set (of device numbers)
local device_list = map (t, get_device) -- create list of device numbers
map (device_list, trigger_set.add) -- add elements to set
return trigger_set.list () -- return sorted list of set members
end

local function action_devices (groups)
local action_set = set() -- create a new empty set (of device numbers)
local function actions (g) return map (g.actions, get_device) end
local action_list = map (groups, actions) -- create list of actions
map (flatten(action_list), action_set.add) -- mash together all the action device lists
return action_set.list () -- return sorted list of set members
end

local function attached_devices (sceneNo)
local _, s = luup.inet.wget("http://127.0.0.1:3480/data_request?id=scene&action=list&scene=" .. sceneNo)
if s == "ERROR" then return end

s = json.decode(s)
s.triggers = trigger_devices (s.triggers) -- restructure slightly
s.actions  = action_devices  (s.groups)

return setmetatable (s, { __tostring = scene_tostring} )
end

-- sceneBlog ()

local file = io.open(filename, "w")

if file then
luup.log ("Opening: " .. filename)
file:write '\nSCENES: \n\n '

for i in pairs (luup.scenes) do
local scene_devices = attached_devices (i)
file:write( tostring(scene_devices) )
end

file:close()
luup.log ("Closing: " .. filename)
else
luup.log ("Failed to open: " .. filename)
end
end

-- main: build and write the database

sceneBlog "/www/scene_devices.txt"

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #11 on: November 11, 2017, 02:35:49 pm »
OK, that's very surprising...

I cut and pasted the code you posted, changed the json module (because I haven't installed dataMine), and it works just fine.

The error message 'Code execution failed' means that the code never even got to run, so there must be something strange going on.  Perhaps a problem with hidden control characters (line endings?) or an editor changing things like ASCII quotes to apostrophes.

Can you just try:

Code: [Select]
json = require "json-dm"
print (pretty(json))

From my module, I get:

Code: [Select]
{
  decode = function: 0x1183ba0,
  default = {
    huge = "8.88e888",
    max_array_length = 1000
  },
  encode = function: 0x1184350,
  version = "2013.11.06 @akbooer"
}
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #12 on: November 11, 2017, 03:00:18 pm »
same result...

I did a restore to the vera before all this, tried to upload the dkjson file in your post but it's not sticking. I have ecobee and smart switch apps which both appears to use a json file. Do you know how I can try using those instead of datamine?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5234
  • Karma: +226/-67
  • "Less is more"
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #13 on: November 11, 2017, 03:24:22 pm »
Under AltUI > Misc > OS Command there is a 'Find Json' button, which should be able to find every file with that in the name (not the extension)
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline dannieboiz

  • Sr. Member
  • ****
  • Posts: 412
  • Karma: +2/-2
Re: Anyway to detect what tripped a device or see all the scene it belongs to?
« Reply #14 on: November 11, 2017, 03:38:35 pm »
Under AltUI > Misc > OS Command there is a 'Find Json' button, which should be able to find every file with that in the name (not the extension)

FWIW after I restore I tried installing DM2 hence the json-dm2 shows below which I also tried same error.

/etc/cmh-ludl/L_SmartSwitch_dkjson.lua
/etc/cmh-ludl/L_ecobee_dkjson.lua
/overlay/etc/cmh-ludl/L_SmartSwitch_dkjson.lua
/overlay/etc/cmh-ludl/L_ecobee_dkjson.lua
/overlay/usr/lib/lua/json-dm2.lua
/overlay/usr/lib/lua/ihjson.lua
/usr/lib/lua/json-dm2.lua
/usr/lib/lua/ihjson.lua