[SOLVED] Converting minutes into hour

The “type” column is showing “MAP file”, instead of “JS file” or “Javascript file”. Could you have a hidden extension giving it the wrong file name?

2 Likes

Good spot - indeed there is incorrect file extension. I will amend configs it as soon I get back home later this evening.

This javascript from @vzorglub works well, problem solved many thanks.

Evening All,

I have recently upgraded to OH 2.4. Looking in the log file I can see many warnings related to my javascript transform function converting minutes into hours/days.

2019-07-02 23:31:46.068 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '85.0' on item 'MyDevice4_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '85.0' to format '%d'
2019-07-02 23:31:46.079 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '240.0' on item 'MyDevice1_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '240.0' to format '%d'
2019-07-02 23:31:46.090 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '0' on item 'MyDevice5_Time_Active' with pattern 'JS(minstohours.js):%d': Cannot format state '0' to format '%d'
2019-07-02 23:31:46.093 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '85.0' on item 'MyDevice2_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '85.0' to format '%d'
2019-07-02 23:31:46.096 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '11.0' on item 'MyDevice5_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '11.0' to format '%d'
2019-07-02 23:31:46.106 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '240.0' on item 'MyDevice1_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '240.0' to format '%d'
2019-07-02 23:31:46.153 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '0' on item 'MyDevice5_Time_Active' with pattern 'JS(minstohours.js):%d': Cannot format state '0' to format '%d'
2019-07-02 23:31:46.170 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '11.0' on item 'MyDevice5_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '11.0' to format '%d'
2019-07-02 23:31:46.200 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '240.0' on item 'MyDevice1_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '240.0' to format '%d'
2019-07-02 23:31:46.209 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '0' on item 'MyDevice5_Time_Active' with pattern 'JS(minstohours.js):%d': Cannot format state '0' to format '%d'
2019-07-02 23:31:46.213 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state '11.0' on item 'MyDevice5_Time_Day' with pattern 'JS(minstohours.js):%d': Cannot format state '11.0' to format '%d'

I can see that things has changed in new OH version, so was expecting some teething problems at the start. Would you be able to tell me if I need to changing anything on my script to get it working as I can`t figured that out yet.

The output of a JS transform is always a string. I don’t know why it used to work with format %d and now does not, but try %s instead.

@rossko57 changed output to string and working again. No sure either why it used to work with format %d :+1:

I’ve only just realised that your JS returns something like "2 hours 10 minutes", which certainly is a string and won’t format nicely using %d for decimal number.

I was worrying it was some regression bug, but in fact it’s an enhancement to report instead of failing silently. Luckily it just passes the whole string along despite failure, I guess.

For everyone who just copied the code and is now wondering why it doesnt work: Check if JS Transformation is installed.^^

Then the code works flawlessly (Thanks @vzorglub!)

1 Like

Hi , this minstohours.js work perfect thanks ! , but i want to know if whe can cast the result in a rule ?

for exemple my Item is :

Number StartTimeRuntimeMinutesHuman “Alarm cuisiniere StartTime Minutes Human[JS(minstohours.js):%s]” and work perfect in the sitemap.

There is a way to call the result of : minstohours.js directly in the rule ?

Actually this is what i find to say the minute past…
say(“Cuisiniere Allumee depuis”+ ((StartTimeRuntimeMinutesHuman.state as DecimalType).intValue ))
this work for Minutes but not for Hours …or i cast 61 minutes…not 1hour and 1 minutes like in the sitemap

thanks for your help :+1:

Yes, you can use transformation services in rules too.
It’s in the docs -


with examples
var fahrenheit = transform("JS", "convert-C-to-F.js", temperature)
1 Like

thanks rossko57 and thanks for the link ! :- :+1:

Thanks!
That helps a lot.

Hello all,
I 've the same problem, using Openhab3
In took the javascript code and put it into the transform directory.
If I used the wrong datatype, I get an error in the log file. So, it’s working.
But: Nothing happens in the item view, there I have still the same display than before ( minutes )
And the widget also shows no difference.
I do not know, how to access the return key of the javascript.
I searched in the documentation, but I didn’t find a solution…

How did you associate the script with your Item? Where are you looking at a display?

What I did:
In the Openhab item from the sys info binding put the javascript in the profile:


This seems to work, an error in the javascript generates a log file entry.
I didn’t change the Item as I expected, the output value of the channel is changing:

And the widget looks like this:

component: oh-label-card
config:
  actionAnalyzerItems:
    - OpenhabSysteminfo_SystemUptime
  item: OpenhabSysteminfo_SystemUptime
  title: Uptime
  icon: oh:switch
  footer: =items.OpenhabSysteminfo_IPAddress.state

Applying a profile to a link alters the data from a channel before it gets updated to the Item.
The output of your transform is a string like “2 hours 10 minutes”. You can’t update a Number type Item with a string like that.

1 Like

Thanks a lot. Got more understanding, how openhab works. :grinning:
But, still not solved the problem. I’ll try to make a number2number javascript conversion by dividing it by 1440. Then I should get days ( hopefully )

We don’t know what “the problem” is. What are trying to achieve here, just a pretty transformation for display? If it is that, leave your Item in minutes and apply whatever transformation you want to the Item’s state presentation pattern.

That is exactly, what I want to archive. Just transforming the minutes in the Widget Display to a better readable output like days/hours/minutes or similar. I searched the documentation but I didn’t find a way to do this.

Alright, stop fiddling with profiles if you don’t want to change the Items state.
Apply the transformation to your Item’s state presentation pattern metadata. (Most of the docs are still OH2 and describe the same thing using in the Item’s label.)

This thread is relevant for that -