Convert Epoch into Date Time String

HI,
I am running openhab 3.3.0 and i need to convert epoch to a string with readable DateTime without using a rule. It is possible to do with trasformation direct into Item definition?

Thanks

Where is this epoch value coming from?

Hi,
it come from zigbee2mqtt, looking at docs there is also a way to send datetime in ISO format, but I prefer to have datetime in Italian format, so converting epoch might be best way

Thanks

Since that’s an option, it would be by far easiest to receive it in ISO format and put it into a DateTime Item and then you can use the State Description Pattern to format it any way you want.

Hi @rlkoshak,
i found a way to convert with Javascript trasformation:

(function(i) {
   var d = new Date(parseInt(i));
   return d.toString();
})

I will give a try also passing ISO format to Item and use State Description Pattern. I am here to learn new way to do things :slightly_smiling_face:

Many Thanks

If you are on 3.4, you’d be wise to use the SCRIPT transform instead. The JavaScript Transformation goes away in OH 4.

@rlkoshak
I am on Openhab 3.3.0, there is a way to use SCRIPT transform too? So I can also start to play with this

Thanks

No, it wasn’t introduced until 3.4 but it replaces the JavaScript transformation with a way to use any of the rules languages as a transformation.

is there just some kind of documentation for SCRIPT transform?
I had in plan to update to 3.4.0, but as soon 4.0.0 was annunced, I am thinking to wait 4.0.0 and skip 3.4.0.

All I’ll say to that is that the longer you wait, the more work it will be to do the upgrade. If you moved to 3.4 now and 4.0 in June/July you’ll likely have less work overall than jumping from 3.3 to 4.0. And even if it is the same or more work, you’ll have spread it across two upgrade sessions instead of needing to do it all at once.

Ok,
now it all clear to me. I just wait a couple of month, OpenHab is my Thermostat and control my heater, so I cannot make lot of outage during winter. So during early spring I will move to 3.4.1, I use OpenHab into docker container so upgrade should be quick, and then before next winter I have all time to move to 4.0.0

Thanks for your suggestion.

HI @rlkoshak
just one quick question, you mentioned that Javascript trasformation will be removed from 4.0.0.
I also use XSLT Transformation and Regex Trasformation, they will be removed too from 4.0.0?

Thanks

No, just Javascript.

For background, the Javascript transformation uses Nashorn JavaScript which comes with Java 8 and Java 11. However, as of Java 14 it no longer comes with Java. There is a new add-on to install it but it doesn’t make sense to keep this really old version of JavaScript around as the only way to do a scripted transformation.

Because there are plans to make all the rules languages separately installable add-ons, it didn’t really make sense to just code up a specific and separate transformation add-on for each. So a universal SCRIPT transformation was developed which lets you use any rules language installed instead of a specific language.

HI @rlkoshak
I did some test and I just updated to 3.4.1, so I am trying SCRIPT transform

I have this Item:

String ZIGBEE_LASTSEEN_BUTTON_SCRIVANIAMANSARDA "Scrivania Mansarda Last Seen [SCRIPT(js:LastSeenFromEpoch.script):%s]" <signalz> { channel = "mqtt:topic:zigbee2mqtt:xiaomi:button:Xiaomi_ScrivaniaMansarda:LastSeen" }

That return LastSeen Epoch of this sensor, and then I have LastSeenFromEpoch.script:

(function (data) {
    var d = new Date(data);
    var start = Date.now();
    var difference = start - d;
    var seconds = Math.floor(difference / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    if (hours > 0) {
        return hours.toString() + ' ore fa';
    } else if (minutes > 0) {
        return minutes.toString() + ' minuti fa';
    } else {
        return seconds.toString() + ' secondi fa';
    } 
})(input)

This return NaN Seconds. I did some test and seem issue is in this code:

var d = new Date(data);

I test this Javascript on a Online tester: JavaScript Tester online
and my code work fine:

Any Ideas on why this do not work on Openhab?

Thanks

The only thing I can think of which might be causing a problem is that input is a String, not a number. input is always a String no matter what the type of the Item is.

1 Like

Hi @rlkoshak
as usual thanks for you precious help, you are right, I need to convert Input into a number.
This Work

var d = new Date(Number(data));