Page 6 of 7
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 13:30
by sjefk
I adjusted the script just like yours and this is the output..
2016-05-04 13:30:00.418 LUA: {
"geocoded_waypoints" : [ {}, {} ],
"routes" : [],
"status" : "ZERO_RESULTS"
}
2016-05-04 13:30:00.418 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_google.lua: /home/pi/domoticz/scripts/lua/script_time_google.lua:56: attempt to index field '?' (a nil value)
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 14:02
by woody4165
what if you write the exact same url into a browser?
Try to curl the url you use in the browser instead the one mixed up with variables
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 14:19
by sjefk
Curl gives the exact same result like in a normal browser.
pi@raspberrypi ~/domoticz/scripts/lua $ curl "
https://maps.googleapis.com/maps/api/di ... le-API-Key"
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "EjhWYW4gTGVldXdlbmhvZWtsYWFuIDUzLTU1LCA0OTA0IEtQIE9vc3RlcmhvdXQsIE5lZGVybGFuZA",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ-zDCwLhvxkcREbu0NLbbhRI",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 52.1154437,
"lng" : 5.1058217
},
"southwest" : {
"lat" : 51.6188531,
"lng" : 4.8544003
}
},
"copyrights" : "Map data ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "65.0 km",
"value" : 65016
},
"duration" : {
"text" : "43 mins",
"value" : 2579
},
"end_address" : "Atoomweg 60, 3542 AB Utrecht, Netherlands",
"end_location" : {
"lat" : 52.1106402,
"lng" : 5.062270199999999
},
"start_address" : "Van Leeuwenhoeklaan 53-55, 4904 KP Oosterhout, Netherlands",
"start_location" : {
"lat" : 51.6200264,
"lng" : 4.8544003
},
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 15:10
by woody4165
I changed my lat/lon with yours and base on the miniscript published before, this is the result in the log:
Code: Select all
LUA: distanza 65.0 km distanzan 65.016 duration 43 mins
LUA: {
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "EjhWYW4gTGVldXdlbmhvZWtsYWFuIDUzLTU1LCA0OTA0IEtQIE9vc3RlcmhvdXQsIE5lZGVybGFuZA",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ-zDCwLhvxkcREbu0NLbbhRI",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 52.1154437,
"lng" : 5.1058217
},
"southwest" : {
"lat" : 51.6188531,
"lng" : 4.8544003
}
},
"copyrights" : "Map data ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "65.0 km",
"value" : 65016
},
"duration" : {
"text" : "43 mins",
"value" : 2579
},
"duration_in_traffic" : {
"text" : "43 mins",
"value" : 2581
},
"end_address" : "Atoomweg 60, 3542 AB Utrecht, Netherlands",
"end_location" : {
"lat" : 52.1106402,
"lng" : 5.062270199999999
},
"start_address" : "Van Leeuwenhoeklaan 53-55, 4904 KP Oosterhout, Netherlands",
"start_location" : {
"lat" : 51.6200264,
"lng" : 4.8544003
},
"steps" : [
{
"distance" : {
"text" : "0.2 km",
"value" : 213
},
"duration" : {
"text" : "1 min",
"value" : 33
},
"end_location" : {
"lat" : 51.6188929,
"lng" : 4.856876199999999
},
In the first row you can see the three result obtained from
Code: Select all
distancet = gmaps.routes[1].legs[1].distance.text
distance = gmaps.routes[1].legs[1].distance.value/1000
duration = gmaps.routes[1].legs[1].duration_in_traffic.text
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 16:28
by sjefk
Now i am really lost. I will start from scratch again later en will let you know..
Thanks for your help so far !
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 16:47
by woody4165
try to share all the script (without personal information)...
Maybe it can be helpful
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 17:01
by sjefk
Oke here is the code i copied
Code: Select all
---------------------------------
--Script to calculate duration and distance between two points using Google Maps
--Author : woody4165 based on Neutrino Traffic with Waze, updated by G3rard
--Date : 9 April 2016
---------------------------------
commandArray={}
time = os.date("*t")
day = tonumber(os.date("%w"))
--idx of devices for capturing the travel minutes in both direction
idxtraffic='668'
idxroute='669'
--usual traveltime (mins)
usualtimehomework = 45
usualtimeworkhome = 45
-- Coordinates starting point
fromx="4.85461"
fromy="51.62021"
-- Coordinates destination point
tox="5.06212"
toy="52.11058"
--Google Api Key
key='xxxxxxxxxxxxxxxxxxxxx'
--determine workday (Mo-Fri)
if (day > 0 and day < 6) then
week=true; weekend=false
else
week=false; weekend=true
end
--determine time (6:00-9:00, 15:00-18:00)
if ((time.hour > 5 and time.hour < 14) or (time.hour == 9 and time.min < 1)) then
mattina=true; sera=false
elseif ((time.hour > 14 and time.hour < 18) or (time.hour == 19 and time.min < 1)) then
mattina=false; sera=true
else
mattina=false; sera=false
end
--calculate traveltime
function traveltime(fromx,fromy,tox,toy)
--import JSON.lua library
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
-- get data from Google Maps and decode it in gmaps
local jsondata = assert(io.popen('curl "https://maps.googleapis.com/maps/api/directions/json?origin='..fromx..','..fromy..'&destination='..tox..','..toy..'&departure_time=now&key='..key..'"'))
local jsondevices = jsondata:read('*all')
jsondata:close()
local gmaps = json:decode(jsondevices)
print(jsondevices)
-- Read from the data table, and extract duration and distance in value. Divide distance by 1000 and duration_in_traffic by 60
distancet = gmaps.routes[1].legs[1].distance.text
distance = gmaps.routes[1].legs[1].distance.value/1000
duration = gmaps.routes[1].legs[1].duration_in_traffic.value/60
summary = gmaps.routes[1].summary
print("Distance "..distance.." distancet "..distancet.." duration "..duration)
-- mins round the duration
mins=math.ceil(duration)
return mins
end
--weekday (Mo-Fri) and time between 7:00-9:00
if (week and mattina) then
--every 10 minutes
if((time.min % 10)==0) then
traffic=traveltime(fromx,fromy,tox,toy)
message=tostring(traffic)..'min per andare in ufficio alle '..("%02d:%02d"):format(time.hour, time.min)..' percorso '..tostring(summary)
print(message)
--send message if traveltime is longer than usual
if(traffic > usualtimehomework) then
os.execute('curl --data chat_id=xxxxxxxx --data-urlencode "text='..message..'" "https://api.telegram.org/botxxxxxxxxxxxxx/sendMessage" ')
-- commandArray['SendNotification']=message
end
--return a text to the device (eg. 12 mins)
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(traffic)}
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(message)}
end
--weekday (Mo-Thu) and time between 16:00-19:00
elseif (week and sera) then
--every 10 minutes
if((time.min % 10)==0) then
traffic=traveltime(tox,toy,fromx,fromy)
message=tostring(traffic)..'min per andare a casa alle '..("%02d:%02d"):format(time.hour, time.min)..' percorso '..tostring(summary)
print(message)
--send message if traveltime is longer than usual
-- if(traffic > usualtimeworkhome) then
-- os.execute('curl --data chat_id=xxxxxx --data-urlencode "text='..message..'" "https://api.telegram.org/botxxxxxxxxx/sendMessage" ')
-- commandArray['SendNotification']=message
-- end
--return a text to the device (eg. 12 mins)
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(traffic)}
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(message)}
end
else
-- set device to 0 to prevent device get the last value after working hour
zero=0
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(zero)}
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(zero)}
end
return commandArray
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 17:24
by sjefk
I think i found something.. When i replace the variables ..fromx..','..fromy.. and ..tox..','..toy.. it works....
Re: Traffic with Google Maps in Domoticz
Posted: Wednesday 04 May 2016 17:33
by woody4165
Well done!
Re: Traffic with Google Maps in Domoticz
Posted: Friday 13 May 2016 14:16
by markk
bran2000 wrote:ok thanks i copied a new JSON.lua and now it seems to work, my switch is working.
the home-work switch is working it indicates a number. Sure it will be great with a graph, to bad i have to wait a new stable version.
i'll wait the afternoon to check the work-home is working
@Bran2000 - I'm stuck at the point you were just before this. Can you let me know the new JSON.lua that you copied to make it work for you please?
Re: Traffic with Google Maps in Domoticz
Posted: Friday 13 May 2016 15:00
by bran2000
hi,
i don't remember which one i used but it's definitely one of those
just download the JSON.lua file :
http://regex.info/blog/lua/json
if it's not working, try this one with the wiki :
https://www.domoticz.com/wiki/Lua_-_json.lua
Re: Traffic with Google Maps in Domoticz
Posted: Saturday 14 May 2016 12:09
by markk
Ok, thanks for that. I can see the script is running in the log. Suppose I now need to wait until Monday morning to see if it works. Going to be a long weekend
Re: Traffic with Google Maps in Domoticz
Posted: Saturday 14 May 2016 17:16
by woody4165
Since here is determined if it's week or weekend
Code: Select all
--determine workday (Mo-Fri)
if (day > 0 and day < 6) then
week=true; weekend=false
else
week=false; weekend=true
end
You can change here from week to weekend (and then revert it back) to check it right now or tomorrow
Just look for
and
Re: Traffic with Google Maps in Domoticz
Posted: Saturday 14 May 2016 18:09
by markk
woody4165 wrote:Since here is determined if it's week or weekend
Code: Select all
--determine workday (Mo-Fri)
if (day > 0 and day < 6) then
week=true; weekend=false
else
week=false; weekend=true
end
You can change here from week to weekend (and then revert it back) to check it right now or tomorrow
Just look for
and
I did this and it's all working. Thank you very much.
Re: Traffic with Google Maps in Domoticz
Posted: Friday 20 May 2016 12:51
by cattoo
Ill have a issue, the lua script dont run at all. Ive putted it in the folder with the other lua that ill have. And those are working, why does not the new one?
Re: Traffic with Google Maps in Domoticz
Posted: Friday 20 May 2016 12:57
by woody4165
I have the script in the Events inside the Domoticz web interface (Setup->More Options->Events).
I never tried from lua folder.
In any case, have you checked file permissions?
Re: Traffic with Google Maps in Domoticz
Posted: Friday 20 May 2016 15:33
by cattoo
Did not know that you could put lua in there now. But it aint show anything in the log file even if ill put it in events.
Re: Traffic with Google Maps in Domoticz
Posted: Friday 20 May 2016 17:09
by woody4165
If the script runs inside domoticz, should say the event has been triggered every minute, if you chose as event type Time.
If it's outside, I think there should be something to modify, probably.
In any case, as you can see inside the script it will update the devices only during some hours...
Re: Traffic with Google Maps in Domoticz
Posted: Tuesday 07 June 2016 7:36
by sjefk
Perhaps an idea is to integrate presence detection in the following way ?
If athome then stop sending the notifications workhome
if atwork then stop sending the notifications homework.
Re: Traffic with Google Maps in Domoticz
Posted: Monday 03 October 2016 17:30
by leoncornelissen
Thanks for this script! I implemted it twice while I want to measure several locations over time.
I run the script every 5 minutes and when I look in the log I see the variables overwritten each 5 minutes with the same value.
It does not harm but if I want to see the status of 12h ago I have to dig through >10 pages of logging.
I want to improve the writing of the logging by first doing a check what the previous value was and if there is a difference than write out the new values. Normally I read and write values and status via commandArray['DeviceName']
But the following part of code is new to me:
Code: Select all
commandArray[1]={['UpdateDevice'] =idxtraffic..'|0|' .. tostring(traffic)}
I understand that this is an easy way of writing values but is there also a construction for reading the current value based on the idx??