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


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:

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

You can calculate them each in milliseconds by using something like

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.

1 Like

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.

Do you understand what that code is doing?

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

A example using a change node and JSONata

[{"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":[]}]
       , "[H]:[m]:[s].[f]"
1 Like

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
msg.payload = totalms - tms

If you haven't already done so then I recommend watching this playlist: Node-RED Essentials. 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.

1 Like

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