Convert Timestamp

@vzorglub

my computer was corrupt, sorry for my late answer.

i try your latest code, and now i get only this:

1520846028

JSON:

{"data":[{"active":true,"analysed":1,"done":16.833320500932302,"job":"E3DV6_Chain_Closed_Full_V4.","jobid":22,"linesSend":50285,"name":"ANET","ofLayer":68,"online":1,"pauseState":0,"paused":false,"printTime":18913.532202940485,"printedTimeComp":4334.7602734694547,"slug":"ANET2","start":1520846028,"totalLines":298723}]}

LOG

2018-03-12 11:47:00.113 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'rep update': Could not cast 1520846028 to org.eclipse.smarthome.core.library.types.DecimalType; line 44, column 34, length 29

With your old function:

(function(i) {
var date = new Date(parseInt(i) * 1000);
return date.toISOString();
})(input)

i get
2018-03-12T09:13:48.000Z

It seems the the JS transform doesn’t like parsing a string integer into a float so lets try to change it into a float in the rule

try:

rule "rep update"
when
    Item RepStart received update or
    Item RepEnd received update
then
    var startTime = RepStart.state as Number
    var endTime = RepEnd.state as Number
    endTime = startTime + endTime
    startTime = startTime + 0.1
    RepStartNumber.postUpdate(startTime.toString)
    RepEndNumber.postUpdate(endTime.toString)
end

I have done a bit of testing.
My previous javascript was untested. I was full of bugs, surprise surprise.

Try this: (Replace the whole script)

AND remove the line I added in the rule previously although it shouldn’t matter.

(function(i) {
    var value = parseFloat(i)
    var date = new Date(parseInt(value) * 1000);
    var weekDay = "";
    var day = "";
    var month = "";
    var year = "";
    var hours = "";
    var minutes = "";
    var seconds = "";

    //Sun, 03/03/2018 17:55:16
    switch (date.getDay()) {
        case 0:
            weekDay = "Sun";
            break;
        case 1:
            weekDay = "Mon";
            break;
        case 2:
            weekDay = "Tue";
            break;
        case 3:
            weekDay = "Wed";
            break;
        case 4:
            weekDay = "Thu";
            break;
        case 5:
            weekDay = "Fri";
            break;
        case 6:
            weekDay = "Sat";
           break;
    }
    if (date.getDate() < 10) {
        day = '0' + date.getDate().toString();
    } else {
        day = date.getDate().toString();
    }
    if (date.getMonth() < 10) {
        month = '0' + date.getMonth().toString();
    } else {
        month = date.getMonth().toString();
    }
    year = (date.getFullYear() - 2000).toString();
    if (date.getHours() < 10) {
        hours = '0' + date.getHours().toString();
    } else {
        hours = date.getHours().toString();
    }
    if (date.getMinutes() < 10) {
        minutes = '0' + date.getMinutes().toString();
    } else {
        minutes = date.getMinutes().toString();
    }
    if (date.getSeconds() < 10) {
        seconds = '0' + date.getSeconds().toString();
    } else {
        seconds = date.getSeconds().toString();
    }
    var result = weekDay + ', ' + day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds;
    return result;
})(input)

1 Like

looks good

Mon, 12/02/18 10:13:48

thx :slight_smile:

Do you get the correct values for start time and end time?
Happy to help

99% are correct

i seems the value for the month isnt correct

12/02/18
but, today is 12/03/18

the rest works good, thx

The month index starts at 0!
months 0 to 11
Just add one to the getMonth()

    if ((date.getMonth() + 1) < 10) {
        month = '0' + (date.getMonth() + 1).toString();
    } else {
        month = (date.getMonth() + 1).toString();
    }
1 Like