Traffic with waze in Domoticz

Moderator: leecollings

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

Re: Traffic with waze in Domoticz

Post by salvation »

Great tip, I will try later today.

Changed this part

Code: Select all

$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');
to

Code: Select all

   $curl = curl_init();
   curl_setopt_array($curl, array(
       CURLOPT_RETURNTRANSFER => 1,
       CURLOPT_URL => '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',
       CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'
   ));
   $traffic = curl_exec($curl);
   curl_close($curl);
and that did the trick indeed.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Traffic with waze in Domoticz

Post by D'rMorris »

I also implemented this functionality, but then with a LUA script. I like to keep scripts in one language preferably and since everything else is also LUA (with the exception of some python scripts to control my LCD screens), I went for the LUA solution. Next to that, I like that LUA is controlled by Domoticz, whereas bash or python run more standalone.

I chose to have a LUA time script (so it runs every minute), but in the script I have a lastupdate timeout of 10 minutes. I still need to add additional time variables, because I only want to run the script between 6AM and 10AM and 3PM and 7PM.

Code: Select all

print('<b style="color:Blue">>   Monitoring Traveltimes</b>')

------------- Begin time functions -------------

function timedifference_lastupdate_homework (s)
  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_lastupdate_homework = os.difftime (t1, t2)
  return difference_lastupdate_homework
end

function timedifference_lastupdate_workhome (s)
  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_lastupdate_workhome = os.difftime (t1, t2)
  return difference_lastupdate_workhome
end

------------- End time functions -------------


------------- Begin local variables -------------

-- Debug variables:
debug                  = uservariables["debug_general"]
timeout                = uservariables["timeout_general"]
difference_homework    = timedifference_lastupdate_homework(otherdevices_lastupdate['TravelToWork'])
difference_workhome    = timedifference_lastupdate_workhome(otherdevices_lastupdate['TravelToHome'])

------------- End local variables -------------


------------- Begin debug statements -------------

if debug=="Y" 
  then
  print('<b style="color:Red">>> Debug is set to: ' .. debug .. '</b>')
  print('<b style="color:Red">>> Update timeout: ' .. timeout .. '</b>')
  print('<b style="color:Red">>> Lastupdate HomeWork  : ' .. difference_homework .. '</b>')
  print('<b style="color:Red">>> Lastupdate WorkHome  : ' .. difference_workhome .. '</b>')
end

------------- End debug statements -------------


------------- Begin code -------------

commandArray = {}

if (difference_homework > timeout and difference_workhome > 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()

   print("========================================================================================================")
   print(trajet)
   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 (already used with DTGBOT and stored in the standard library)
json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")()

-- Update these variables ---------------
--idx of devices for capturing the travel minutes in both direction
idxhomework = 'xxx'
idxworkhome = 'xxx'

--coordinates Home
departy="xx.xxxxxx"
departx="x.xxxxxx"

--Coordinates Work
arrivey="xx.xxxxxx"
arrivex="x.xxxxxx"

-- get travel time
homework=traveltime(departx,departy,arrivex,arrivey)
workhome=traveltime(arrivex,arrivey,departx,departy)
print("homework:",homework,"workhome:",workhome)

-- update Domoticz devices
url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxhomework .. '&nvalue=0&svalue=' .. homework
read = os.execute('curl -s "'..url..'"')
url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxworkhome .. '&nvalue=0&svalue=' .. workhome
read = os.execute('curl -s "'..url..'"')

else
   print('<b style="color:Red">>> HomeWork lastupdate: ' .. difference_homework .. ' seconds ago</b>')
   print('<b style="color:Red">>> WorkHome lastupdate: ' .. difference_workhome .. ' seconds ago</b>')

end

------------- End code -------------

return commandArray

sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sjefk »

Hi D'rMorris,
i used you LUA solution but get an error 6 Bad argument #1 'sub' (string expected, got nil).
Am i doing something wrong ?
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Traffic with waze in Domoticz

Post by D'rMorris »

sjefk wrote:Hi D'rMorris,
i used you LUA solution but get an error 6 Bad argument #1 'sub' (string expected, got nil).
Am i doing something wrong ?
Did you create the 2 user variables debug_general (type: string, value either Y or N) & timeout_general (type: integer, value in my case is 600 (10 minutes))?
Next to that, did you create 2 virtual devices called TravelToWork and TravelToHome? In my case I created virtual sensors of the type "pressure".

Code: Select all

debug                  = uservariables["debug_general"]
timeout                = uservariables["timeout_general"]
difference_homework    = timedifference_lastupdate_homework(otherdevices_lastupdate['TravelToWork'])
difference_workhome    = timedifference_lastupdate_workhome(otherdevices_lastupdate['TravelToHome'])
sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sjefk »

I forgot the uservariables :(
Just added them and renamed my 2 virtual pressure sensor as described in your code.
But still i get the some error, immediatly after the blue text Monitoring Traveltimes
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Traffic with waze in Domoticz

Post by D'rMorris »

Did you also put the JSON file here: json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")()?
sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sjefk »

Yep..

pi@raspberrypi ~/domoticz/scripts/lua $ ls -lrt JS*
-rw-r--r-- 1 pi pi 34843 Oct 12 18:06 JSON.lua
pi@raspberrypi ~/domoticz/scripts/lua $

And the JSON was in uppercase so i changed it in the lua script to:

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

But this also did not help..
sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sjefk »

Problem is clearly in otherdevices_lastupdate, when i use a different device as example i get this:

LUA: 2016-01-07 05:20:25

So perhaps i did not create the virtual pressure sensor correctly.
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Traffic with waze in Domoticz

Post by D'rMorris »

When you renamed the virtual sensors, did you copy it exactly like this:
TravelToWork
TravelToHome
The device names are case sensitive, so device TravelToWork is not the same as traveltowork for example.
sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sjefk »

Yes i copy, paste'd it.
I put these print lines at the top of the script.

print(otherdevices_lastupdate['TravelToWork'])
print(otherdevices_lastupdate['TravelToHome'])
print(otherdevices_lastupdate['Weegschaal'])
print(otherdevices_lastupdate['Denon Status'])

And it only reports the last 2, one of which is also a virtual (text) sensor.

2016-01-07 11:35:00.419 LUA: > Monitoring Traveltimes
2016-01-07 11:35:00.419 LUA: test
2016-01-07 11:35:00.419 LUA: 2016-01-07 05:20:25
2016-01-07 11:35:00.419 LUA: 2016-01-07 11:34:02
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Traffic with waze in Domoticz

Post by D'rMorris »

Strange... When I look at my devices, the 2 virtual sensors are:
Hardware:VirtualSwitches
Name: TravelToHome
Type: General
Subtype: Pressure
Data: 56.0 Bar
Lastupdate: 2016-01-07 11:41:20

and

Hardware:VirtualSwitches
Name: TravelToWork
Type: General
Subtype: Pressure
Data: 56.0 Bar
Lastupdate: 2016-01-07 11:41:20

Can you check if the lastupdate has a value in the devices section? Otherwise, first start with the script below.

Code: Select all

    print('<b style="color:Blue">>   Monitoring Traveltimes</b>')

    ------------- Begin code -------------

    commandArray = {}

    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()

       print("========================================================================================================")
       print(trajet)
       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 (already used with DTGBOT and stored in the standard library)
    json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")()

    -- Update these variables ---------------
    --idx of devices for capturing the travel minutes in both direction
    idxhomework = 'xxx'
    idxworkhome = 'xxx'

    --coordinates Home
    departy="xx.xxxxxx"
    departx="x.xxxxxx"

    --Coordinates Work
    arrivey="xx.xxxxxx"
    arrivex="x.xxxxxx"

    -- get travel time
    homework=traveltime(departx,departy,arrivex,arrivey)
    workhome=traveltime(arrivex,arrivey,departx,departy)
    print("homework:",homework,"workhome:",workhome)

    -- update Domoticz devices
    url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxhomework .. '&nvalue=0&svalue=' .. homework
    read = os.execute('curl -s "'..url..'"')
    url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxworkhome .. '&nvalue=0&svalue=' .. workhome
    read = os.execute('curl -s "'..url..'"')

    ------------- End code -------------

    return commandArray

sjefk
Posts: 24
Joined: Thursday 07 January 2016 7:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by sjefk »

I found out the problem was indeed that there was no data in the virtual sensor
I updated the values manually via JSON.
Now it works :)

Thanks for your time D'rMorris !
ubfssF
Posts: 59
Joined: Monday 02 November 2015 14:12
Target OS: Linux
Domoticz version: 2.2364
Location: Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by ubfssF »

Forget this post
Last edited by ubfssF on Monday 25 January 2016 16:52, edited 2 times in total.
ubfssF
Posts: 59
Joined: Monday 02 November 2015 14:12
Target OS: Linux
Domoticz version: 2.2364
Location: Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by ubfssF »

D'rMorris wrote:Strange... When I look at my devices, the 2 virtual sensors are:
Hardware:VirtualSwitches
Name: TravelToHome
Type: General
Subtype: Pressure
Data: 56.0 Bar
Lastupdate: 2016-01-07 11:41:20

and

Hardware:VirtualSwitches
Name: TravelToWork
Type: General
Subtype: Pressure
Data: 56.0 Bar
Lastupdate: 2016-01-07 11:41:20

Can you check if the lastupdate has a value in the devices section? Otherwise, first start with the script below.

Code: Select all

    print('<b style="color:Blue">>   Monitoring Traveltimes</b>')

    ------------- Begin code -------------

    commandArray = {}

    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()

       print("========================================================================================================")
       print(trajet)
       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 (already used with DTGBOT and stored in the standard library)
    json = (loadfile "/home/pi/domoticz/scripts/lua/json.lua")()

    -- Update these variables ---------------
    --idx of devices for capturing the travel minutes in both direction
    idxhomework = 'xxx'
    idxworkhome = 'xxx'

    --coordinates Home
    departy="xx.xxxxxx"
    departx="x.xxxxxx"

    --Coordinates Work
    arrivey="xx.xxxxxx"
    arrivex="x.xxxxxx"

    -- get travel time
    homework=traveltime(departx,departy,arrivex,arrivey)
    workhome=traveltime(arrivex,arrivey,departx,departy)
    print("homework:",homework,"workhome:",workhome)

    -- update Domoticz devices
    url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxhomework .. '&nvalue=0&svalue=' .. homework
    read = os.execute('curl -s "'..url..'"')
    url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxworkhome .. '&nvalue=0&svalue=' .. workhome
    read = os.execute('curl -s "'..url..'"')

    ------------- End code -------------

    return commandArray

I got an error after retrieving data from Waze:
"...........................
{"x":4.4859796245308,"y":51.905177579834,"z":"NaN"},{"x":4.4859796245308,"y":51.905177579834,"z":"NaN"},{"x":4.485384204847058,"y":51.90486958680452,"z":"NaN"},{"x":4.485384204847058,"y":51.90486958680452,"z":"NaN"},{"x":4.4847550607899835,"y":51.90454488519832,"z":"NaN"},{"x":4.4847550607899835,"y":51.90454488519832,"z":"NaN"},{"x":4.484661151354932,"y":51.904497777241176,"z":"NaN"},{"x":4.484617489349021,"y":51.90447587497455,"z":"NaN"},{"x":4.484617489349021,"y":51.90447587497455,"z":"NaN"},{"x":4.484446818065281,"y":51.904488792804386,"z":"NaN"},{"x":4.484350476380506,"y":51.90454638986189,"z":"NaN"}],"segCoords":null}
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 48 100 48 0 0 301 0 --:--:-- --:--:-- --:--:-- 303
========================================================================================================
{ "error" : "Error: 503|Could not find a route"}
lua: script_waze_reistijden.lua:18: attempt to index field 'response' (a nil value)
stack traceback:
script_waze_reistijden.lua:18: in function 'traveltime'
script_waze_reistijden.lua:52: in main chunk
[C]: in ?
"
____________________________________________________________________________________________________________________________
After changing the lat-lon at my work a bit it worked; in front of it is a one way road and Waze couldn't cope with that apparantly. :D

Now the log displays:
2016-01-25 16:35:02.588 LUA: homework:
2016-01-25 16:35:02.588 LUA: 26
2016-01-25 16:35:02.588 LUA: workhome:
2016-01-25 16:35:02.588 LUA: 26
Only the virtual devices get no values :evil:

When I put http://192.168.123.127:8080/json.htm?ty ... evice&idx=' .. 64 .. '&nvalue=0&svalue=' .. 26 in a browser, I get "status" : "ERR"

___________________________________________________________________________________________________________
I had to change the last part of the script and then it worked! Now the scripts ends like this:

Code: Select all

    -- get travel time
    homework=traveltime(departx,departy,arrivex,arrivey)
    workhome=traveltime(arrivex,arrivey,departx,departy)
    print("homework:",homework,"workhome:",workhome)

    -- update Domoticz devices
    -- url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxhomework .. '&nvalue=0&svalue=' .. homework
    -- read = os.execute('curl -s "'..url..'"')
    -- url='http://IP:PORT/json.htm?type=command&param=udevice&idx=' .. idxworkhome .. '&nvalue=0&svalue=' .. workhome
    -- read = os.execute('curl -s "'..url..'"')
    commandArray[1] = {['UpdateDevice'] = idxhomework .. '|0|' .. homework}
    commandArray[2] = {['UpdateDevice'] = idxworkhome .. '|0|' .. workhome}
    ------------- End code -------------

    return commandArray
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Traffic with waze in Domoticz

Post by D'rMorris »

Did you try settings --> more --> user variables ;-)?
Raspberry Piet
Posts: 158
Joined: Saturday 11 January 2014 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: NL
Contact:

Re: Traffic with waze in Domoticz

Post by Raspberry Piet »

Since yesterday possible to choose for custom sensor in Domoticz! Very nice option i think.

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

Re: Traffic with waze in Domoticz

Post by remb0 »

Raspberry Piet wrote:Since yesterday possible to choose for custom sensor in Domoticz! Very nice option i think.

Image
Great tip! I made also the icons, thanks :)
Attachments
Waze_icons.zip
(14.51 KiB) Downloaded 181 times
Pierryck
Posts: 24
Joined: Saturday 27 February 2016 20:39
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by Pierryck »

My virtual device is a general text, which one do you take to change the icon ?
Raspberry Piet
Posts: 158
Joined: Saturday 11 January 2014 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: NL
Contact:

Re: Traffic with waze in Domoticz

Post by Raspberry Piet »

remb0 wrote:
Raspberry Piet wrote:Since yesterday possible to choose for custom sensor in Domoticz! Very nice option i think.

Image
Great tip! I made also the icons, thanks :)

Good! And here are the icons for the people don't like their job ;) :
Attachments
Waze_sad_icons.zip
(7.16 KiB) Downloaded 130 times
Raspberry Piet
Posts: 158
Joined: Saturday 11 January 2014 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: NL
Contact:

Re: Traffic with waze in Domoticz

Post by Raspberry Piet »

Pierryck wrote:My virtual device is a general text, which one do you take to change the icon ?
Custom Sensor. Available in Beta version.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest