Jacopo1891 / MMM-GoogleTrafficTimes

A MagicMirror module that shows travel times taking into account Google traffic information
16 stars 4 forks source link
google-maps magic-mirror-modules magicmirror2 mirror raspberry-pi smarthome transport

Module: Google Traffic Times

A module for the MagicMirror that displays driving times from a location to one or more destinations based on Google Maps Traffic information. As it uses the times in traffic the values are dynamic as long as there is reasonaby accurate traffic detail available to Google in your area. If the in traffic travel time is longer then a symbol will be showed.

Table of contents

Installation

Navigate into your MagicMirror's ~/MagicMirror/modules folder and execute

git clone https://github.com/Jacopo1891/MMM-GoogleTrafficTimes.git 

A new folder will be created, please navigate into it. Once in ~/MagicMirror/modules/MMM-GoogleTrafficTimes run:

npm install

to install the module and dependencies.

Using the module

To use this module, add it to the modules array in the config/config.js file:

var config = {
    modules: [
        {
            module: 'MMM-GoogleTrafficTimes',
            position: 'top_left',
            config: {
                key: 'YOUR_KEY',
                origin: 'SW1A 1AA',
                destinations: [
                    {
                        name: "Work",
                        address: "SW1A 2PW"
                    },
                    {
                        name: "Gym",
                        address: "XXX"
                    }
                ],
                updateInterval: 900000,
                avoidHighways: false,
                avoidTolls: false,
                mode: 'driving',
                language: "en-EN",
                offsetTime: 25,
                lastUpdate: true,
                timeLastUpdateWarning: 1,
                horizontalLayout: false,
                schedules: [],
                debug: false
            },
        }
    ]
}

The Destinations need to be entered in the form

[
    {
        name: "Work",
        address: "SW1A 2PW"
    }
]

In the example config for Destination 1 we have a Label of Work with an Address of SW1A 2PW.

The Label name appears as the title for each result as shown in the Example Screenshot below.

In this release the origin and destination addresses have been tested across a large number of countries but certainly not all.

Whilst the Google API can accept a multitude of formats from addresses to lat&long co-ordinates this script has some matching code to make the results format nicely and this could have issues with an as yet untried address format.

Google API Key

In order to use this module you will need a Google Maps API which is available from the Google GCP console. You will need to enable the following APIs for your key, Maps JavaScript API, Geocoding API, Distance Matrix API.

Offset Time

To determine if the road is busy or not, I decided to add the optimal time (meaning without traffic by Google Matrix) with an offset obtained using this simple formula: optimalTime + (optimalTime * offset) If the estimated time from Google Matrix is greater than this value, it means there is traffic, and the symbol is displayed. By default, the offset is set to 25%, which seems like a good compromise.

Schedules

The schedules parameter accepts an array of objects, each representing a schedule for module display.
Each object in the list must include the following keys:
days: An array of numbers representing the days of the week to apply the schedule. Day numbers correspond to the following values: 0 (Sunday) to 6 (Saturday). For example, [0, 1, 2, 3, 4] indicates that the schedule applies from Sunday to Thursday.
startHH: The starting hour of the schedule (in 24-hour format).
startMM: The starting minute of the schedule.
endHH: The ending hour of the schedule (in 24-hour format).
endMM: The ending minute of the schedule.
If the days array is empty, the content will be displayed at all times.

Here's an example of how to configure the schedules parameter:

schedules: [
    {
        days: [0, 1, 2, 3, 4], // From Sunday to Thursday
        startHH: "08",
        startMM: "00",
        endHH: "17",
        endMM: "30"
    },
    {
        days: [5], // Friday
        startHH: "08",
        startMM: "30",
    },
    {
        days: [], // Display at all times
        startHH: null,
        startMM: null,
        endHH: null,
        endMM: null
    }
]

This example sets up three schedules:
From Sunday to Thursday, display content from 08:00 to 17:30.
On Friday, display content from 08:30.
Display content at all times when the days array is empty.

Debug

  1. Stop any running instance of MagicMirror2.
  2. Make sure you are in the main directory of your project.
  3. Set debug: true on config.js as described in installation section.
  4. Execute the following command to start to see all logs.
    npm start dev

    or

    npm start dev | grep MMM-GoogleTrafficTimes

    to see only MMM-GoogleTrafficTimes's logs.

Example Screenshot

alt text

alt text

alt text

alt text

alt text

alt text

Suggestions

Please feel free to raise an issue on GitHub for any features you would like to see or usage issues you experience and I will endeavour to address them.

Buy me a coffee

Find it useful? Please consider buying me or other contributors a coffee.

blue-button