Traffic with Google Maps in Domoticz

Moderator: leecollings

bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

i have this message errow now in my logs any idea what is it ?
Error: EventSystem: in Traffic-SAM-Google-Maps: [string "---------------------------------..."]:59: attempt to call a nil value
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Traffic with Google Maps in Domoticz

Post by assenzuid »

woody4165 wrote:
assenzuid wrote:Great stuff. I will also try to implement this.
I have also a feature request.
Is it possible to get some traffic information say around 30Kms of my Home coordinates in a readable view?
My programming knowledge is not so good
I don't think that Google give this kind of information in a text way.
The only thing that is near to that is a google map view with colored street, but I don't think can be useful.
If you have a website that give this information and produce such an XML or a file like that maybe it can be checked.
Somithing like this, it's a RSS feed
http://www.verkeerplaza.nl/rssfeed
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

do i need to create a switch device with text or create a variable (setup/user variables) and select integer ?
i think it' s the last step i miss
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

bran2000 wrote:i have this message errow now in my logs any idea what is it ?
Error: EventSystem: in Traffic-SAM-Google-Maps: [string "---------------------------------..."]:59: attempt to call a nil value
the line 59 concerns the JSON.lua
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
do i need to modify the json.lua ?
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

No, I haven't modified the JSON.lua at all.
Maybe it's useful to see all the script. Are you referencing the JSON.lua in your right folder?
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

bran2000 wrote:do i need to create a switch device with text or create a variable (setup/user variables) and select integer ?
i think it' s the last step i miss
In the beginning I used a text device, but if you have the beta release of domoticz you can use a Custom device and you will get also the log/graph.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

it's in the right folder.
i found the script here :
http://domoticz.com/wiki/Lua_-_json.lua in the section : Lua script, is it this one i have to use ?

EDIT


i use the stable domoticz version so i don't have the custom device. If i user a standard switch, i atttached the capture screen.
Attachments
Capturedomo.PNG
Capturedomo.PNG (199.59 KiB) Viewed 3435 times
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

When I was saying to see all the script, I meant your script, not the JSON.lua... [WINKING FACE]
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

because i thought it was a problem with the lua.json (because it's the origin file, i did'nt touch this file)
so this is the script i use :

---------------------------------
-- Script to calculate duration and distance between two points using Google Maps
-- Autor : woody4165 based on Neutrino Traffic with Waze
-- Date : 9 April 2016
-- Need a text device (using duration) or an integer (using mins)
---------------------------------
commandArray={}

time = os.date("*t")
day = tonumber(os.date("%w"))

idx = '198'
-- If a weekday and time between 7AM and 9AM
if ((day > 0 and day < 6) and (time.hour > 6 and time.hour < 10)) then

-- Every 10 minutes
if((time.min % 10)==0) then

--import JSON.lua library
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

-- variables --
-- Coordinates starting point
fromx="xx.yyyyyyy"
fromy="xx.yyyyyyy"
-- Coordinates destination point
tox="xx.yyyyyyy"
toy="xx.yyyyyyy"
-- Google Api Key
key = 'MY API KEY'

-- get data from Google Maps and decode it in gmaps
local jsondata = assert(io.popen('curl "https://maps.googleapis.com/maps/api/di ... key='..key..'"'))
local jsondevices = jsondata:read('*all')
jsondata:close()
local gmaps = json:decode(jsondevices)

-- Read from the data table, and extract duration and distance in text
distance = gmaps.routes[1].legs[1].distance.text
duration = gmaps.routes[1].legs[1].duration_in_traffic.text

-- mins is only the number part of duration (for evaulation purpose or to return a number in a number device)
for minutes in string.gmatch(duration, "%d+") do mins = tonumber(minutes) end

-- send a Telegram message with status update (replace xxxxxxx with chatid and yyyyyyyyy with bot key)
-- In case you don't want to receive Telegram message, just remove or comment next two lines
message = mins..' mins to go to office at '..time.hour..':'..time.min..' !!!'
os.execute('curl --data chat_id=xxxxxxxxxx--data-urlencode "text='..message..'" "https://api.telegram.org/botyyyyyyyyyyy ... endMessage" ')


-- return a text to the device (eg. 12 mins)
commandArray[1]={['UpdateDevice'] =idx..'|0|' .. tostring(duration)}
end

end

return commandArray
Last edited by bran2000 on Sunday 17 April 2016 10:04, edited 1 time in total.
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

I've tested your script, just changed JSON.lua position, added my Api Key and made some print of "distance", "duration" and "mins:

Code: Select all

2016-04-15 07:39:01.327 LUA: 19.6 km
2016-04-15 07:39:01.327 LUA: 32 mins
2016-04-15 07:39:01.327 LUA: 32
2016-04-15 07:39:01.961 EventSystem: Script event triggered: test
This is the result.

You have to check somewhere else, the script works fine.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

thanks,
ok so the script is correct, you speak about changing the JSON.lua position, what do i need to change ?
is it possible to have the right JSON.lua version, can you give me yours to be sure ?
i checked if it was a problem with the api key, it's not i did this
https://maps.googleapis.com/maps/api/di ... y=MYAPIKEY
it seems working, this just the beginning script not the full :)
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJVQapm1N05kcRZHm8Bzhk3xc",
"types" : [ "route" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJpyRMylh55kcR71J7IWM7xUs",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 48.77894970000001,
"lng" : 2.3969549
},
"southwest" : {
"lat" : 48.7372995,
"lng" : 2.1974075
}
},
"copyrights" : "Données cartographiques ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "19,6 km",
"value" : 19569
},
"duration" : {
"text" : "24 minutes",
"value" : 1450
},

my other question : with a virtual sensor and the sensor type text (i use the stable version), it will work or not (i mean the duration will display) ?
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

don't remember where I get it from, but this afternoon/evening I will post it "mine".
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

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
Last edited by bran2000 on Friday 15 April 2016 9:55, edited 1 time in total.
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

absolutely nothing to touch in JSON.lua
you can use a Virtual Dummy device and as type Text. This is what I used before the Custom Device
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

ok thanks for all your support !
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

I've compared the JSON.lua in my RPi and the one in the Wiki and they are exactly the same.
If you want I can share, but nothing should change...
Did you make cut&paste in an editor?
Have you saved with Unix LF (LineFeed)?

Otherwise you can try to catch from author
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
bran2000
Posts: 60
Joined: Saturday 20 June 2015 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with Google Maps in Domoticz

Post by bran2000 »

i think you haven't seen my previous answer ! Everything is working fine now.
i have a little problem i received during one minute all the notifications by email, by prowl, by Pilot. it begins at 4:00 pm and finished at 04:01 PM.
Do you know i can only select one notification ?
Thanks
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

Ops, I missed that!

You can't with a Text device, you can with a Custom Device.
Image
As you can see, there is a Notification button on the Custom device where you can choose which notification type to use.

You should also save the Lua script in the Events page as "Time" as below:
Image to avoid multiple notification during a period of time.
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with Google Maps in Domoticz

Post by woody4165 »

One more thing I've found regarding the google map api.

The travel time is being calculated on a certain route and it may vary depending on the traffic.
So if you want to know on which route is being calculated the travel time, just get this value:

Code: Select all

    summary = gmaps.routes[1].summary
And you will know on which route has been calculated.
Now I'm using the Custom Device to get travel time, and to get also logs, and I resumed the Dummy Text device to show the message that I have already built to send it via Telegram, adding just the summary variable.

Code: Select all

        message=tostring(homework)..'min to go to office at '..("%02d:%02d"):format(time.hour, time.min)..' route '..tostring(summary)
and modified/added

Code: Select all

-- modified, added [1] after commandArray
commandArray[1]={['UpdateDevice'] =idxhomework..'|0|' .. tostring(homework)} 
-- addedd
commandArray[2]={['UpdateDevice'] =idxroute..'|0|' .. tostring(message)}  
Nice!
Cubietruck - Linux cubietruck 4.13.16 (Debian GNU/Linux 8 (jessie)) + Domoticz + RFLink, Xiaomi Gateway, Owl USB, Yeelight Color and B/W, ESP8266, Broadlink RM2, Netatmo Thermostat
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Traffic with Google Maps in Domoticz

Post by assenzuid »

I get below error.

2016-04-15 19:00:00.749 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_googletravel.lua: /home/pi/domoticz/scripts/lua/script_time_googletravel.lua:54: attempt to index field '?' (a nil value)
2016-04-15 19:00:01.531 Error: EventSystem: in script_time_googletravel: [string "--------------------------------- ..."]:54: attempt to index field '?' (a nil value)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests