Convert from DateTime item to Number

Hello,

I need the sunrise- and sunsettime of the astro binding in a number variable with the format “minutes of a day”. For example sunrise is at 7:30 the content of my number variable has to be: 450.

My question is now, is there any possibility to convert, for example the hours an minutes of a DateTime item into Number variables in a rule file?

Pseudo-Code of what i want to do:

var Number hours = MyDateTimeItem.gethours
var Number minutes = MyDateTimeItem.getminutes

Result:

content of MyDateTimeItem: 18:30 Uhr
content of hours: 18
content of minutes: 30

I have not tested this, but maybe you could do something like the following:

var Number minutesSinceMidnight = ( MyDateTimeItem.calendar.millis / (60 * 1000) )

Unfortunately it does not work, i get the following error:

2017-02-09 13:30:37.239 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'testregel': The name '<XFeatureCallImplCustom>.calendar' cannot be resolved to an item or type.

Do i have to include some special file?
I only have included:

import org.openhab.core.library.types.*
import org.joda.time.DateTime

I’ve got it working with this

var DateTime	sonnenaufgang 			= new DateTime((I_BR_AS_SO_aufgang.state as DateTimeType).calendar.timeInMillis)
var DateTime	sonnenuntergang 		= new DateTime((I_BR_AS_SO_untergang.state as DateTimeType).calendar.timeInMillis)
var Integer		sonnenaufgang_min		= sonnenaufgang.getMinuteOfDay
var Integer		sonnenuntergang_min 	= sonnenuntergang.getMinuteOfDay

regards
Sven

P.S. It is working without includes

1 Like

Sorry, its not working…

I get this error:

2017-02-09 13:59:44.058 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘testregel’
java.lang.NullPointerException: null
at org.eclipse.xtext.common.types.util.JavaReflectAccess.getRawType(JavaReflectAccess.java:107) ~[na:na]
at org.eclipse.xtext.common.types.util.JavaReflectAccess.getConstructor(JavaReflectAccess.java:90) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateConstructorCall(XbaseInterpreter.java:511) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateVariableDeclaration(XbaseInterpreter.java:601) ~[na:na]
at sun.reflect.GeneratedMethodAccessor873.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(XbaseInterpreter.java:321) ~[na:na]
at sun.reflect.GeneratedMethodAccessor844.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:204) ~[na:na]
at org.openhab.model.script.internal.engine.ScriptImpl.execute(ScriptImpl.java:59) ~[na:na]
at org.openhab.core.scriptengine.ScriptExecutionThread.run(ScriptExecutionThread.java:44) ~[na:na]

OK, now it WORKS!!

BUT: @der-brumm-baer i have to include:

import org.joda.time.*

Thank you for your help guys!! :+1:

So you are on 1.8.3 or 2.x ?

Im on 1.8.3, sorry i have forgotten to tell.