[HELP NEEDED] Converting Seconds Into Minutes

A little background first.

Like a lot of people I commute to work using the Train and by using the solution here. I can obtain the expected time the trains will depart from my location station. In addition the rule provides the time until the train is due to depart as a whole number; ie 22.

By using this rule I’m able to get an estimated time it will take me to drive to the station. The time however is in seconds. So makes it a little difficult to calculate which is the earliest train i can get. I thought I’d found a solution by using a small JS to map the value from seconds to minutes.

(function(i) {
    if (i == 'NULL') { return i; }
    if (i == '-') { return 'Undefined'; }
    minutes = Math.floor(i / 60);
return minutes;
})(input)

Although this works it doesn’t provide me with a whole number, I get a decimal value return; ie 19.0.

As my knowledge is limited I’m looking for some help. Open to any alternatives or suggestions.

Thanks,

Garry

Does that matter? You can control the format for display purposes in your UI

    var int minutes = Math.floor(i / 60);

Sadly that doesn’t work with JavaScript by the looks of it as I get an error.

Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:4:9 Expected ; but found minutes
        var int minutes = Math.floor(i / 60);

Here’s the item and error from the log.

Number Journey_Time "[JS(minutes.js):%.0f]" <time>
2020-01-07 19:37:16.211 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '977' on item 'Journey_Time' with pattern 'JS(minutes.js):%.0f': Cannot format state '977' to format '%.0f'

Yes it does matter as I can’t then use the data to make my calculation work, because the numbers are in two different formats.

Really? You have some calculation that gives you different results for 18 and 18.0?? Maybe we can help with that.

Although the UI has 18.0 displayed, the stored data in the item is still in seconds. So I’m therefore not able to complete the calculations I’d like to. My end game is to identify which train I’m likely to catch, by using the approximate time to the station and subtracting that from the time until the train arrives.

The time until the train arrives is provided in minutes, so either way I’ll need some form of calculation.

This fails because a label transform always returns a string, “18.0”
The % format cannot operate on the string “18.0” because it is not a number 18.0
You must use
"your text [JS(minutes.js):%s]"

Alright, so it must be dealt with in the transform itself.
It doesn’t seem like that should be a difficult job in javascript.
Perhaps the issue is that the javascript is in fact returning a number, and so some internal part of the transformation service is doing number->string without any particular care about integers.

return minutes.toString() ;

Just made the changes you suggested @rossko57. Now the sitemap is displaying the value in seconds, so no transformation. But in the openhab.log the output is showing in minutes, well sort of.

[INFO ] [arthome.model.script.Transport.rules] - It will take 19.90000000 minutes to get to the Station.

Then the transform has failed in some way and you’ll have to fix it. What have you tried? First step might be to be back out of the last change you made.

Be aware that if you are using a sitemap and have specified a label="text [format]" in your sitemap entry, that will override anything you might have specified in your Item definition.

This is the logWarn() output from one of your rules.
Your rules are currently a secret from us.
Do you need help with that?