Update Time Item in OH Rule

Hi,

I want to use a defined time item to trigger a rule. I know that this is possible with OH3. I am looking for a way to simply update this time item via Basic UI. I also found out that this is not possible.

My idea is to have 2 items in Basic UI for hour and minute. Every change will trigger a rule to update a time item in the backend and this timeitem will trigger another rule.

The question is now if I have 2 number items (hour, minute) how can I update a time item with their values?
I have read multiple threads but find no fitting solution.

Thanks

in JRuby it’s easy.

new_time = LocalTime.of(HourIttem.state.to_i, MinuteItem.state.to_i)
MyDateTimeItem.update(new_time.to_zoned_date_time)

In RulesDSL there’s a whole circus of type casting and dance required. There’s also an intermediate creation of ZonedDateTime.now.with(new_time).

Thanks for quick response but maybe there is some misunderstanding on my side. I have implemented one test rule:

rule "Wecker Uhrzeit"

when
	// Wenn sich Minute oder Stunde ändert
	Item virt_wecker_minute changed or
	Item virt_wecker_stunde changed
	
then
	val new_time = LocalTime.of(virt_wecker_stunde.state.to_i, virt_wecker_minute.state.to_i)
	virt_wecker_uhrzeit.update(new_time.to_zoned_date_time)
	
end

But then I get the following error in the log after executing:

No resource methods have been found for resource class javax.ws.rs.core.Response

Am I missing something?

to_i is a Ruby thing.

In RulesDSL, as I said, you need to do a circus of type casting. Try this

val hour = (virt_wecker_stunde.state as Number).intValue
val min = (virt_wecker_minute.state as Number).intValue
val new_time = LocalTime.of(hour, min)

val zdt = ZonedDateTime.now.with(new_time)
val datetime = new DateTimeType(zdt)
virt_wecker_uhrzeit.postUpdate(datetime)

You could rewrite the rule in jruby like this:

rule "Wecker Uhrzeit" do
  changed virt_wecker_minute, virt_wecker_stunde
  run do 
    new_time = LocalTime.of(virt_wecker_stunde.state.to_i, virt_wecker_minute.state.to_i)
    virt_wecker_uhrzeit.update(new_time.to_zoned_date_time)
  end
end

By the way, Since openHAB 4, BasicUI now has an input element that lets you enter a date/time directly into a DateTimeItem

Ok - seems to work. But one thing I missed: my target is an item from “datetime” where hour and minute is updated correctly corresponding my 2 items - thanks.

But now this item is using the date from today with the entered hour+minute so if I try to use this item to fire a rule for a daily thing this won’t work.

Is there an option to have an item just with the time instead of the whole date? I think if the date is still in it I need another process to update it to the next day…

Ok - nice hint. But I am on OH3 at the moment. The last major update from v2 to v3 took several weeks that all of my automatization is running again so I will do this at a later point. Maybe the update from v3 to v4 is easier but last time it was many work for my environment.

There are several options:

  • Do you want the date to be 1 day from today? This will make it trigger just once, until you change the time again (which will re-set the date to 1 day from that time)
  • Do you just want to trigger on the time (ignore the date) every day?

In my case Option 2 is sufficient. The date can be ignored and I want to fire the rule every day.

Show me your rule

Currently for setting the time item this is the rule:

rule "Wecker Pia Uhrzeit"

when
	// Wenn sich Minute oder Stunde ändert
	Item virt_wecker_pia_minute changed or
	Item virt_wecker_pia_stunde changed
	
then
	val hour = (virt_wecker_pia_stunde.state as Number).intValue
	val min = (virt_wecker_pia_minute.state as Number).intValue
	val new_time = LocalTime.of(hour, min)

	val zdt = ZonedDateTime.now.with(new_time)
	val datetime = new DateTimeType(zdt)
	virt_wecker_pia_uhrzeit	.postUpdate(datetime)
	
end

And then I’d like to use “virt_wecker_pia_uhrzeit” for firing another rule!

Paste the other rule here.

BTW you have one extra space before .postUpdate there.

If you haven’t got a rule, here’s an example:

rule "Execute the alarm" 
  Time is virt_wecker_pia_uhrzeit timeOnly
then
  // Do what you want to do here
end

Ah ok - the “timeOnly” is what I need - thanks. Will test this.

Thanks!!

It’s documented here https://v34.openhab.org/docs/configuration/rules-dsl.html#time-based-triggers

Thanks. This page is already open but I havn’t checked this last sentence!