# Kodi HTTP request media player extract time and convert to time left

**URL:** https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502
**Category:** General
**Created:** [6 February 2021 16:39 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502 "2021-02-06T16:39:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![notownblues](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/notownblues/32/67810_2.png) [@notownblues](https://discourse.nodered.org/u/notownblues)
#### Post date: [6 February 2021 16:39 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/1 "2021-02-06T16:39:24Z")

</div>

Hi,

I'd like to extract the current time and total time on my media player (Kodi) from HTTP request and convert it into time left:

 ![Screenshot 2021-02-06 at 15.55.42](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b44e5bd86a2c1ffecffd8b37face3c6c0f060570.png)

Does anyone know how to do this?

I've been looking around for a few hours but I'm really struggling.

I like to get it as a final value in the payload like "1 hour 40 minutes" and ignore the hours if 0 so I can then ask Alexa "how long left on the movie" and she would read out the payload value.

Many thanks

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 February 2021 16:57 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/2 "2021-02-06T16:57:39Z")

</div>

You can calculate them each in milliseconds by using something like

```auto
let t = msg.payload.result.time
let tms = ((t.hours*60 + t.minutes)*60 + t.seconds)*1000 + t.milliseconds

```

and similarly with totaltime, and subtract them to give the time left in milliseconds.

---

<div class="post-metadata">

### Author: ![notownblues](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/notownblues/32/67810_2.png) [@notownblues](https://discourse.nodered.org/u/notownblues)
#### Post date: [6 February 2021 17:15 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/3 "2021-02-06T17:15:12Z")

</div>

That's great thank you so much. What do I have to put just before and after your code? I've tried to run that in the function node with a debug node but it's not returning anything? Sorry bit of a noob.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 February 2021 17:27 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/4 "2021-02-06T17:27:44Z")

</div>

Do you understand what that code is doing?

---

<div class="post-metadata">

### Author: ![notownblues](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/notownblues/32/67810_2.png) [@notownblues](https://discourse.nodered.org/u/notownblues)
#### Post date: [6 February 2021 17:31 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/5 "2021-02-06T17:31:57Z")

</div>

Yes I guess calculating the hours, minutes and milliseconds altogether? But how do you get this to show up in the payload once converted?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [6 February 2021 18:00 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/6 "2021-02-06T18:00:14Z")

</div>

A example using a change node and JSONata

```auto
[{"id":"54dc537.d5a342c","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.results.\t$fromMillis(\t (totaltime.minutes-time.minutes)*60000+\t (totaltime.hours-time.hours)*3600000+\t (totaltime.seconds-time.seconds)*1000+\t (totaltime.milliseconds-time.milliseconds)\t, \"[H]:[m]:[s].[f]\"\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":700,"wires":[["fc6d8e15.464738"]]},{"id":"b5280679.934c8","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"results\":{\"time\":{\"hours\":0,\"minutes\":10,\"seconds\":36,\"milliseconds\":123},\"totaltime\":{\"hours\":0,\"minutes\":90,\"seconds\":26,\"milliseconds\":67}}}","payloadType":"json","x":140,"y":680,"wires":[["54dc537.d5a342c"]]},{"id":"fc6d8e15.464738","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":700,"wires":[]}]

```

```auto
payload.results.
$fromMillis(
           (totaltime.minutes-time.minutes)*60000+
           (totaltime.hours-time.hours)*3600000+
           (totaltime.seconds-time.seconds)*1000+
           (totaltime.milliseconds-time.milliseconds)
       , "[H]:[m]:[s].[f]"
)

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 February 2021 21:09 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/7 "2021-02-06T21:09:18Z")

</div>

> [@notownblues](#):
>
> how do you get this to show up in the payload once converted

To send the calculated value `tms` in msg.payload use  
`msg.payload = tms`  
`return msg`

Check that is calculating it right for the time value and if it is then add after the lines in the previous post the similar calculation for the other value  
`let total = ....`  
`let totalms = ...`  
where you should be able to fill in the blanks, then to return the difference in msg.payload, instead of  
`msg.payload = tms`  
use  
`msg.payload = totalms - tms`

If you haven't already done so then I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Also have a read through the docs page [Working with Messages](https://nodered.org/docs/user-guide/messages).

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [7 April 2021 21:10 UTC](https://discourse.nodered.org/t/kodi-http-request-media-player-extract-time-and-convert-to-time-left/40502/8 "2021-04-07T21:10:05Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
