Unix Timestamp

How do I create an Unix Timestamp in a rule.


Does someone know if this is possible.

I need it to send an position update to owntracks.


Internally now() returns one of these:

So this works:

rule "Test Epoch"
    Time cron "0/10 * * * * ?"
    var epoch = now().getMillis()
    logInfo("epoch", Long::toString(epoch))

How to I create epoch from a DateTime Item.

I have tried with:

var dt = new DateTime(VolvoLocationTS.state)
var epoch = dt.getMillis()

but i receive this error

2016-02-13 22:51:00.364 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Test
java.lang.IllegalArgumentException: No instant converter found for type: org.openhab.core.library.types.DateTimeType



var epoch = (VolvoLocationTS.state as DateTimeType).calendar.timeInMillis

corrected – sorry!


Recive error

java.lang.ClassCastException: Cannot cast org.openhab.core.library.types.DateTimeType to org.openhab.core.library.types.DecimalType


Sorry, silly typo, corrected above.


That worked :+1:


1 Like

Hi all,

Im trying to use the rule code given by @guessed

var epoch = now().getMillis()

this works except it gives me the time in UTC, which i understand…
So i also tried looking at the joda-time.sorceforge link he also provided, to see if i could work out how to specify the tz i want it to return…

I tried var epoch = (now("Australia/Sydney").getMillis()) …but not unsurprisingly, it didnt work…

So i tried:
DateTimeZone dtZone = DateTimeZone.forID("Australia/Sydney"); var epoch = DateTime.now(DateTimeZone)
…that didnt work etiher…

Any help pls… ( clearly im hacking away…badly…)


It’s a little unclear what you’re looking to do. The now.getMillis data is defined to be expressed relative to UTC so it wouldn’t matter what TZ you’re operating in, it’ll return the same value.

now.getZone will return the name of the TZ that you’re Computer has been set to operate in. If that’s currently returning UTC, but you want it to return your local TZ for Sydney (eg AEDT), then you can adjust the setting in your operating system and it should come out correctly.

Can you explain more what you’re looking to do?

Here’s a test program showing the bit together (I’m in California):

rule "Test Timezone"
    Time cron "0/10 * * * * ?"
    var n = now
    var e = n.getMillis
    var z = n.getZone
    logInfo("test-timezone", String::format("%s %s %s", n.toString, z.toString, e.toString))


07:31:00.003 DEBUG o.o.m.r.i.e.ExecuteRuleJob[:53]- Executing scheduled rule 'Test Timezone'
07:31:00.011 INFO  o.o.model.script.test-timezone[:53]- 2016-02-23T07:31:00.005-08:00 America/Los_Angeles 1456241460005

Hi @guessed,

What im trying to do is get the unixtime/epoch for the current TZ.

This is then used to send to a anduino ( mysensors) with at RTC which requests the time on the hour, or startup and therefore copes with DST changeovers.

Thanks to your example above, now.getzone does return this for me as i need!

Thanks heaps again!


1 Like

Haven’t heard that expression in eons. It takes me back somewhat :wink:


What should be a correct code ?