Help with Waze ETA script

Moderator: leecollings

Post Reply
RobertH
Posts: 12
Joined: Sunday 15 November 2015 19:10
Target OS: Windows
Domoticz version:
Location: Norway, Kongsberg
Contact:

Help with Waze ETA script

Post by RobertH »

Hi,

I have tried to modifie the script under to work under Windows I changed the include path of the JSON parser file but I still gives me an error, the file is there and the name is exactly the same. Could this be the curl part and how would I go about changing it?

From log:
2016-03-09 08:43:00.070 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_eta.lua: ...ram Files (x86)\Domoticz\scripts\lua\script_time_eta.lua:50: attempt to index local 'jsonTrajet' (a nil value)


The code:

Code: Select all

--C:\Program Files (x86)\Domoticz\scripts\lua\script_time_eta.lua
------------- Begin time functions -------------
 
function timedifference_lastupdate_homework (s)
	if s == nil then 
		print('Error: Could not get lastupdate from device')
		difference = 1000 --value have to be larger than timeout. new virtual devices don't get corrent lastupdate value, skips the first check
		return difference
	end
	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)
	return difference
end
 
------------- End time functions -------------
 
 
------------- Begin local variables -------------
 
-- Debug variables:
 
timeout                = 300 
difference_homework    = timedifference_lastupdate_homework(otherdevices_lastupdate['ETA']) --change to the name of the virtual device
 
------------- End local variables -------------
 
------------- Begin code -------------
 
commandArray = {}
 
if (difference_homework > timeout)
then
 
function traveltime(departx,departy,arrivex,arrivey)
   --get route from WAZE--
   local waze=assert(io.popen('curl "https://www.waze.com/row-RoutingManager/routingRequest?from=x%3A'..departx..'+y%3A'..departy..'&to=x%3A'..arrivex..'+y%3A'..arrivey..'&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS%3At%2CALLOW_UTURNS"'))
   local trajet = waze:read('*all')
   waze:close()
   
   local jsonTrajet = json:decode(trajet)
 
   --Get major road from JSON
   routeName = jsonTrajet['response']['routeName']
   --Get route steps from JSON info
   route = jsonTrajet['response']['results']
 
   --Calculate the total time by adding the time of all sections
   routeTotalTimeSec = 0
   for response,results in pairs(route) do
      routeTotalTimeSec = routeTotalTimeSec + results['crossTime']
   end
 
   --translate the number of seconds to minutes
   routeTotalTimeMin = routeTotalTimeSec/60-((routeTotalTimeSec%60)/60)
 
   return routeTotalTimeMin
end
 
--import JSON addin (download from http://regex.info/code/JSON.lua)
json = (loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\JSON.lua")()
 
-- Update these variables ---------------
--idx of virtual device for capturing the travel minutes
idxhomework = '68'
 
departy="59.684534" --lat
departx="9.627667" --long
 
arrivey="59.675331" --lat
arrivex="9.641017" --long
 
-- get travel time
homework=traveltime(departx,departy,arrivex,arrivey)
 
 
 
 commandArray[1] = {['UpdateDevice'] = idxhomework .. '|0|' .. homework}
 
 
end
 
------------- End code -------------
 
return commandArray

Robert
Domoticz running on Windows Server 2008 in VMware (ESXi), UZB1 stick, 3x FIBARO FGD212 Dimmer, 1x FIBARO RGBW Controller FGRGBWM-441, 2x FIBARO FGMS001 Motion Sensor and 2x Popp 009006 Plug-in Switch
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Help with Waze ETA script

Post by simonrg »

If JSON.lua is in the same directory as the script, then you should be able to just use:

Code: Select all

json = (loadfile "JSON.lua")()
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Rydin
Posts: 4
Joined: Friday 05 February 2016 23:21
Target OS: Windows
Domoticz version:
Contact:

Re: Help with Waze ETA script

Post by Rydin »

I see that you use my version of the script. I did not try it in windows, but after some testing now i see that all you need is to install curl.
I downloaded curl from here http://winampplugins.co.uk/curl/ and copied it to c:\windows
simonrg wrote:If JSON.lua is in the same directory as the script, then you should be able to just use:

Code: Select all

json = (loadfile "JSON.lua")()
I tried to skip the path to JSON.lua on my raspberry, but for some reason it couldn't find the file.

Edit:
Same problem on windows. Can't find JSON.lua without the full path to the file.
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Help with Waze ETA script

Post by jvdz »

That is indeed the default behavior I have seen in the past. The search path for the LUA loadfile command doesn't include the calling script directory.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Help with Waze ETA script

Post by simonrg »

Rydin wrote:Same problem on windows. Can't find JSON.lua without the full path to the file.
Strange loadfile works without using package.path, so if Lua is running in the directory where the scripts are then JSON.lua should be found.

Whereas require uses package.path which could explicitly include the current path or anyother directory.

What does print(package.path) return?

Does require(JSON) find JSON.lua?

For Windows specifically then you may need to escape the brackets in the path name, so you could try moving JSON to a very simple directory - c:\test\JSON.lua.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Rydin
Posts: 4
Joined: Friday 05 February 2016 23:21
Target OS: Windows
Domoticz version:
Contact:

Re: Help with Waze ETA script

Post by Rydin »

print(package.path)
2016-03-09 13:15:00.148 LUA: C:\Program Files (x86)\Domoticz\lua\?.lua;C:\Program Files (x86)\Domoticz\lua\?\init.lua;C:\Program Files (x86)\Domoticz\?.lua;C:\Program Files (x86)\Domoticz\?\init.lua;.\?.lua

edit:
test = require("JSON")
print(test)

2016-03-09 13:31:00.242 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_waze.lua: ...am Files (x86)\Domoticz\scripts\lua\script_time_waze.lua:69: module 'JSON' not found:
no field package.preload['JSON']
no file 'C:\Program Files (x86)\Domoticz\lua\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\lua\JSON\init.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON\init.lua'
no file '.\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.dll'
no file 'C:\Program Files (x86)\Domoticz\loadall.dll'
no file '.\JSON.dll'
Last edited by Rydin on Wednesday 09 March 2016 13:32, edited 2 times in total.
RobertH
Posts: 12
Joined: Sunday 15 November 2015 19:10
Target OS: Windows
Domoticz version:
Location: Norway, Kongsberg
Contact:

Re: RE: Re: Help with Waze ETA script

Post by RobertH »

Rydin wrote:I see that you use my version of the script. I did not try it in windows, but after some testing now i see that all you need is to install curl.
I downloaded curl from here http://winampplugins.co.uk/curl/ and copied it to c:\windows
simonrg wrote:If JSON.lua is in the same directory as the script, then you should be able to just use:

Code: Select all

json = (loadfile "JSON.lua")()
I tried to skip the path to JSON.lua on my raspberry, but for some reason it couldn't find the file.

Edit:
Same problem on windows. Can't find JSON.lua without the full path to the file.
Thank you very much. That worked perfectly! Image Are you the one who made the ST1 script also? Curl solved my problem on that one also..

Robert

Sent from my SM-G925K using Tapatalk
Domoticz running on Windows Server 2008 in VMware (ESXi), UZB1 stick, 3x FIBARO FGD212 Dimmer, 1x FIBARO RGBW Controller FGRGBWM-441, 2x FIBARO FGMS001 Motion Sensor and 2x Popp 009006 Plug-in Switch
Rydin
Posts: 4
Joined: Friday 05 February 2016 23:21
Target OS: Windows
Domoticz version:
Contact:

Re: Help with Waze ETA script

Post by Rydin »

Yes, I made the ST1 script. Happy that it works for you :)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Help with Waze ETA script

Post by simonrg »

Rydin wrote:print(package.path)
2016-03-09 13:15:00.148 LUA: C:\Program Files (x86)\Domoticz\lua\?.lua;C:\Program Files (x86)\Domoticz\lua\?\init.lua;C:\Program Files (x86)\Domoticz\?.lua;C:\Program Files (x86)\Domoticz\?\init.lua;.\?.lua

test = require("JSON")
print(test)

2016-03-09 13:31:00.242 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_waze.lua: ...am Files (x86)\Domoticz\scripts\lua\script_time_waze.lua:69: module 'JSON' not found:
no field package.preload['JSON']
no file 'C:\Program Files (x86)\Domoticz\lua\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\lua\JSON\init.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON\init.lua'
no file '.\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.dll'
no file 'C:\Program Files (x86)\Domoticz\loadall.dll'
no file '.\JSON.dll'
Not sure you haven't solved this already.
Just intersting that if you look at package.path it does not include Domoticz\scripts\lua, but Domoticz\lua - so placing JSON.lua in Domoticz\lua would allow require to find it.
Equally json = (loadfile "JSON.lua")() may well work then if Lua is really running is Domoticz\lua.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest