[HowTo] Setting Up commute time with Bing Maps

Hello Everybody,

with this flow you can setup a Sensor for you daily commute time for example to your Office.
It will include the traffic jam time.
You will need a BingMaps API Key. you can get it from here: https://www.bingmapsportal.com

Import the following Flow in Node-Red:

[
    {
        "id": "5a94a709.5719c8",
        "type": "tab",
        "label": "BingMapsTraffic2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "824fd5d.3486e28",
        "type": "http request",
        "z": "5a94a709.5719c8",
        "name": "askBing",
        "method": "GET",
        "ret": "obj",
        "url": "",
        "x": 460,
        "y": 220,
        "wires": [
            [
                "8988d4fa.7a95e8"
            ]
        ]
    },
    {
        "id": "8988d4fa.7a95e8",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "extractData",
        "func": "/*\nreturns:\n1. travelDurationTraffic in min\n2. travelDuration in min\n3. traveDelay in min\n4. trafficJam boolean\n5. trafficCongestion string\n6. travelDistance in km\n7. msg object (for debugging and return to the loop)\n*/\n\nvar travelDurationTraffic = Math.ceil(msg.payload.resourceSets[0].resources[0].travelDurationTraffic / 60);\nvar travelDuration = Math.ceil(msg.payload.resourceSets[0].resources[0].travelDuration / 60);\n\nvar msgTraffic = [\n    {\n        topic: msg.topic_prefix + \".name \",\n        payload: msg.name\n        },\n    {\n        topic: msg.topic_prefix + \".travelDurationTraffic \",\n        payload: travelDurationTraffic\n        },\n    { \n        topic: msg.topic_prefix + \".travelDuration \",\n        payload: travelDuration\n        },\n    {\n        topic: msg.topic_prefix + \".travelDelay \",\n        payload: travelDurationTraffic - travelDuration\n        },\n    { \n        topic: msg.topic_prefix + \".trafficJam \",\n        payload: (travelDurationTraffic - travelDuration >= msg.threshold)\n        },\n    { \n        topic: msg.topic_prefix + \".trafficCongestion \",\n        payload: msg.payload.resourceSets[0].resources[0].trafficCongestion\n        },\n    { \n        topic: msg.topic_prefix + \".travelDistance \",\n        payload: msg.payload.resourceSets[0].resources[0].travelDistance\n        },\n        msg\n];\n\nreturn msgTraffic;",
        "outputs": "8",
        "noerr": 0,
        "x": 353.5555419921875,
        "y": 380.1111145019531,
        "wires": [
            [
                "73a97c0e.1cb064",
                "584e4bf8.289514",
                "3f78999e.7bf6f6"
            ],
            [
                "73a97c0e.1cb064",
                "729355f6.8207dc",
                "3f78999e.7bf6f6"
            ],
            [
                "73a97c0e.1cb064",
                "d3fd81f2.6d178",
                "3f78999e.7bf6f6"
            ],
            [
                "73a97c0e.1cb064",
                "f0e992a3.1d42b",
                "3f78999e.7bf6f6"
            ],
            [
                "73a97c0e.1cb064",
                "c69255cb.461bf8",
                "3f78999e.7bf6f6"
            ],
            [
                "73a97c0e.1cb064",
                "54548ec5.0a50b",
                "3f78999e.7bf6f6"
            ],
            [
                "73a97c0e.1cb064",
                "a5f1d86e.c8a248",
                "3f78999e.7bf6f6"
            ],
            [
                "a2229ff.f7a6d6"
            ]
        ]
    },
    {
        "id": "61297dc1.a8ffa4",
        "type": "inject",
        "z": "5a94a709.5719c8",
        "name": "Trigger",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "x": 124.88883209228516,
        "y": 66.00001335144043,
        "wires": [
            [
                "a0b3e1ef.f2201"
            ]
        ]
    },
    {
        "id": "26bf5d6.91e54a2",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "defineRoutes",
        "func": "/*\nDefine routes:\nfrom: starting address, encode spaces with %20\nto: destination address, encode spaces with %20\nthreshold: in minutes, if duration with traffic is larger than threshold, set trafficJam to true\nname: name :-)\n*/\n\nmsg.routes = [\n {\n from: 'Hum%2037,81234%20Muenchhausen',\n to: 'Sepplstrasse%2069,84321%20Humburg',\n threshold: 5,\n name: 'Home2Office'\n },\n {\n from: 'Sepplstrasse%2069,84321%20Humburg',\n to: 'Hum%2037,81234%20Muenchhausen',\n threshold: 5,\n name: 'Office2Home'\n }\n];\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 490,
        "y": 60,
        "wires": [
            [
                "a2229ff.f7a6d6"
            ]
        ]
    },
    {
        "id": "5511566d.52ce58",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "prepareIteration",
        "func": "// msg.method = \"GET\";\nmsg.url = \"http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1=\" + msg.payload.from + \"&wayPoint.2=\" + msg.payload.to + \"&optimize=timeWithTraffic&mfa=1&key=\" + msg.config.bing_key;\nmsg.topic_prefix = msg.config.topic_prefix + msg.payload.name;\nmsg.name = msg.payload.name;\nmsg.threshold = msg.payload.threshold;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 298.22222900390625,
        "y": 220.5555419921875,
        "wires": [
            [
                "824fd5d.3486e28"
            ]
        ]
    },
    {
        "id": "a0b3e1ef.f2201",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "setConfig",
        "func": "msg.config = {\n    bing_key: \"xxxxxxxxxxxx\",\n    topic_prefix: \"node - red .0.traffic.\"\n    };\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 297.11106872558594,
        "y": 66.55555725097656,
        "wires": [
            [
                "26bf5d6.91e54a2",
                "7f4343ac.267a9c"
            ]
        ]
    },
    {
        "id": "a2229ff.f7a6d6",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "loopRoutes",
        "func": "context.routes = context.routes || msg.routes;\n\n// iterate through all the routes\nif (context.routes.length > 0) {\n msg.payload = context.routes.shift();\n return [null, msg];\n}\n\n// Reset routes-context, otherwise we immediately stop in the next run\ncontext.routes = false;\n// we are done with the loop\nreturn [msg, null];",
        "outputs": "2",
        "x": 208.6666259765625,
        "y": 158.22222900390625,
        "wires": [
            [],
            [
                "5511566d.52ce58"
            ]
        ]
    },
    {
        "id": "7f4343ac.267a9c",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "",
        "active": false,
        "console": "false",
        "complete": "true",
        "x": 690,
        "y": 140,
        "wires": []
    },
    {
        "id": "73a97c0e.1cb064",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "",
        "active": false,
        "console": "false",
        "complete": "payload",
        "x": 706.6666259765625,
        "y": 195.22222900390625,
        "wires": []
    },
    {
        "id": "d02717b9.ca35c8",
        "type": "catch",
        "z": "5a94a709.5719c8",
        "name": "",
        "x": 669.6666259765625,
        "y": 68.22222900390625,
        "wires": [
            [
                "7f4343ac.267a9c"
            ]
        ]
    },
    {
        "id": "584e4bf8.289514",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "name",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 650,
        "y": 300,
        "wires": []
    },
    {
        "id": "729355f6.8207dc",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "travelDurationTraffic",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 700,
        "y": 340,
        "wires": []
    },
    {
        "id": "d3fd81f2.6d178",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "travelDuration",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 680,
        "y": 380,
        "wires": []
    },
    {
        "id": "f0e992a3.1d42b",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "travelDelay",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 670,
        "y": 420,
        "wires": []
    },
    {
        "id": "c69255cb.461bf8",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "trafficJam",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 660,
        "y": 460,
        "wires": []
    },
    {
        "id": "54548ec5.0a50b",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "trafficCongestion",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 690,
        "y": 500,
        "wires": []
    },
    {
        "id": "a5f1d86e.c8a248",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "travelDistance",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 680,
        "y": 540,
        "wires": []
    },
    {
        "id": "3f78999e.7bf6f6",
        "type": "join",
        "z": "5a94a709.5719c8",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "7",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 650,
        "y": 600,
        "wires": [
            [
                "24235771.daf5d8",
                "b91f9c7c.77ea4"
            ]
        ]
    },
    {
        "id": "a3240c73.b6dd8",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 970,
        "y": 700,
        "wires": []
    },
    {
        "id": "24235771.daf5d8",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "Home2Office",
        "func": "if (msg.topic === \"node - red .0.traffic.Home2Office.travelDistance \")\n{\n    return msg\n}",
        "outputs": 1,
        "noerr": 0,
        "x": 630,
        "y": 700,
        "wires": [
            [
                "dc6ba8cf.aa78f8"
            ]
        ]
    },
    {
        "id": "b91f9c7c.77ea4",
        "type": "function",
        "z": "5a94a709.5719c8",
        "name": "Office2Home",
        "func": "if (msg.topic === \"node - red .0.traffic.Office2Home.travelDistance \")\n{\n    return msg\n}",
        "outputs": 1,
        "noerr": 0,
        "x": 630,
        "y": 740,
        "wires": [
            [
                "90829605.de2168"
            ]
        ]
    },
    {
        "id": "2b2a116d.01ff8e",
        "type": "debug",
        "z": "5a94a709.5719c8",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 970,
        "y": 740,
        "wires": []
    },
    {
        "id": "90829605.de2168",
        "type": "ha-api",
        "z": "5a94a709.5719c8",
        "name": "",
        "server": "",
        "protocol": "http",
        "method": "post",
        "path": "/api/states/sensor.bingmapstraffic_office2home",
        "data": "{\"state\":\"{{payload.1}}\",\"attributes\":{\"name\":\"{{payload.0}}\",\"travelDurationTraffic\":\"{{payload.1}} Minuten\",\"trafelDuration\":\"{{payload.2}} Minuten\",\"trafelDelay\":\"{{payload.3}} Minuten\",\"trafficJam\":\"{{payload.4}}\",\"trafficCongestion\":\"{{payload.5}}\",\"trafelDistance\":\"{{payload.6}} km\",\"unit_of_measurement\":\"min\"}}",
        "location": "payload",
        "locationType": "msg",
        "responseType": "json",
        "x": 810,
        "y": 740,
        "wires": [
            [
                "2b2a116d.01ff8e"
            ]
        ]
    },
    {
        "id": "dc6ba8cf.aa78f8",
        "type": "ha-api",
        "z": "5a94a709.5719c8",
        "name": "",
        "server": "",
        "protocol": "http",
        "method": "post",
        "path": "/api/states/sensor.bingmapstraffic_home2office",
        "data": "{\"state\":\"{{payload.1}}\",\"attributes\":{\"name\":\"{{payload.0}}\",\"travelDurationTraffic\":\"{{payload.1}} Minuten\",\"trafelDuration\":\"{{payload.2}} Minuten\",\"trafelDelay\":\"{{payload.3}} Minuten\",\"trafficJam\":\"{{payload.4}}\",\"trafficCongestion\":\"{{payload.5}}\",\"trafelDistance\":\"{{payload.6}} km\",\"unit_of_measurement\":\"min\"}}",
        "location": "payload",
        "locationType": "msg",
        "responseType": "json",
        "x": 810,
        "y": 700,
        "wires": [
            [
                "a3240c73.b6dd8"
            ]
        ]
    }
]

You have to configure the “setConfig” and “defineRoutes” Node with your API Key and the Adress you want to use.
In the “API” Node you have to add a new Home Assistant Server.

Have Fun! :slight_smile:

If you have any Questions feel free to ask.

BingMaps is free for fewer than 125,000 transactions per calendar year on websites and mobile apps that are not on/for the Windows Phone.

I think this should be OK.

Regards Saboti