Page 1 of 6
Traffic with waze in Domoticz
Posted: Saturday 29 August 2015 9:31
by bricololo
Hello, there is a new API for Waze traffic.
https://www.waze.com/row-RoutingManager/routingRequest?
I am looking to get travel time Home - Work with this request.
Actually, I can get an XML file via the request,
(for me, latitude and longitude are reversed)
https://www.waze.com/row-RoutingManager ... LOW_UTURNS
It is possible to have the answer in XML or JSON, modify returnXML=true by returnJSON=true
<summary time = "9264" must be divided by 60 for take the time.
I would like to create a script in shell or python to have the traffic.
This value will be displayed in Domoticz with a JSON request and just put this script in CRON.
There is sources
http://www.domo-blog.fr/info-trajet-waz ... raspberry/
https://plus.google.com/+NicolasFovet/posts/Ei14c24n3hP
https://github.com/Nimrod007/waze-api/b ... /route.php
If anyone can realize the scripts before me it's will be good.
Thanks
Re: Traffic with waze in Domoticz
Posted: Saturday 29 August 2015 10:52
by Egregius
Nice idea

I wouldn't put in into domoticz do.
Quickly created a page on my site, bookmark it on my phone
http://egregius.be/traffic.php
I'm wondering how much request you may sent to that service
Offcourse with something like this you could also easily drop in into a text virtual switch.
Code: Select all
<?php
header('Content-Type: text/html; charset=UTF-8');
function traffic($startlat, $startlon, $endlat, $endlon) {
GLOBAL $traffic;
$traffic=file_get_contents('https://www.waze.com/row-RoutingManager/routingRequest?from=x%3A'.$startlon.'+y%3A'.$startlat.'&to=x%3A'.$endlon.'+y%3A'.$endlat.'&at=0&returnJSON=true&timeout=60000&nPaths=1&clientVersion=4.0.0');
$traffic=json_decode($traffic,true);
$tijd=0;$lengte=0;
foreach($traffic['response']['results'] as $street) {
$tijd = $tijd + $street['crossTime'];
$lengte = $lengte + $street['length'];
}
$tijd = strftime("%k:%M:%S",$tijd);
$lengte = round($lengte / 1000,0);
return array('tijd' => $tijd, 'lengte' => $lengte);
}
$hw = traffic(50.892565,3.114135,50.877122,4.164326);
echo 'Home - Work = '.$hw['tijd'].' / '.$hw['lengte'].'km<br>';
$wh = traffic(50.877122,4.164326,50.892565,3.114135);
echo 'Work - Home = '.$wh['tijd'].' / '.$wh['lengte'].'km<br>';
Re: Traffic with waze in Domoticz
Posted: Saturday 29 August 2015 12:40
by remb0
Wake is super!! Maybe the output can be written to a virtual text switch.
Re: Traffic with waze in Domoticz
Posted: Saturday 29 August 2015 12:58
by Egregius
Offcourse. Just add something like this:
Code: Select all
<?php
$idx = 153;//IDX of the virtual text switch
file_get_contents('http://ip:port/json.htm?type=command¶m=udevice&idx='.$idx.'&nvalue=0&svalue='.$hw['tijd'])
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 11:04
by bricololo
It's interesting but I don't use a web server, so no php script.
That's why I'd like a shell script or python.
The hardest for me is to extract the time value of the answer to the request.
Thank for your answer
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 16:47
by Egregius
You can use php without webserver;)
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 17:55
by bricololo
Yes...
It's works
Thanks a lot
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 18:00
by Egregius
You're welcome
Add the script to crontab so it runs every 5 minutes or something.
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 20:28
by bricololo
I don't understand,
Travel times are increased by an hour.
For a normal time 18min, I get 1 hour 18 min.
For a normal time 1 hour 30 min, I get 2 hour 30 min.
Is possible to decrease an hour.
I tried
$timestamp = mktime($tijd);
$timestamp -= 3600;
$tijd = date($timestamp);
but it's not works
Thanks
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 20:45
by Egregius
Yes, I noticed that also...
Try
Code: Select all
$tijd = strftime("%k:%M:%S",$tijd-3600);
strftime uses UTC or DST or something. If it's going to be a problem after wintertime I'll search for another function.
Re: Traffic with waze in Domoticz
Posted: Monday 31 August 2015 21:31
by bricololo
Super,
Thanks, I'm not good in php.
Re: Traffic with waze in Domoticz
Posted: Thursday 03 September 2015 9:30
by remb0
maybe a nice for the wiki. a little tutor how to schedule, run configue and see it in domoticz?
Re: Traffic with waze in Domoticz
Posted: Monday 21 September 2015 21:26
by iero
Hello
I simplified the script in order to get only the time (every 10 minutes) for my trip home -> work
And I populate a "pressure" sensor to get the best timing to leave home (before 8h10 in my case)
Here is the code (quite the same) :
Code: Select all
---------------------------------
-- Script de calcul de temps pour un trajet entre 2 coordonnées
-- Auteur : Neutrino (updated iero)
-- Date : 7 septembre 2015 (updated 21/09/2015)
-- Nécessite un capteur virtuel de type Pression
-- source :
-- http://www.domo-blog.fr/info-trajet-waze-eedomus-version-raspberry/
---------------------------------
commandArray={}
time = os.date("*t")
if((time.min % 10)==0) then
--import des fontions pour lire le JSON
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
--variables à modifier----------------
--idx du capteur
idx = 'XX'
--coordonnées de départ
departx="X.XXX"
departy="X.XXX"
--coordonnées d'arrivée
arrivex="X.XXX"
arrivey="X.XXX"
--Récupération du trajet et de sa durée en temps réel via 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&timeout=6000&nPaths=1&options=AVOID_TRAILS%3At%2CALLOW_UTURNS"'))
local trajet = waze:read('*all')
waze:close()
local jsonTrajet = json:decode(trajet)
--Noms des principales routes empruntées
routeName = jsonTrajet['response']['routeName']
--Liste des routes empruntées
route = jsonTrajet['response']['results']
--calcul du temps de trajet (en secondes)
routeTotalTimeSec = 0
for response,results in pairs(route) do
routeTotalTimeSec = routeTotalTimeSec + results['crossTime']
end
--Temps de trajet en minutes
routeTotalTimeMin = routeTotalTimeSec/60-((routeTotalTimeSec%60)/60)
--print(routeTotalTimeMin)
commandArray[1]={['UpdateDevice'] =idx..'|0|' .. tostring(routeTotalTimeMin)}
end
return commandArray
Next step, I would like to receive an alert if waze see a police control on this trip..
Re: Traffic with waze in Domoticz
Posted: Monday 28 September 2015 11:50
by sploutchi
Hi iero !
I just tried your lua script but i haave this error :
2015-09-28 11:48:10.576 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_WAZEFLO.lua:35: attempt to index local 'jsonTrajet' (a nil value)
Any idea ?
My data are :
--coordonnées de départ
departx="48.632936"
departy="2.291525"
--coordonnées d'arrivée
arrivex="48.796156"
arrivey="2.299969"
Re: Traffic with waze in Domoticz
Posted: Monday 28 September 2015 17:16
by Timple
iero wrote:Hello
I simplified the script in order to get only the time (every 10 minutes) for my trip home -> work
And I populate a "pressure" sensor to get the best timing to leave home (before 8h10 in my case)
[...]
Next step, I would like to receive an alert if waze see a police control on this trip..
Very nice!
Re: Traffic with waze in Domoticz
Posted: Monday 28 September 2015 20:42
by remb0
there is an link to JSON.lua (for the calsulations) you miss that file!
download:
http://regex.info/code/JSON.lua
and place it in the right folder.
Re: Traffic with waze in Domoticz
Posted: Wednesday 21 October 2015 23:55
by Whatsek
I have the same error, I have the JSON.lua from you link, and put in /home/pi/domoticz/scripts/lua/JSON.lua
Do I have to edit something in that file? I couldn`t find anything about "trajet"
Re: Traffic with waze in Domoticz
Posted: Saturday 24 October 2015 10:22
by Derik
pffff I cannot handle all the great options ij Domoticz....[ like that ]
Is there perhaps a clean and working code example for Waze?
Or a Wiki?
Re: Traffic with waze in Domoticz
Posted: Sunday 25 October 2015 8:58
by salvation
Egregius wrote:Yes, I noticed that also...
Try
Code: Select all
$tijd = strftime("%k:%M:%S",$tijd-3600);
strftime uses UTC or DST or something. If it's going to be a problem after wintertime I'll search for another function.
Thanks for this script Egregius, works great.
I've also tried the LUA variant but it's not working. Get the same message as others. Seems the call to waze is not correct and returns an empty result. But I didn't know how to fix.
Re: Traffic with waze in Domoticz
Posted: Sunday 25 October 2015 9:09
by Egregius
Once you go PHP you'll never look back to lua
