[Solved]Extracting data from gc_jsonEvents

I’m having trouble decoding the json.
I have tried to use the example from the User guide but i get an error when running the luup code.

Anyone else got this working?

I think it stops at the line : local json = require(“json”)

Regards Jesper

[quote=“JesperJ, post:1, topic:183146”]I’m having trouble decoding the json.
I have tried to use the example from the User guide but i get an error when running the luup code.

Anyone else got this working?

I think it stops at the line : local json = require(“json”)

Regards Jesper[/quote]

Try this as a test and check the log file:

Apps–>Develop Apps → Test Luup Code (Lua)

local jsonevents = "[{'eventEnd':1411581600,'eventStart':1411578000,'eventParameter':'','eventName':'TEST 1'},{'eventEnd':1411585200,'eventStart':1411581600,'eventParameter':'','eventName':'TEST 2'}]" local json = require("json") -- loads the package local events = json.decode(jsonevents) package.loaded.json = nil -- unloads the package for i = 1, #events do luup.log(events[i].eventName) end

The log file entry should look like this:

08 09/24/14 11:05:09.988 JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunLua <0x2d73b680>
08 09/24/14 11:05:09.989 JobHandler_LuaUPnP::HandleActionRequest argument Code=local jsonevents = “[{‘eventEnd’:1411581600,‘eventStart’:1411578000,‘eventParameter’:‘’,‘eventName’:‘TEST 1’},{‘eventEnd’:1411585200,‘eventStart’:1411581600,‘eventParameter’:‘’,‘eventName’:‘TEST 2’}]”
local json = require(“json”)
local events = json.decode(jsonevents)
package.loaded.json = nil – unloads the package
for i = 1, #events do
luup.log(events[i].eventName)
end <0x2d73b680>
50 09/24/14 11:05:10.007 luup_log:0: TEST 1 <0x2d73b680>
50 09/24/14 11:05:10.007 luup_log:0: TEST 2

That worked great!

But when i run your test code i get this in my logfile:

01 09/24/14 20:22:45.621 LuaInterface::LoadCode: [string “local GCAL_SID = “urn:srs-com:serviceId:GCal3”…”]:28: ‘’ expected near ‘end’ <0x2fefa680>
01 09/24/14 20:22:45.623 JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = “urn:srs-com:serviceId:GCal3”
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, “gc_jsonEvents”,myGooglePlugin)
if (jsonEvents == “[]”) then
return
end

– load the jason package
package.loaded.json = nil
local json = require(“json”)
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
–startEvent = os.date(“%Y-%m-%dT%H:%M:%S”, startevent)
startDate = os.date(“%Y-%m-%d”, startevent)
startTime = os.date(“%H:%M:%S”, startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date(“%H:%M:%S”, endevent)
eventname = eventList[ i ].eventName
event = "On " … startDate … " event " … eventname … " will start at " … startTime … " and end at " …
endTime
luup.log(event)
end
– unload json module
package.loaded.json = nil
end <0x2fefa680>

[quote=“JesperJ, post:3, topic:183146”]That worked great!

But when i run your test code i get this in my logfile:

01 09/24/14 20:22:45.621 LuaInterface::LoadCode: [string “local GCAL_SID = “urn:srs-com:serviceId:GCal3”…”]:28: ‘’ expected near ‘end’ <0x2fefa680>
01 09/24/14 20:22:45.623 JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = “urn:srs-com:serviceId:GCal3”
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, “gc_jsonEvents”,myGooglePlugin)
if (jsonEvents == “[]”) then
return
end

– load the jason package
package.loaded.json = nil
local json = require(“json”)
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
–startEvent = os.date(“%Y-%m-%dT%H:%M:%S”, startevent)
startDate = os.date(“%Y-%m-%d”, startevent)
startTime = os.date(“%H:%M:%S”, startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date(“%H:%M:%S”, endevent)
eventname = eventList[ i ].eventName
event = "On " … startDate … " event " … eventname … " will start at " … startTime … " and end at " …
endTime
luup.log(event)
end
– unload json module
package.loaded.json = nil
end <0x2fefa680>[/quote]

Yep ! The test code was presented as a function. Your code is ‘inline’ and therefore there are one too many ‘end’ statements (i.e. you omitted the 'function testjson() ’ statement from the start of the example. Remove the last ‘end’ statement and it should work as expected.

I’m really appreciating your help.
Tried it but got a new error :

01 09/24/14 22:51:17.471 LuaInterface::LoadCode: [string “local GCAL_SID = “urn:srs-com:serviceId:GCal3”…”]:26: ‘=’ expected near ‘json’ <0x2faa2680>
01 09/24/14 22:51:17.473 JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = “urn:srs-com:serviceId:GCal3”
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, “gc_jsonEvents”,myGooglePlugin)
if (jsonEvents == “[]”) then
return
end

– load the jason package
package.loaded.json = nil
local json = require(“json”)
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
–startEvent = os.date(“%Y-%m-%dT%H:%M:%S”, startevent)
startDate = os.date(“%Y-%m-%d”, startevent)
startTime = os.date(“%H:%M:%S”, startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date(“%H:%M:%S”, endevent)
eventname = eventList[ i ].eventName
event = "On " … startDate … " event " … eventname … " will start at " … startTime … " and end at " …
endTime
luup.log(event)
end
unload json module
package.loaded.json = nil

[quote=“JesperJ, post:5, topic:183146”]I’m really appreciating your help.
Tried it but got a new error :

01 09/24/14 22:51:17.471 LuaInterface::LoadCode: [string “local GCAL_SID = “urn:srs-com:serviceId:GCal3”…”]:26: ‘=’ expected near ‘json’ <0x2faa2680>
01 09/24/14 22:51:17.473 JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = “urn:srs-com:serviceId:GCal3”
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, “gc_jsonEvents”,myGooglePlugin)
if (jsonEvents == “[]”) then
return
end

– load the jason package
package.loaded.json = nil
local json = require(“json”)
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
–startEvent = os.date(“%Y-%m-%dT%H:%M:%S”, startevent)
startDate = os.date(“%Y-%m-%d”, startevent)
startTime = os.date(“%H:%M:%S”, startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date(“%H:%M:%S”, endevent)
eventname = eventList[ i ].eventName
event = "On " … startDate … " event " … eventname … " will start at " … startTime … " and end at " …
endTime
luup.log(event)
end
unload json module
package.loaded.json = nil[/quote]

This bit
… = “urn:srs-com:serviceId:GCal3”…"]:26: …
tells you approx where the error occurred i.e. line 26

there is no command ‘unload json module’ you have changed it from a comment to something Lua cannot interpret
it needs to be commented back out ’ – unload json module’

Thanks!

Did that and got a new error. :frowning:

01 09/25/14 12:24:37.306 GetLuaInterface can’t find device type: 0/0x10bd690 str: (null) <0x30418680>
01 09/25/14 12:24:37.306 luup_variable_get interface 0x10c1d88 args 3 <0x30418680>
01 09/25/14 12:24:37.335 LuaInterface::StartEngine failed run: 0 /usr/lib/lua/json.lua:362: attempt to get length of upvalue ‘js_string’ (a nil value) <0x30418680>
01 09/25/14 12:24:37.336 JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = “urn:srs-com:serviceId:GCal:3”
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, “gc_jsonEvents”,lul_device)
if (jsonEvents == “[]”) then
return
end

– load the jason package
package.loaded.json = nil
local json = require(“json”)
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
–startEvent = os.date(“%Y-%m-%dT%H:%M:%S”, startevent)
startDate = os.date(“%Y-%m-%d”, startevent)
startTime = os.date(“%H:%M:%S”, startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date(“%H:%M:%S”, endevent)
eventname = eventList[ i ].eventName
event = "On " … startDate … " event " … eventname … " will start at " … startTime … " and end at " …
endTime
luup.log(event)
end
–unload json module
package.loaded.json = nil

The test code in the documentation had the wrong GCAL_SID (from prior versions) and a variable name error
Use this code for testing - remember to replace myGooglePlugin with your device number

function testJson()
local GCAL_SID = "urn:srs-com:serviceId:GCalIII"
 -- change myGooglePlugin to be your GCal3 plugin device #
local myGooglePlugin = 8
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if ((jsonEvents == "[]") or (jsonEvents == nil)) then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, endTime, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
-- unload json module
package.loaded.json = nil
end

local test = testJson()

[quote=“JesperJ, post:7, topic:183146”]Thanks!

Did that and got a new error. :([/quote]

My bad - see post above. The result of the call that should have been
localjsonEvents=luup.variable_get(GCAL_SID,“gc_jsonEvents”,myGooglePlugin) was a nil for two reasons
(1) GCAL_SID was wrong and the third argument was ‘lul_device’ instead of ‘myGooglePugin’ (I’d copied it from my code not changed it)

That’s why the error message was complaining about the length of the json string …

Thanks Stuart for all your help!
I got it working now, great plugin!

Best regards
Jesper

Let’s say i got 2 different events on the same day and want to use the start time from the second event.
Can this be done?
I understand that i should look for the eventname of the second event but how do i read out the time from the table?

/Jesper

[quote=“JesperJ, post:11, topic:183146”]Let’s say i got 2 different events on the same day and want to use the start time from the second event.
Can this be done?
I understand that i should look for the eventname of the second event but how do i read out the time from the table?

/Jesper[/quote]

Something like this - just regular Lua :

function getEventStartEnd(eventToMatch)
local GCAL_SID = "urn:srs-com:serviceId:GCalIII"
 -- change myGooglePlugin to be your GCal3 plugin device #
local myGooglePlugin = 8
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if ((jsonEvents == "[]") or (jsonEvents == nil)) then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")

local eventList =json.decode(jsonEvents)

local startevent, endevent
for i,v in pairs(eventList) do
  if v.eventName == eventToMatch then
    startevent = eventList[ i ].eventStart
    endevent = eventList[ i ].eventEnd
    break
  end
end

-- unload json module
package.loaded.json = nil

-- do any time conversions e.g. to string that you want here

return startevent, endevent
end



local eventName = "NAME OF EVENT"
local eventStartTime, eventEndTime = getEventStartEnd(eventName)
print ((eventStartTime or "no match") .. " - " .. (eventEndTime or ""))

Ok thanks, will try it soon.

/Jesper

Does this

http://forum.micasaverde.com/index.php/topic,26692.msg229451.html#msg229451

look interesting / usable for you guys ?

[quote=“Stuart, post:14, topic:183146”]Does this

http://forum.micasaverde.com/index.php/topic,26692.msg229451.html#msg229451

look interesting / usable for you guys ?[/quote]

I don’t think I have the RAM for another Plug-in :slight_smile:

Here is what I do now in case it helps anyone else:

I have one google calendar where we put overlapping events for things like vacation, guests, running the spa, opening the blinds, etc and i have a scene that runs every 3 minutes to check for overlapping events (here is the start of the code in that scene):

[code]local active_events = luup.variable_get(“urn:srs-com:serviceId:GCalIII”,“gc_ActiveEvents”, d_gcalSensor) or “”

local i, j = string.find(active_events, “VACATION”)
if i == nil then
luup.call_action(“urn:dcineco-com:serviceId:MSwitch1”,“SetStatus6”,{newStatus6=0},d_mode) – turn off vacation multiswitch button if no active Vacation event
else – found event
luup.call_action(“urn:dcineco-com:serviceId:MSwitch1”,“SetStatus6”,{newStatus6=1},d_mode) – turn on vacation multiswitch button if active Vacation event
end – found event

local i, j = string.find(active_events, “GUEST”)
if i == nil then
luup.call_action(“urn:dcineco-com:serviceId:MSwitch1”,“SetStatus5”,{newStatus5=0},d_mode) – turn off guest multiswitch button if no active Guest event
else – found event
luup.call_action(“urn:dcineco-com:serviceId:MSwitch1”,“SetStatus5”,{newStatus5=1},d_mode) – turn on guest multiswitch button if active Guest event
end – found event[/code]

This works ok but it is not very efficient :slight_smile:

I’m trying to extract the event start and stop time from gc_jsonEvents, but not having much luck even getting the basic data. The code I’m using doesn’t give any errors but I’m not getting anything in my logs (other than the function loading). I’m using the example code from v2.3 Advanced Guide but luck. I have an event in gc_jsonEvents when I look at the variable page. Code below if anyone has any advice on this:

 function testJson()
local GCAL_SID = "urn:srs-com:serviceId:GCalIII"
 -- change myGooglePlugin to be your GCal3 plugin device #
local myGooglePlugin = 351
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if ((jsonEvents == "[]") or (jsonEvents == nil)) then
return
end
 
 local json = require "json"
 local eventList =json.decode(jsonEvents)
 package.loaded.json = nil 
 local numberEvents = #eventList
 local startevent, startDate, startTime, endevent, endTime, eventname, event
 for i = 1,numberEvents do
  startevent = eventList[i].eventStart
  startDate = os.date("%Y-%m-%d", startevent)
  startTime = os.date("%H:%M:%S", startevent)
  endevent = eventList[i].eventEnd
  endTime = os.date("%H:%M:%S", endevent)
  eventname = eventList[i].eventName
  event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..endTime
print(event)
 end
 return
end

I’m assuming you’re calling your function and you just didn’t show that code? The function won’t run automatically when defined. It has to be called.

The print() statement is probably not putting anything anywhere you can easily see. Try using luup.log() instead. Also remember that the log buffers, so whatever you write may not be visible until the buffer fills and is flushed.

Here’s a sample of what I use in my startup Lua and call from scene code. It looks similar to what you posted and works well for me.

[code]function checkPineLakeGCal (calID)
local GCAL_SID = “urn:srs-com:serviceId:GCalIII”
local list = luup.variable_get(GCAL_SID,“gc_jsonActiveEvents”,calID)

if (list == “[]”) then – no events
return false
end
local json = require(‘json’)
local eventList = json.decode(list)
local numberEvents = #eventList
for i = 1,numberEvents do
if eventList[i].eventName == “PINE LAKE” or eventList[i].eventName == “PINELAKE” then
luup.log("found PINE LAKE on Google calendar with ID: " … calID)
return true
end
end

– otherwise return false
return false
end[/code]

I'm assuming you're calling your function and you just didn't show that code?

I’m just going to go flog myself in the corner for a while and pretend you didn’t just have to ask “is it plugged in”. booboo fixed.

[quote=“tculpepp, post:19, topic:183146”]

I’m assuming you’re calling your function and you just didn’t show that code?

I’m just going to go flog myself in the corner for a while and pretend you didn’t just have to ask “is it plugged in”. booboo fixed.[/quote]

That’s funny! On the plus side, if you ask for extra eyes on something, you get them around here. I love that about this community.