[SOLVED] Sending datetime to an item

Hello, I’m trying to write a rule for saving a android alarm time to a item in datetime format to use in further rules. I have the right date and time but i cant seem to manage saving it to a DateTime item. I get following error:

2019-02-09 11:38:13.188 [INFO ] [smarthome.model.script.android alarm] - timestampString=2019-02-09T16:20:00.000+0100

2019-02-09 11:38:13.192 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Calculate android alarm Jimmy datetime': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

item:

Number alarmTimeJimmy        "Alarm tijd van android"    <time>
DateTime androidAlarmJimmy  "Android alarm Jimmy [%1$tl:%1$tM %tp]"   <time>

rule:

import java.text.SimpleDateFormat
import java.util.Date

rule "Calculate android alarm Jimmy datetime"
when
    Item alarmTimeJimmy changed
then
    val timestampEpoch = (alarmTimeJimmy.state as Number).longValue
    timestampEpoch=timestampEpoch*1000
    val SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    val String timestampString = sdf.format(new Date(timestampEpoch))
    logInfo("android alarm","timestampString="+timestampString)
    androidAlarmJimmy.sendCommand(sdf)
end

I’m quite new to programming rules so it might be something obvious to people with more experiance

See:

Thanks for the docs, i got it to work now. DateTime seems to be rather complex compared to Number’s

That is an understatement!!
Could you post the working code for other users, please?
Thanks

This is how i did it now since i allready had a dateTime string for the loginfo:

import java.text.SimpleDateFormat
import java.util.Date

rule "Calculate android alarm Jimmy datetime"
when
    Item alarmTimeJimmy changed
then
    var timestampEpoch = (alarmTimeJimmy.state as Number).longValue
    timestampEpoch=timestampEpoch*1000
    var SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    var String timestampString = sdf.format(new Date(timestampEpoch))
    logInfo("android alarm","Android alarm Jimmy changed to "+timestampString)
    androidAlarmJimmy.sendCommand(DateTimeType.valueOf(timestampString)) 
end
1 Like

Edit: Added loginfo.
I think, this should do the same (create a timestamp):

rule "Calculate android alarm Jimmy datetime"
when
    Item alarmTimeJimmy changed
then
    androidAlarmJimmy.postUpdate(new DateTimeType)
    logInfo("android alarm","Android alarm Jimmy changed to " + androidAlarmJimmy.state.format("%1$td.%1$tm.%1$ty %1$tH:%1$tM"))  // java string format see https://www.novixys.com/blog/java-string-format-examples/#31_Date_and_Time_Formatting
end

Hello, ran into an other problem i think is related. Using the above value i want to check if the alarmtime has been past and in wich timespan, but i think i’m compairing different types of DateTime

rule "On Jimmy's android alarm"
when
    Time cron "0 0/1 * 1/1 * ? *"
then
logInfo("android alarm","rule started")

    if(now.isBefore(androidAlarmJimmy.state as DateTimeType)){
        logInfo("android alarm", "before alarm time: leaving rule")
        return
    }
    // else{
    //      if(now.isAfter(now.withTimeAtStartOfDay.plusDays(1).minusHours(19)) && now.withTimeAtStartOfDay.plusDays(1).minusHours(18)){

    //      }
    // }
end

it seems “now.isBefore()” returnes a different type then the dateTime item. Can someone tell me wich type iit returnes so i can try and convert it to the right type to compare to the dateTime item. Or maybe someone knows a better way to accomplish this?

What i would like to do is determine if the alarm goes off between 5-6AM, between 1-2PM or between 9-10PM so i can take different actions in the rule.

For example if the alarm is triggered between 5-6AM i want the lights in the bedroom on my side to go on in night mode for 2 min and the coffymaker to turn on
Thanks

    i f(now.isBefore(new DateTime(androidAlarmJimmy.state.toString))) {