From Ituri

I wrote a short shell script which shows the current traffic congestion inside of pimatic. The script can be included as a ShellSensor and shows the time that it’ll take you to travel to your defined destination according to the current traffic. I use this every morning to see how long it’ll take me to get to work:

At first you see the time with traffic, then the quickest route and eventually the traffic congestion (None/Mild/…).
Here’s the section for your config.json

  "id": "traffic",
  "name": "Verkehr",
  "class": "ShellSensor",
  "attributeName": "traffic",
  "attributeType": "string",
  "attributeUnit": "",
  "command": "/usr/local/bin/",
  "interval": 600000

And here’s the script itself:

start='Examplestreet, Hometown'
end='Examplestreet, Destination'
data=$(curl -s\=$start\&waypoint.2\=$end\&optimize=time\&distanceUnit\=km\&key=$apikey)

twt=$(echo $data | jq '.resourceSets | .[] | .resources | .[] | .travelDurationTraffic')
y=$((twt / 60))
traffic=$(echo $data | jq '.resourceSets | .[] | .resources | .[] | .trafficCongestion' | sed 's/"//' | sed 's/"//')
route=$(echo $data | jq '.resourceSets | .[] | .resources | .[] | .routeLegs | .[] | .description' | sed 's/"//' | sed 's/"//')

echo "$y Minuten via $route ($traffic)"

There are some things to do in order to get the script running:

  • Install jq. It’s needed to parse the data. Unfortunately, there’s no package available in raspbian so you’ll need to install it from source.
  • Get an API-key from bing. Here are instructions on how to get a key (it’s free!).
  • Add your API-key on top of the script. Also change your starting point and your destination. At the end, don’t forget to copy the script to /usr/bin/local.

That’s it. I know this is just an ugly hack, but I thought it might of help for some of you.