Simple Transformation for ETA

I have a value which is the number of seconds remaining for something - I simply want to use a transformation to change it to display the date and time in the future from now (based on the system) + the number of seconds. I cant get my head around how to do this in a transformation - best I seem to get is a UTC date. can anyone provide a JS transformation code snippet to achieve this?

Like

@rossko57, I don’t think that is what Andrew is after. And if that were the case, the first thing is to find out if it’s a Number:Time. If so one can use the usually DateTime formatting to convert the number of seconds to days:hours:minutes:seconds format. For example, I have a Number:Time holding the amount of time my UPS can run given the current charge and load.

I’ve set the Pattern Format in the State Description metadata to

%1$tH:%1$tM:%1$tS

And it shows the value as

image

But I think OP wants something like now.plusSeconds(Items.state)

In that case the following should come close to working:

(function(i) {
    if (i == 'NULL') { return i; }
    if (i == '-') { return 'Undefined'; }
    var secs = parseInt(i);
    var time = new Date();
    time.setSeconds(time.getSeconds() + secs);
    return time....
})(input)

The correct call to generate the String will largely depend on your locale. See JavaScript Date Reference to pick out which is the best call and arguments to get the String in the format you desire.

Over all, this might be easier to do with a Rule and a DateTime Item. Then it’s just as simple as triggering a Rule when the seconds change and

MyDateTime.postUpdate(now.plusSeconds((MySeconds.state as Number).intValue))

Note, it will be slightly different if MySeconds is a Number:Time.

2 Likes

Thanks for the replies.
The item is a Number:Time but its a dummy item so can be whatever it needs to be. @rlkoshak, copying your function I have the same issue I ended up with trying to solve this before asking for help. I end up with UTC time. The current value I am trying to transform in the below screenshot is 1300 - which is the number of seconds I would like to add to now()

I have checked the TZ on the OH server - that is set correctly

OK I see that my local system was still on UTC - I have now set that to the correct TZ but this seems not to have changed the value - this is still showing in utc

Right - been working through this and have something but not really happy about the formatting

(function(i) {
        if (i == 'NULL') { return i; }
        if (i == '-') { return 'Undefined'; }
        var secs = parseInt(i);
        var time = new Date();
        
        time.setSeconds(time.getSeconds() + secs);
        ETAtime = time.toDateString() + " " + time.getHours().toString() + ":" + time.getMinutes().toString()
        return ETAtime
    })(input)

gives me this:

However I have no control over the date or time format - would prefer not to have year in there but seems I can only have numbers for days or months if I pull it apart some more - if anyone knows how to format dates - everything ive looked up regarding .toLocaleString with options seems to be ignored.

As I said, this would be easier with a rule and a DateTime Item

Then you have full control over how it appears using standard OH stuff.

Also notice in the link to the JavaScript docs above that there is a toLocaleString method.

Thanks for your patience @rlkoshak, Im afraid that OH3 makes me feel dumb evry time I try to do something. I was trying to acheive doing less rules in OH3 than I have in OH2 but if there are reasons such as control over formatting I am willing to conceed.

However, I tried what you suggested with a DateTime item and was getting cannot convert zoneddatetime to string errors - then changing the item to a string and randomly guessing what to put in to get something, I got the full date and time string with timezone by typing this:

PrusaMini_ETA.postUpdate(now.plusSeconds((PrusaMini_PrintTimeLeft.state as Number).intValue).toString())

But no control over the state transformation as a string ARGGGGGGH

feeling Dumb…

The Item should be a DateTime Item.

On the sitemap, you format it in the label as has always been the case. See Items | openHAB. In this approach nothing is different between OH 2 and OH 3.

If using MainUI in OH 3, take that same state formatting string (i.e.everything between the [ ] in the label and apply it to the Item using the State Description Item Metadata. Click on “add metadata” and select “State Desccription” an add the formatting string to the Pattern field.

In this case where you put the formatting string has changed, but what that formatting string needs to be remains the same.