# Node-RED: Travel Time Calculation API

**URL:** <https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962>\
**Category:** Configuration\
**Tags:** feature-integration\
**Created:** [February 16, 2022, 9:50pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962 "2022-02-16T21:50:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [February 16, 2022, 9:50pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/1 "2022-02-16T21:50:07Z")

</div>

**Feature description**

Hello community.

I wanted to know if there is a free API solution or another way to have on the dashboard the travel time to get to work for example and in my case more particularly to know the travel time to the 5 agencies where I work to not be late for work 😅

---

<div class="post-metadata">

**Author:** ![cicoub13](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/cicoub13/32/4482_2.png) [@cicoub13](https://community.gladysassistant.com/u/cicoub13)\
**Post date:** [February 17, 2022, 10:39am UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/2 "2022-02-17T10:39:22Z")

</div>

[TravelTime API Documentation | Isochrone | Distance Matrix | Directions](https://docs.traveltime.com/api/overview/introduction) works well and you can make 1000 requests per month for free

[Directions API | MapQuest Developer Documentation](https://developer.mapquest.com/documentation/directions-api/optimized-route/get/) allows 15,000 requests per month (never tried)

[Présentation de l'API Distance Matrix &nbsp;|&nbsp; Distance Matrix API (Legacy) &nbsp;|&nbsp; Google for Developers](https://developers.google.com/maps/documentation/distance-matrix/overview#Introduction) Google works great, but it’s paid (0.005 $ per request)

[Directions API | API Docs | Mapbox](https://docs.mapbox.com/api/navigation/directions/) MapBox is for me the **best** with 100K requests per month

---

<div class="post-metadata">

**Author:** ![cicoub13](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/cicoub13/32/4482_2.png) [@cicoub13](https://community.gladysassistant.com/u/cicoub13)\
**Post date:** [February 17, 2022, 10:54am UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/3 "2022-02-17T10:54:22Z")

</div>

However, I don’t know how to display it in the Dashboard. Maybe a virtual device?

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [February 26, 2022, 10:58pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/4 "2022-02-26T22:58:11Z")

</div>

Thanks for the links, I took Mapbox in the end.

I’ve integrated it into Node-RED and I’m receiving the information correctly. Now I need to extract the travel time to retrieve the information in MQTT and then display it on the dashboard.

@Psoy you manage Node-RED, a bit can you help if you can. How do I extract the duration and make a change to get the answer in minutes rather than seconds?

 ![Capture d’écran 2022-02-27 à 10.18.49](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/9/9f0d4ebc2e0ebd864e247befe1f38bbb4d6dade6.png)

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [February 27, 2022, 3:07pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/5 "2022-02-27T15:07:07Z")

</div>

Hello Jerome  
do your payloads always have the same size and the same shape with each query?

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [February 27, 2022, 3:10pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/6 "2022-02-27T15:10:30Z")

</div>

Jerome  
Could you send me a copy of your flow, please?

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [February 27, 2022, 3:35pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/7 "2022-02-27T15:35:30Z")

</div>

![Screenshot 2022-02-27 at 16.34.39](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/2/29b5e1c1f55ef68b58b39a6d91d06fd2681fc95d.png)

Apparently yes, the size and shape are identical with each query

---

<div class="post-metadata">

**Author:** ![cce66](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/c/8797f3/32.png) [@cce66](https://community.gladysassistant.com/u/cce66)\
**Post date:** [February 27, 2022, 3:59pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/8 "2022-02-27T15:59:18Z")

</div>

Hello @jerome  
To send a copy of the flow, go to the menu on the right / Export

 ![image](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/5/57c1c043193adf582b0994becf81ec2c8f253e0d.png)  
Select «&nbsp;copy to clipboard&nbsp;»  
 ![image](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/f/f84f188185c25977793880fdd19fcaa75b79c1c1.png)  
then come and paste it here using \</\>

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [February 27, 2022, 4:06pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/9 "2022-02-27T16:06:06Z")

</div>

````auto
< [
    {
        "id": "2aa6c7d0d6c34c69",
        "type": "http request",
        "z": "78945adb8706e343",
        "name": "Api Route Les Andelys",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://api.mapbox.com/directions/v5/mapbox/driving-traffic/01.XXXXXX,49.XXXXXXX;01.41859,49.24665?access_token=XXXXXXXXXXXXXXX",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 440,
        "y": 60,
        "wires": [
            [
                "902eeb605ce660fd",
                "d03b888bd26d03f2"
            ]
        ]
    },
    {
        "id": "6ea28c57b0da3a95",
        "type": "inject",
        "z": "78945adb8706e343",
        "name": "Every hour",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "3600",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 150,
        "y": 60,
        "wires": [
            [
                "2aa6c7d0d6c34c69"
            ]
        ]
    },
    {
        "id": "902eeb605ce660fd",
        "type": "debug",
        "z": "78945adb8706e343",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 590,
        "y": 120,
        "wires": []
    },
    {
        "id": "d03b888bd26d03f2",
        "type": "function",
        "z": "78945adb8706e343",
        "name": "Extract Duration",
        "func": "msg.payload=msg.payload.duration\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 740,
        "y": 60,
        "wires": [
            [
                "da8870c4b442b262",
                "9a4c9dd06fabf7df"
            ]
        ]
    },
    {
        "id": "da8870c4b442b262",
        "type": "mqtt out",
        "z": "78945adb8706e343",
        "name": "Route Les Andelys",
        "topic": "gladys/master/device/mqtt:les-andelys/feature/mqtt:les-andelys/state",
        "qos": "2",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "b43615cb2732e077",
        "x": 970,
        "y": 60,
        "wires": []
    },
    {
        "id": "9a4c9dd06fabf7df",
        "type": "debug",
        "z": "78945adb8706e343",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 870,
        "y": 120,
        "wires": []
    },
    {
        "id": "b43615cb2732e077",
        "type": "mqtt-broker",
        "name": "",
        "broker": "mqtt://localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]
>```
````

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [February 27, 2022, 4:38pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/10 "2022-02-27T16:38:11Z")

</div>

Here is the function to convert seconds to minutes. You create a node function and paste this in:

`var input = msg.payload;`  
`input = parseFloat(input);// convert to number`  
`var seconds = String(input % 60); // get the remainder representing the seconds`  
`var minutes = String(Math.floor(input / 60));// get the integer part of the quotient representing the minutes`  
`msg.payload = minutes + "mn " + seconds +"sec";`  
`return msg;`

Note that the line: `quotient representing the minutes`  
should be on the same line as  
`var minutes = ......`

To capture the duration, it’s the same principle as for retrieving the rflink device, you need to count the characters 😉

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [February 27, 2022, 4:42pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/11 "2022-02-27T16:42:31Z")

</div>

Jerome  
and the Rflink show is good?

For the flow, I still have the same error I had with [cce66](https://community.gladysassistant.com/u/cce66), there are quotes on each line, which are not the right ones, and which cause the copy-paste to crash. We need quotes like this " and not like inverted commas, I don’t know where it comes from. 🤔

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [February 27, 2022, 5:18pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/12 "2022-02-27T17:18:57Z")

</div>

Hello cce66,

How do you practice to get your flow in a window with a scrollbar? Because the quotes are good here.

 ![image](https://gladys-assistant-community.b-cdn.net/uploads/default/original/2X/e/eef0cbcf6e2048aeff45d0d648bcc34257e08167.png)

---

<div class="post-metadata">

**Author:** ![cce66](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/c/8797f3/32.png) [@cce66](https://community.gladysassistant.com/u/cce66)\
**Post date:** [February 28, 2022, 9:16pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/13 "2022-02-28T21:16:23Z")

</div>

```auto
type or paste code here

```

You first need to click on \</\> then this displays  
…  
type or paste code here  
…  
You then need to paste the code instead of «&nbsp;type or paste code here&nbsp;»

---

<div class="post-metadata">

**Author:** ![VonOx](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/vonox/32/10128_2.png) [@VonOx](https://community.gladysassistant.com/u/VonOx)\
**Post date:** [February 28, 2022, 9:30pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/14 "2022-02-28T21:30:05Z")

</div>

> [@\[TUTORIEL\] Formater son code dans un message: Aidez nous à vous aider ;)](https://community.gladysassistant.com/t/tutorial-format-your-code-in-a-message-help-us-help-you/3320):
>
> Salut, Souvent, lors de vos soucis ou questions, la citation de code est nécessaire. Cependant, le forum a tendance à “reformater” le code que vous postez =\> Changer quelques caractères et l’indentation sans prévenir … rage Les conséquences sont multiples: Le code n’est pas mis en évidence, On a du mal à lire, Si on veut copier / coller… Patatra, les caractères ne sont pas les bons et en matière de code informatique, ça a toute sont importance…face_vomitingExemple de script reformaté…

---

<div class="post-metadata">

**Author:** ![cce66](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/c/8797f3/32.png) [@cce66](https://community.gladysassistant.com/u/cce66)\
**Post date:** [March 1, 2022, 1:01am UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/15 "2022-03-01T01:01:38Z")

</div>

````auto
[{"id":"56b83b7bec2eff35","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"2aa6c7d0d6c34c69","type":"http request","z":"56b83b7bec2eff35","name":"Api Route Les Andelys","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://api.mapbox.com/directions/v5/mapbox/driving-traffic/42.586364, 2.370217;01.41859,49.24665?access_token=pk.eyJ1IjoianVtb3JlYXVqdSIsImEiOiJjbDA3Y2g3OGUwMzhmM29xMGR2OGJocHJwIn0.VYT4Y8Hd_of5tKXqluVjng","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":380,"y":220,"wires":[["902eeb605ce660fd","d35d5f8df3edeeea"]]},{"id":"6ea28c57b0da3a95","type":"inject","z":"56b83b7bec2eff35","name":"Every hour","props":[{"p":"payload"}],"repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":220,"wires":[["2aa6c7d0d6c34c69"]]},{"id":"902eeb605ce660fd","type":"debug","z":"56b83b7bec2eff35","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":280,"wires":[]},{"id":"da8870c4b442b262","type":"mqtt out","z":"56b83b7bec2eff35","name":"Route Les Andelys","topic":"gladys/master/device/mqtt:les-andelys/feature/mqtt:les-andelys/state","qos":"2","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"b43615cb2732e077","x":950,"y":220,"wires":[]},{"id":"9a4c9dd06fabf7df","type":"debug","z":"56b83b7bec2eff35","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":280,"wires":[]},{"id":"d35d5f8df3edeeea","type":"json","z":"56b83b7bec2eff35","name":"","property":"payload","action":"obj","pretty":false,"x":570,"y":220,"wires":[["4e6f68ad1abebbb4"]]},{"id":"4e6f68ad1abebbb4","type":"function","z":"56b83b7bec2eff35","name":"duration","func":"var duration=msg.payload.routes[0].duration;\n\n// transforme en nombre\nduration = parseFloat(duration);\n\n// convertit en hh:mn:ss\nvar temps=new Date();\ntemps.setTime(duration*1000);\nmsg.payload = (temps.getHours()-1) + \":\" + temps.getMinutes() + \":\" + temps.getSeconds();\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":220,"wires":[["9a4c9dd06fabf7df","da8870c4b442b262"]]},{"id":"b43615cb2732e077","type":"mqtt-broker","name":"","broker":"mqtt://localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]```

You import into a flow, you load your access token and you will get the output in hh:mm:ss :wink:
````

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [March 1, 2022, 7:56am UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/16 "2022-03-01T07:56:03Z")

</div>

> [@cce66](#):
>
> you first need to click on \</\> then it displays

Hello cce66,  
I got it, I understood, I didn’t see the \</\> in the top menu. Sometimes it doesn’t think fast 😩  
thank you again  
have a good day

---

<div class="post-metadata">

**Author:** ![cce66](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/c/8797f3/32.png) [@cce66](https://community.gladysassistant.com/u/cce66)\
**Post date:** [March 1, 2022, 9:10pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/17 "2022-03-01T21:10:45Z")

</div>

Don’t worry, I did the same thing! 😉 It’s good for the flow, did you manage to import it and does it work as you want? Actually, you had to go through a JSON and extract `msg.payload.routes[0].duration` and not `msg.payload.duration` because `duration` comes from the «&nbsp;route object&nbsp;» [https://docs.mapbox.com/api/navigation/directions/](https://docs.mapbox.com/api/navigation/directions/) I also got caught out on another API! 🤣

And to display the travel time in minutes, you need to replace

```javascript
msg.payload = (temps.getHours()-1) + ":" + temps.getMinutes() + ":" + temps.getSeconds();

```

with

```javascript
msg.payload = ((temps.getHours()-1) *60) + temps.getMinutes();

```

while waiting for the possibility to display strings in Gladys!

---

<div class="post-metadata">

**Author:** ![Psoy](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/psoy/32/9005_2.png) [@Psoy](https://community.gladysassistant.com/u/Psoy)\
**Post date:** [March 1, 2022, 9:36pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/18 "2022-03-01T21:36:34Z")

</div>

good evening cce66,  
Thanks, it’s good for the flow, I’m discovering this time calculation API and I’m going to delve into your time conversion method. For the «&nbsp;duration&nbsp;», indeed I had applied your method in the Xiaomi gateway, but I didn’t think of it this time and as Jerome told me that it was the same structure for all messages, I proposed the heavy method: extracting the string corresponding to the value of the «&nbsp;duration&nbsp;» by counting the characters of the string. But it’s still better with Json 😉

---

<div class="post-metadata">

**Author:** ![jerome](https://gladys-assistant-community.b-cdn.net/user_avatar/community.gladysassistant.com/jerome/32/7030_2.png) [@jerome](https://community.gladysassistant.com/u/jerome)\
**Post date:** [March 1, 2022, 9:40pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/19 "2022-03-01T21:40:09Z")

</div>

Hi guys.

Cool, thanks.

However, is it normal that I have -1:33:11; what does the -1 mean?

So in the dashboard it shows me -1

---

<div class="post-metadata">

**Author:** ![cce66](https://gladys-assistant-community.b-cdn.net/letter_avatar_proxy/v4/letter/c/8797f3/32.png) [@cce66](https://community.gladysassistant.com/u/cce66)\
**Post date:** [March 1, 2022, 9:43pm UTC](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962/20 "2022-03-01T21:43:28Z")

</div>

> [@cce66](#):
>
> temps.getHours()-1)

Because the journey is less than 1 hour (temps.getHours()-1) put (temps.getHours())) the -1 is used in case of exceeding 24 hours but it’s not useful in this case  
so just put

msg.payload = (temps.getHours() \*60) + temps.getMinutes();

[Next page](https://community.gladysassistant.com/t/node-red-api-calcul-du-temps-de-trajet/6962.md?page=2)
