Author Topic: [Solved]Extracting data from gc_jsonEvents  (Read 4075 times)

Offline tculpepp

  • Newbie
  • *
  • Posts: 13
  • Karma: +2/-0
Re: Extracting data from gc_jsonEvents
« Reply #15 on: August 31, 2018, 12:29:01 pm »
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:

Code: [Select]
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

Online rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1028
  • Karma: +166/-1
Re: Extracting data from gc_jsonEvents
« Reply #16 on: August 31, 2018, 12:58:16 pm »
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.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.

Offline jswim788

  • Hero Member
  • *****
  • Posts: 805
  • Karma: +58/-2
Re: Extracting data from gc_jsonEvents
« Reply #17 on: August 31, 2018, 01:38:03 pm »
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: [Select]
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

Offline tculpepp

  • Newbie
  • *
  • Posts: 13
  • Karma: +2/-0
Re: Extracting data from gc_jsonEvents
« Reply #18 on: August 31, 2018, 02:23:26 pm »
Quote
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.

Online rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1028
  • Karma: +166/-1
Re: Extracting data from gc_jsonEvents
« Reply #19 on: August 31, 2018, 03:20:03 pm »
Quote
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.

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.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3, Lite. Hassio, Slapdash.