Convert Timestamp

hello,

how can i convert a timestamp (e.g. 1520073863) to a readable format?

i read my item via JSON.

thx for help.

Can you post your item definition?

What do you want do do?

Or take a look at this:


DateTimeType is explained in the mid…

this was my item

Number RepStart  "Start [%s]"     {http="<[http://192.168.1.227:3344/printer/list:10000:JSONPATH($.data[0].start)]"}

this was the complete info of my JSON File

{"data":[{"active":true,"analysed":1,"done":0.13900380605659440,"job":"1_Z_Motor_Mount_Left.","jobid":9,"linesSend":294,"name":"ANET","ofLayer":250,"online":1,"pauseState":0,"paused":false,"printTime":22410.677829925866,"printedTimeComp":507.33782463806983,"slug":"ANET2","start":1520073863,"totalLines":211505}]}

to add the start-value works.
in this case 1520073863 is a timestamp for Saturday, 03-Mar-18 10:44:23 UTC

at first, i want in my BasicUI “Saturday, 03-Mar-18 10:44:23 UTC” and not
1520073863. so i think, i must confert the timestamp of my JSON file to a real date-time-value. but how?

https://docs.openhab.org/addons/bindings/ntp/readme.html
https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Number RepStart  "Start [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]"  {http=...

link

Number RepStart “Start [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]” {http=…
or
DateTime RepStart “Start [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]” {http=…

dont work :frowning:

2018-03-03 18:01:13.616 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value '1520073863' of item RepStart with format '%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM': {}

Create a TimeFormat.js file in your transform folder
with the following content

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

and your item

DateTime RepStart "[JS(TimeFormat.js):%s]" {http="<[http://192.168.1.227:3344/printer/list:10000:JSONPATH($.data[0].start)]"}

Good luck

i get only a “-”

possible that this was a problem?

2018-03-03 20:50:52.849 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - transformation throws exception [transformation=org.eclipse.smarthome.transform.javascript.internal.JavaScriptTransformationService@1a5428c, value=-]

org.eclipse.smarthome.core.transform.TransformationException: An error occurred while executing script.

	at org.eclipse.smarthome.transform.javascript.internal.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:84) [245:org.eclipse.smarthome.transform.javascript:0.10.0.b1]

	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.transform(ItemUIRegistryImpl.java:462) [155:org.eclipse.smarthome.ui:0.10.0.b1]

	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.getLabel(ItemUIRegistryImpl.java:386) [155:org.eclipse.smarthome.ui:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.render.AbstractWidgetRenderer.preprocessSnippet(AbstractWidgetRenderer.java:118) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.render.TextRenderer.renderWidget(TextRenderer.java:42) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.renderWidget(PageRenderer.java:172) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:137) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:158) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processPage(PageRenderer.java:100) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.smarthome.ui.basic.internal.servlet.WebAppServlet.service(WebAppServlet.java:179) [196:org.eclipse.smarthome.ui.basic:0.10.0.b1]

	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]

	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:584) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]

	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) [85:org.eclipse.jetty.security:9.3.22.v20171030]

	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:284) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]

	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) [88:org.eclipse.jetty.servlet:9.3.22.v20171030]

	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80) [191:org.ops4j.pax.web.pax-web-jetty:6.0.7]

	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.server.Server.handle(Server.java:534) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:333) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) [87:org.eclipse.jetty.server:9.3.22.v20171030]

	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:283) [79:org.eclipse.jetty.io:9.3.22.v20171030]

	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:108) [79:org.eclipse.jetty.io:9.3.22.v20171030]

	at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) [79:org.eclipse.jetty.io:9.3.22.v20171030]

	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) [90:org.eclipse.jetty.util:9.3.22.v20171030]

	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) [90:org.eclipse.jetty.util:9.3.22.v20171030]

	at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) [90:org.eclipse.jetty.util:9.3.22.v20171030]

	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) [90:org.eclipse.jetty.util:9.3.22.v20171030]

	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) [90:org.eclipse.jetty.util:9.3.22.v20171030]

	at java.lang.Thread.run(Thread.java:748) [?:?]

Caused by: javax.script.ScriptException: RangeError: Invalid Date in <eval> at line number 3

	at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150) ~[?:?]

	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249) ~[?:?]

	at org.eclipse.smarthome.transform.javascript.internal.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:82) ~[?:?]

	... 35 more

Caused by: jdk.nashorn.internal.runtime.ECMAException: RangeError: Invalid Date

	at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57) ~[?:?]

	at jdk.nashorn.internal.runtime.ECMAErrors.rangeError(ECMAErrors.java:266) ~[?:?]

	at jdk.nashorn.internal.runtime.ECMAErrors.rangeError(ECMAErrors.java:238) ~[?:?]

	at jdk.nashorn.internal.runtime.ECMAErrors.rangeError(ECMAErrors.java:225) ~[?:?]

	at jdk.nashorn.internal.objects.NativeDate.toISOStringImpl(NativeDate.java:1043) ~[?:?]

	at jdk.nashorn.internal.objects.NativeDate.toISOString(NativeDate.java:845) ~[?:?]

	at jdk.nashorn.internal.scripts.Script$Recompilation$1219826$13A$\^eval\_.L:1(<eval>:3) ~[?:?]

	at jdk.nashorn.internal.scripts.Script$1219825$\^eval\_.:program(<eval>:1) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) ~[?:?]

	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402) ~[?:?]

	at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150) ~[?:?]

	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249) ~[?:?]

	at org.eclipse.smarthome.transform.javascript.internal.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:82) ~[?:?]

	... 35 more

I wrote that JS function on top of my head
Difficult to debug in openHAB
Try:

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

The javascript transform only passes strings so parse i into an integer

I just tested this on my system and I get:
image

thx, works with a item as string

is it possible to calculate with this?

e.g.
item Start = 1520099716
item Duration = 12345
–> item End = Start + Duration (as readable Time)

thx for help

regard

You will need 4 items:

String RepStart {JSON} // your item definition
String RepEnd {JSON} // your item definition
Number RepStartNumber "[JS(TimeFormat.js):%s]"
Number RepEndNumber "[JS(TimeFormat.js):%s]"

and a rule:

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

Good luck

thx, will try this. and my last question (sorry).
can i format the output?
e.g.
from 2018-03-03T17:55:16.000Z
to
Sun, 03/03/2018 17:55:16
?

really my last question :slight_smile:

have a problem

endTime isnt a integer. example: 1077.9310463460163

can i convert this to 1077 or 1078?

regard

Try that instead:

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
    RepStartNumber.postUpdate(startTime.toString)
    RepEndNumber.postUpdate(endTime.toString)
end

thx

i get this here

Rule 'rep update': Could not cast 1520184351 to java.lang.Number; line 44, column 21, length 24

this are from my items:

2018-03-04 18:32:03.300 [vent.ItemStateChangedEvent] - RepStart changed from NULL to 1520184351
RepEnd changed from 706.4367411783377 to 717.2421195238306

You didn’t eliminate the typo :slight_smile: Please consider to edit the posting, this is more helpful, when someone finds this thread and tries the code without reading all postings :wink:

Got it!!!
Didn’t think of going back to change the original post
Thanks