Traffic with waze in Domoticz

Moderator: leecollings

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 »

Raspberry Piet wrote:
Pierryck wrote:My virtual device is a general text, which one do you take to change the icon ?
Custom Sensor. Available in Beta version.
Awesome.. but which beta version ? I am running Version: 3.4933 but can not select a custom sensor..
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 »

sjefk wrote:
Raspberry Piet wrote:
Pierryck wrote:My virtual device is a general text, which one do you take to change the icon ?
Custom Sensor. Available in Beta version.
Awesome.. but which beta version ? I am running Version: 3.4933 but can not select a custom sensor..
Forget my post.. was not looking closely :(
konix
Posts: 5
Joined: Wednesday 24 September 2014 8:40
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by konix »

Hello all , I use a script below to the rise of Waze information under domoticz and it works perfectly on a custom sensor :-) . I would like now blocked configuration journey on one trip ? Have you an idea of ​​the configuration to perform to freeze the desired road ?


Below my my request

Code: Select all

ocal 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()
Thank you in advance.
Raspberry PI 2 whith Domoticz (Master)
Raspberry PI B+ whith Domoticz (Slave)
Raspberry PI B+ whith OSMC
RFLINK - ZWAVE - TELEINFO - 1 WIRE
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 »

I got almost every minute this error message:
2016-03-24 21:11:01.165 Error: EventSystem: in script_time_Waze: /home/pi/domoticz/scripts/lua/JSON.lua:383: html passed to JSON:decode():
503 Service Temporarily Unavailable

Maybe it's coincidence , but since I installed pi-hole I get this error . waze.com is already on my whitelist and I have data in my sensors so it looks good. How can I debug it and get rid of this message?
ceedebee
Posts: 44
Joined: Sunday 08 March 2015 20:18
Target OS: -
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by ceedebee »

I'm using the waze script but with ssl socket instead of curl to get status information from the https call (or does curl also include this?).
The only problem is I'm getting a time out once a day. I already added a timeout, but with no luck.
Any idea about how to solve this?
woody4165
Posts: 476
Joined: Monday 14 March 2016 13:55
Target OS: Linux
Domoticz version: beta
Location: Rome, Italy
Contact:

Re: Traffic with waze in Domoticz

Post by woody4165 »

Very nice script.
I'm trying also to send a telegram message when traffic is more than a certain value.
The only thing is that I get several messages because the script runs several time during the 1 minute checking timeframe.
I tried using an UserVariable and set it to 0 if it has to send Telegram message and 1 when it has sent the message.

Code: Select all

--      Invia messagio a Telegram bot per traffico  
        if (routeTotalTimeMin > 20 and uservariables["solouna"]<1) then
            minuti = tostring(routeTotalTimeMin)
            message = minuti..' minuti per andare in ufficio alle '..time.hour..':'..time.min..' !!!'
            os.execute('curl --data chat_id=xxxxxxxx--data-urlencode "text='..message..'"  "https://api.telegram.org/botxxxxxxxxx/sendMessage" ')
            uservariables["solouna"]=1

        end            
Then I set back to 0 when it goes out of the checking timeframe.

But it's not working...
Any suggestion?

Thanks
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
dervogt
Posts: 26
Joined: Thursday 30 June 2016 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Amsterdam
Contact:

Re: Traffic with waze in Domoticz

Post by dervogt »

just FYI: I just had some issues with the script after initial implementation, giving no travel times back anymore on previously good geo coordinates: all of a sudden there was an internal error in the route calculation. After verifying the geocoordinates on the Waze life map (NOTon Google maps, en re-setting them in the script, the script is returning travel times again.

I am not sure if this was a change on the Waze side of the routing API or just inaccurate (but previously working) geo coordinates.

Thought I am quickly going to share this if anyone else xperiences the same issue as well
matthiasblomme
Posts: 1
Joined: Monday 06 March 2017 14:12
Target OS: Windows
Domoticz version:
Contact:

Re: Traffic with waze in Domoticz

Post by matthiasblomme »

Has anyone checked if you can get a list or details of your scheduled drives?
That would be even more fitting for your home environment.
I'm currently looking into waze api's but I'm not finding that much
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Traffic with Waze in Domoticz (LUA/BASH)

Post by lemassykoi »

Hi all,

I slightly adapted this script for my use, and it's perfect. I can monitor, as someone write earlier, traffic time, and see at which time it's better to leave.

Before first run, you need to :
  • Create two User Variables : (settings --> more --> user variables)
    debug_general (type "String", value : "N" for No or "Y" for Yes)
    timeout_general (type "Integer", value : "600" in seconds, the time needed to update Travel Time)
  • Create two Virtual Sensors (I used "Custom Sensor" with "minutes" for Axis Label)
    change their name to "Home to Work" and "Work to Home" (or whatever you want, but you'll need to change values in the script).
    Also take note of their IDX
  • Find your correct Lat / Long, by browsing https://www.waze.com/fr/livemap, enter your address, then click on the Blue marker to get the Permalink. Values will be shown in the Permalink URL.
  • And finally, replace variables in the script (if your values differs)

Code: Select all

-- script_time_Waze.lua
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 -------------
-- User variables --
----------------------
-- IP Address and port
IP = '192.168.1.10'
port = '8080'

--idx of devices for capturing the travel time (in minutes) in both direction
idxhomework = '120' -- Home to Work
idxworkhome = '121' -- Work to Home
HomeToWork = 'Home to Work' -- Replace by the name of your dummy sensor
WorkToHome = 'Work to Home' -- Replace by the name of your dummy sensor

--Coordinates Home
-- https://www.waze.com/fr/livemap?zoom=17&lat=48.79687&lon=2.31007 -- Ex. : Bagneux, France
departy = '48.79687'
departx = '2.31007'

--Coordinates Work
-- https://www.waze.com/fr/livemap?zoom=17&lat=48.88988&lon=2.24181 -- Ex. : La Défense, France
arrivey =  '48.88988'
arrivex =  '2.24181'

----------------------
-- Debug variables:
----------------------
debug                  = uservariables["debug_general"]
timeout                = uservariables["timeout_general"]
difference_homework    = timedifference_lastupdate_homework(otherdevices_lastupdate[HomeToWork])
difference_workhome    = timedifference_lastupdate_workhome(otherdevices_lastupdate[WorkToHome])
------------- 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 Home to Work  : ' .. difference_homework .. '</b>')
  print('<b style="color:Red"> >> Lastupdate Work to Home  : ' .. 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 -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0" --referer "https://www.waze.com" "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()

if debug=="Y" 
  then
   print("========================================================================================================")
   print(trajet)
   print("========================================================================================================")
end

   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")() -- check presence of this script if problem

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

if debug=="Y" 
  then
print('Home to Work : ' .. homework .. ' minutes')
print('Work to Home : ' .. workhome .. ' minutes')
end

-- 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..'"')
print('<b style="color:Green"> >> Update OK !</b>')
    
else
    if debug=="Y" 
  then
   print('<b style="color:Red">>> HomeWork lastupdate: ' .. difference_homework .. ' seconds ago. Must be > to '.. timeout ..'</b>')
   print('<b style="color:Red">>> WorkHome lastupdate: ' .. difference_workhome .. ' seconds ago. Must be > to '.. timeout ..'</b>')
end

end

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

return commandArray
If you want to see DEBUG, change User Variable "debug_general" to "Y"

Big Thanks to D'rMorris

EDIT :

I switched to bash to avoid errors like "more ten seconds... blablabla"
so, the same in BASH :

Code: Select all

#!/bin/bash

## user variables
DOMOTICZ_SERVER="127.0.0.1:8080"
IDXA="122"		## IDX du dummy Maison-Travail
IDXR="123"		## IDX du dummy Travail-Maison
departx="2.44177"     ## Maison
departy="48.62994"
arriveex="2.25733"    ## Travail
arriveey="48.79998"
tmpfileA="/tmp/wazeAller.txt"
tmpfileR="/tmp/wazeRetour.txt"
log="/tmp/wazelog.txt"
rm -rf $log
rm -rf $tmpfileA
rm -rf $tmpfileR
#echo "Start." > $log

## CURL de l'Aller
urlA='https://www.waze.com/row-RoutingManager/routingRequest?from=x%3A'$departx'+y%3A'$departy'&to=x%3A'$arriveex'+y%3A'$arriveey'&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS%3At%2CALLOW_UTURNS'
/usr/bin/curl -A 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0' --referer 'https://www.waze.com' -s -o $tmpfileA $urlA
#echo "urlA : "$urlA >> $log

## Sleep to avoid 503 errors
sleep 5

## CURL du Retour
urlR='https://www.waze.com/row-RoutingManager/routingRequest?from=x%3A'$arriveex'+y%3A'$arriveey'&to=x%3A'$departx'+y%3A'$departy'&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS%3At%2CALLOW_UTURNS'
/usr/bin/curl -A 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0' --referer 'https://www.waze.com' -s -o $tmpfileR $urlR
#echo "urlR : "$urlR >> $log

## calcul Aller
tempstrajetminsA=$(/usr/local/bin/jq -r .response.results[].crossTime $tmpfileA | /usr/bin/awk '{s+=$1} END {print s}' | /usr/bin/awk '{sum = $1 / 60 ; print sum }' | /usr/bin/cut -f1 -d\. )
#echo "Temps trajet Aller : "$tempstrajetminsA >> $log

## calcul Retour
tempstrajetminsR=$(/usr/local/bin/jq -r .response.results[].crossTime $tmpfileR | /usr/bin/awk '{s+=$1} END {print s}' | /usr/bin/awk '{sum = $1 / 60 ; print sum }' | /usr/bin/cut -f1 -d\. )
#echo "Temps trajet Retour: "$tempstrajetminsR >> $log

## Resultats
echo "Temps de Trajet Aller  : $tempstrajetminsA mn"
echo "Temps de Trajet Retour : $tempstrajetminsR mn"

## updating dummy sensors
updateA='http://'$DOMOTICZ_SERVER'/json.htm?type=command&param=udevice&idx='$IDXA'&nvalue=0&svalue='$tempstrajetminsA
updateR='http://'$DOMOTICZ_SERVER'/json.htm?type=command&param=udevice&idx='$IDXR'&nvalue=0&svalue='$tempstrajetminsR

#echo "URL Update A : "$updateA >> $log
#echo "URL Update R : "$updateR >> $log

/usr/bin/curl -s $updateA &
/usr/bin/curl -s $updateR

exit 0
edited to add UserAgent and Referer to LUA (not tested) and Bash (tested) - 20170814
Last edited by lemassykoi on Monday 14 August 2017 16:31, edited 2 times in total.
krizzz
Posts: 200
Joined: Wednesday 20 November 2013 20:36
Target OS: Linux
Domoticz version:
Location: The Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by krizzz »

Thanks for the script! When using the bash script I am getting the following error:

Code: Select all

waze.sh: line 30: /usr/local/bin/jq: No such file or directory
waze.sh: line 34: /usr/local/bin/jq: No such file or directory
Temps de Trajet Aller  : 0 mn
Temps de Trajet Retour : 0 mn
{
   "status" : "OK",
   "title" : "Update Device"
}
{
   "status" : "OK",
   "title" : "Update Device"
}
I googled and found that JQ is a program. But how do I install JQ so that it is available from the /usr/local/bin directory?
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by lemassykoi »

I build JQ from source, and I put my script in a CRON, that's why I specified all the binary paths.

You can install it from apt :

Code: Select all

sudo apt-get install jq
then locate the binary :

Code: Select all

whereis jq
then update the script to the correct path
Abbadon
Posts: 40
Joined: Thursday 01 October 2015 8:25
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Poland, Wrocław
Contact:

Re: Traffic with waze in Domoticz

Post by Abbadon »

sometimes i have problem with your script

Code: Select all

pi@raspberrypi:~/domoticz/scripts$ ./traffic.sh
parse error: Invalid numeric literal at line 1, column 7
Temps de Trajet Aller  : 21 mn
Temps de Trajet Retour : 0 mn
{
   "status" : "OK",
   "title" : "Update Device"
}
{
   "status" : "OK",
   "title" : "Update Device"
}

and sometimes its working fine...

Code: Select all

pi@raspberrypi:~/domoticz/scripts$ ./traffic.sh
Temps de Trajet Aller  : 21 mn
Temps de Trajet Retour : 19 mn
{
   "status" : "OK",
   "title" : "Update Device"
}
{
   "status" : "OK",
   "title" : "Update Device"
}
to prevent 0 i add if to your script, but its not a solution

Code: Select all

if [ $tempstrajetminsA -gt "0" ]; then
#echo "URL Update A : "$updateA >> $log
/usr/bin/curl -s $updateA
fi
if [ $tempstrajetminsR -gt "0" ]; then
#echo "URL Update R : "$updateR >> $log
/usr/bin/curl -s $updateR
fi
any idea?
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by lemassykoi »

I found this : https://github.com/stedolan/jq/issues/501

Try to replace this line :

Code: Select all

/usr/local/bin/jq -r .response.results[].crossTime
by this one :

Code: Select all

/usr/local/bin/jq -r '.response.results[].crossTime'
Abbadon
Posts: 40
Joined: Thursday 01 October 2015 8:25
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Poland, Wrocław
Contact:

Re: Traffic with waze in Domoticz

Post by Abbadon »

sadly no. ;|
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by lemassykoi »

It seems to be JQ/JSON related.

http://stackoverflow.com/questions/3479 ... 1-column-9

try to play with switch R and r, maybe Waze is not full JSON.

I checked my queries, and I don't have any "0 minutes" during past 48 hours.
krizzz
Posts: 200
Joined: Wednesday 20 November 2013 20:36
Target OS: Linux
Domoticz version:
Location: The Netherlands
Contact:

Traffic with waze in Domoticz

Post by krizzz »

I have the script working! Also But seem to have the exact same issue as user Abbadon. Sometimes the value 0 appears but only on the route from work to home
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by lemassykoi »

do any of you have a minus sign in one of his coordinates ?
krizzz
Posts: 200
Joined: Wednesday 20 November 2013 20:36
Target OS: Linux
Domoticz version:
Location: The Netherlands
Contact:

Re: Traffic with waze in Domoticz

Post by krizzz »

lemassykoi wrote:do any of you have a minus sign in one of his coordinates ?
No.

How did you check what were the results over a certain period?


Verzonden vanaf mijn iPhone met Tapatalk
Abbadon
Posts: 40
Joined: Thursday 01 October 2015 8:25
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Poland, Wrocław
Contact:

Re: Traffic with waze in Domoticz

Post by Abbadon »

lemassykoi wrote:It seems to be JQ/JSON related.

http://stackoverflow.com/questions/3479 ... 1-column-9

try to play with switch R and r, maybe Waze is not full JSON.

I checked my queries, and I don't have any "0 minutes" during past 48 hours.
Same, for now best solution is that IF statment
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

Re: Traffic with waze in Domoticz

Post by lemassykoi »

krizzz wrote:
lemassykoi wrote:do any of you have a minus sign in one of his coordinates ?
No.

How did you check what were the results over a certain period?


Verzonden vanaf mijn iPhone met Tapatalk
like this : click on "log" on the dummy sensor

Image
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests