hi!
I’m trying to set up the alarm clock item from the openHAB Android App on my phone. As soon as i set my alarm clock the Number Item:
Number OnePlus7_AlarmClock
gets set. For tomorrow morning for example OnePlus7_AlarmClock.state is 1605072300000.
To use this proberly I want to convert this to a DateTime Type Object “OnePlus7_AlarmClock_time”. I read this: DateTime Conversion
but none of the solutions worked for me.
My alarmclock.rules looks like this at the moment:
rule "AlarmClock: Change Epoch to DateTime"
when
Item OnePlus7_AlarmClock changed
then
logInfo("OnePlus7_AlarmClock changed to","Millis: "+ (OnePlus7_AlarmClock.state as Number))
val DateTimeType OnePlus7_AlarmClock_time = new DateTimeType(new DateTime(OnePlus7_AlarmClock.state))
end
…which leads to the error:
Rule 'AlarmClock: Change Epoch to DateTime': Could not cast 1605072600000 to java.lang.String; line 6, column 79, length 35
I also tried:
val DateTimeType OnePlus7_AlarmClock_time = new DateTimeType(new DateTime(OnePlus7_AlarmClock).toString)
…like suggested in #2 in the Tutorial i linked, but this gives me:
Rule 'AlarmClock: Change Epoch to DateTime': No instant converter found for type: org.eclipse.smarthome.core.library.items.NumberItem
There are some issues with the rule.
First: There is a chance that OnePlus7_AlarmClock.state is not of type Number, so the logInfo would fail with a NullPointerException. An easy way to avoit that would be to stop the rule if the state is not of type Number.
Second: all loggers need two strings as arguments, but the first string is the logger name, not part of the logger message.
Third: as all parts have to be String, a + (Item.state as Number) is not appropriate. The easiest way to printout a value, is to use string substitution.
Fourth: DateTimeType needs a string as parameter, and DateTime(OnePlus7_AlarmClock.state) is not a string.
rule "AlarmClock: Change Epoch to DateTime"
when
Item OnePlus7_AlarmClock changed
then
if(!(OnePlus7_AlarmClock.state instanceof Number))
return;
logInfo("alarmclock","OnePlus7_AlarmClock changed to Millis: {}", OnePlus7_AlarmClock.state)
val DateTimeType OnePlus7_AlarmClock_time = new DateTimeType(new DateTime(OnePlus7_AlarmClock.state).toString)
end
However, the rule will do nothing as OnePlus7_AlarmClock_time is not used and is destroyed right after it’s created.
It seems my topic isn’t in the beginners section for nothing This is my whole rule…
It is also not of type Number, if simply there is no alarm clock set. So I need to catch this case somehow.
the logger is only now for debugging, so i could see the data and guess the type. I’ll delete it, as soon as it is working.
see 2
I’ll try that - EDIT: This leads to following Error:
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'AlarmClock: Change Epoch to DateTime': No instant converter found for type: org.eclipse.smarthome.core.library.types.DecimalType
…at which point is this a DecimalType even?
how do I apply OnePlus7_AlarmClock_time to the actual Item OnePlus7_AlarmClock_time?
Thank you very much for your reply!
*EDIT: My latest rule version, which is still not working:
rule "AlarmClock: Change Epoch to DateTime"
when
Item OnePlus7_AlarmClock changed
then
if(!(OnePlus7_AlarmClock.state instanceof Number))
return;
logInfo("alarmclock","OnePlus7_AlarmClock changed to Millis: {}", OnePlus7_AlarmClock.state)
val DateTimeType alarmclocktime = new DateTimeType(new DateTime((OnePlus7_AlarmClock.state).toString))
OnePlus7_AlarmClock_time.postUpdate(alarmclocktime)
end