Traffic with waze in Domoticz

bricololo
Posts: 26
Joined: Tuesday 18 February 2014 12:51
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Traffic with waze in Domoticz

Post by bricololo » Saturday 29 August 2015 9:31

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

User avatar
Egregius
Posts: 2415
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Traffic with waze in Domoticz

Post by Egregius » Saturday 29 August 2015 10:52

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 :roll:

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>';

User avatar
remb0
Posts: 809
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by remb0 » Saturday 29 August 2015 12:40

Wake is super!! Maybe the output can be written to a virtual text switch.

User avatar
Egregius
Posts: 2415
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Traffic with waze in Domoticz

Post by Egregius » Saturday 29 August 2015 12:58

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&param=udevice&idx='.$idx.'&nvalue=0&svalue='.$hw['tijd'])

bricololo
Posts: 26
Joined: Tuesday 18 February 2014 12:51
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by bricololo » Monday 31 August 2015 11:04

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

User avatar
Egregius
Posts: 2415
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Traffic with waze in Domoticz

Post by Egregius » Monday 31 August 2015 16:47

You can use php without webserver;)

bricololo
Posts: 26
Joined: Tuesday 18 February 2014 12:51
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by bricololo » Monday 31 August 2015 17:55

Yes...
It's works :P
Thanks a lot

User avatar
Egregius
Posts: 2415
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Traffic with waze in Domoticz

Post by Egregius » Monday 31 August 2015 18:00

You're welcome ;)

Add the script to crontab so it runs every 5 minutes or something.

bricololo
Posts: 26
Joined: Tuesday 18 February 2014 12:51
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by bricololo » Monday 31 August 2015 20:28

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

User avatar
Egregius
Posts: 2415
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Traffic with waze in Domoticz

Post by Egregius » Monday 31 August 2015 20:45

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.

bricololo
Posts: 26
Joined: Tuesday 18 February 2014 12:51
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by bricololo » Monday 31 August 2015 21:31

Super,
Thanks, I'm not good in php.

User avatar
remb0
Posts: 809
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by remb0 » Thursday 03 September 2015 9:30

maybe a nice for the wiki. a little tutor how to schedule, run configue and see it in domoticz?

iero
Posts: 7
Joined: Tuesday 01 September 2015 13:38
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by iero » Monday 21 September 2015 21:26

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)

Image

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..

sploutchi
Posts: 15
Joined: Friday 11 July 2014 10:46
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sploutchi » Monday 28 September 2015 11:50

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"

Timple
Posts: 27
Joined: Tuesday 20 January 2015 9:05
Target OS: Linux
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by Timple » Monday 28 September 2015 17:16

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!

User avatar
remb0
Posts: 809
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by remb0 » Monday 28 September 2015 20:42

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.

User avatar
Whatsek
Posts: 76
Joined: Saturday 31 May 2014 23:48
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: Hilversum, The Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by Whatsek » Wednesday 21 October 2015 23:55

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"

User avatar
Derik
Posts: 1827
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Traffic with waze in Domoticz

Post by Derik » Saturday 24 October 2015 10:22

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?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

salvation
Posts: 45
Joined: Saturday 06 September 2014 15:34
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Traffic with waze in Domoticz

Post by salvation » Sunday 25 October 2015 9:58

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.

User avatar
Egregius
Posts: 2415
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Traffic with waze in Domoticz

Post by Egregius » Sunday 25 October 2015 10:09

Once you go PHP you'll never look back to lua :mrgreen:

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests