Set future time for DateTime item with rule (text or blockly)

Hi all

I’m in the need for setting a time in the future, I would like an item that allways holds tomorrows date and time 06:00.

So a basic idea of what I want to achive is as below, but I doesn’t work. When I build wit blockly it cannot parse date

Item
DateTime EvCarReady “När ska bilen vara laddad”

rule "EV ready"
when 
Time cron "0 0 6 ? * * *"
then
EvCarReady.postUpdate(now.plusdays(1))
end

Error code from blockly rule execution

06:45:12.529 [WARN ] [ernal.defaultscope.ScriptBusEventImpl] - State ‘2022-09-03T06:45:12.516582+02:00[Europe/Stockholm]’ cannot be parsed for item ‘EvCarReady’.

I hope anyone could give me a clue

Hi again I found out that this could work, but how do I do if I want to set nextdatee as tomorrow at 06:00 when a rule fires at for instance 14:00, in joda there was a function startoftoday or similar. But is there an equalent method for zonedatetime?

rule "EV ready"
when 
Time cron "0 0 6 ? * * *"
then
EvCarReady.postUpdate (new DateTimeType(now.plusDays(1)))

Sure. now.with(LocalTime.MIDNIGHT).plusHours(30)

Worked as a charm, I would never figured that one out myself, I will use that one but triggerd by another event now when I can have it set to 06:00 whenever the rule fires… I beleive that time is really difficult to handle. I have another project where I try to use a DateTime item in a ECMA 11 script but get stuck on errors. But this piece is one piece in the puzzle.

https://community.openhab.org/t/control-a-water-heater-and-ground-source-heat-pump-based-on-cheap-hours-of-spot-priced-electricity/136566/85

Unfortunately that will break on DST changeovers and tomorrow the time will be one hour off.

now.plusDays(1).withHours(6).withMinutes(0).withSeconds(0).withNanos(0)

should handle that transition better.

Note, look up the method names on ZonedDateTime, I’m going from memory here and it might be slightly different (e.g. withMinuteOfHour, withMinute, etc).

See Working with Date Times in JS Scripting (ECMAScript 11).

1 Like

I did find some text about it but it’s a little like a needle in a haystack, it seems like I foud the correct syntax for this, but that somethoings is missing when trying to update DateTime item. I thought .toString would have worked out.

command in .rules file

EvCarReady.postUpdate(now.plusDays(1).withHour(6).withMinute(0).withSecond(0).withNano(0).toString)

error

Cannot convert '2022-09-07T06:00+02:00[Europe/Stockholm]' to a state type which item 'EvCarReady' accepts: [DateTimeType, UnDefType].

The Item would like you to send it an ISO string

The easiest way to post a ZonedDateTime to a DateTime Item right now is to convert it to a LocalDateTime first which will strip off the troublesome time zone.

EvCarReady.postUpdate(now.plusDays(1).withHour(6).withMinute(0).withSecond(0).withNano(0).toLocalDateTime.toString())

Thanks alot for the moment that made my script work. Thanks alot.

Hello

I’m in the mood of developing this a litte more just for making an item that sets the hour the car should be finished charging, but is it possible to make withHour(6) to be witHour(ev_target_clock.state)?

EvCarReady.postUpdate(now.plusDays(1).withHour(6).withMinute(0).withSecond(0).withNano(0).toLocalDateTime.toString())

I’ve tried some different approaches without any luck, I’ve tried ev_target_clock.state as Number and tried to set Item state to a like this

val hour = (ev_target_clock.state as Number)

but it all endsup in error

Script execution of rule with UID 'ev-4' failed: An error occurred during the script execution: Could not invoke method: java.time.ZonedDateTime.withHour(int) on instance: 2022-11-07T09:52:06.815497+01:00[Europe/Stockholm] in ev

I appreciate any help.

/Marcus

So, what is that Item, what is its state?

This will be a clue as to what is needed

The .withHour() needs a single integer
If you actually had a Number, you can get an integer version with .intValue.

withHour() (and all the other with…() methods mentioned here) need an Integer as parameter, so please try

EvCarReady.postUpdate(now.plusDays(1).withHour((ev_target_clock.state as Number).intValue).withMinute(0).withSecond(0).withNano(0).toLocalDateTime.toString())

As usually all helpfull people helpsout, I’m just bad att transforming number having a hard time to get all kind Big Decimal, float, double and how to cats them in different ways.

Tank both of you.