There is a lot of confusion in that short little rule.
Today is an Item, not a DateTime. You can’t create a DateTime using an Item. You have to use either epoch or an ISO8601 formatted String.
Removing .millis doesn’t round epoch to the nearest second. .millis literally returns the epoch. By removing that you are not longer dealing with epoch.
To store the epoch value so you can use it in your visibility on the sitemap, Today_Epoch needs to be a Number Item. You can’t update a Number Item with a DateTimeType. Conversely, if it isn’t a Number Item than the Item can’t store epoch.
To put epoch for today at midnight (which is what you really need I think) into a Number Item Today_Epoch all you need is
val midnight_today = now.withTimeAtStartOfDay()
Yes, I do need a midnight time as one element, and would still need to convert the String from Partition1_AlarmMemory to EPOCH as well in order to use both in the visibility?
I am really new to coding at this level, so plenty of confusion.
I have changed rule to:
System started or
Item Testing_Switch received command ON or
Time is midnight
val midnight_today = now.withTimeAtStartOfDay()
I now get the following error:
18:59:31.566 [INFO ] [del.core.internal.ModelRepositoryImpl] - Refreshing model 'time.rules'
18:59:36.635 [ERROR] [untime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'SetDay': Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null
Any ideas why that would be? And what I need to do to convert the STRING from the item that already exists?
Yes, I have checked and Today_Epoch is a Number Item - with no dimension
Will try the code snip above as soon as I can get the error cleared from the Midnight_Today piece.
Partition1_AlarmMemory is a a channel from my Caddx binding. When this changes I log the Time to the Partition1_Last_Alarm String item (I think I was unclear in my reply above):
rule "Partition 1 Alarm Timestamp"
Item Partition1_AlarmMemory changed to ON or
Item Testing_Switch received command ON
postUpdate(Partition1_Last_Alarm, new DateTimeType())
logInfo("", "Partition 1 Alarm Timestamp " + Partition1_Last_Alarm.state )
I will be using the two items Today_Epoch and Partition1_AlarmMemory_Epoch in my visibilty condition.
EDIT: Tested the Partiton1_AlarmMemory_Epoch snippit - works great! Thanks
I have now realized that you cannot compare two items in visibility - which presents a small challenge, but will work though that. Will just use the Epoch value to get an age of the information I want/don’t want to display.
The alarm system I am using sets flags for the zones which generated the last alarm. These do not update until they are cleared by the next alarm event, so once they are a certain age they become irrelevant, so want to hide them from view.
So thinking along the lines of only displaying the relevant groups of zones if the last alarm was within the last 24 hours (maybe allow a variable time period: 1 day, 2 days, 7 days).
That way there is only an option to see recent Zones which caused alarms on a Partition (Area) that has had an alarm in X time.
Hope I have explained clearly enough.
Always open to better more streamlined approaches though. Hoping to post my project to the Caddx Binding section as a sample at some stage when I have completed the basics.
Think I’d use a dummy Item, maybe a switch. When update comes in, set switch on. Apply expire binding to switch off, if using fixed delay. If you want a variable delay, use a rules based timer. All this is just a variation of motion activated light.
Then sitemap visibility is simply about the switch on or off.
EDIT - following a different track
Alright, so to put it another way, you have captured alarm events in your static Item states. And you’d like them to go away after a time.
This is classic expire binding job, just apply expire to each Item.
Before we change something.
Would it make sense to copy the content of this thread into a new one called DateTimeConversion (openHAB 3.x)?
I would then rename this original thread to DateTimeConversion (openHAB 2.x).
This would not lose any information for users who do not immediately switch to 3.x.
A silly(?) question: how can I use the future Java code in OH2 today rather than Joda ?
on current OH 2 (but running Java 11) gives me
2020-10-26 17:54:31.530 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'sage für den Rest des Tages erwartete Strommenge voraus': 'withHour' is not a member of 'org.joda.time.DateTime'; line 309, column 9, length 15
And mind we’re in rules DSL so I think I cannot do ZonedDateTime.now() that’s giving errors.
Need to use ZonedDateTime::now. Same likely for the MIDNIGHT literal but no matter what I try it doesn’t work.