Hi all.
I have just upgraded from OH2 to OH3 and I have a rule that’s not working any more. I know I’ve read somewhere that much date stuff has changed, just don’t know really how to solve it.
First in my rules file I have this:
import java.text.SimpleDateFormat
val SimpleDateFormat sdf = new SimpleDateFormat("HH:mm")
var String update_time
and then in a rule which is executed when a particular Item changes state I do
update_time = sdf.format(now.millis)
…and then I use this string for some notification stuff. I simply need a string containing the time when the Item was last changed. Any way to do that in OH3? Maybe even an easier way than this?
For the record, the error I get in my log now is:
'millis' is not a member of 'java.time.ZonedDateTime'
If you use previousState(true), you get an HistoricItem with state and timestamp properties that represent the Item when it had a state different than the current state. I’m not sure what is returned when the current state is NULL, but this may suit your purposes.
Assuming these updates are caused by updates to Items linked to a Channel, you can create a DateTime Item linked to the same Chanel with one of the time Profiles. This will update the Item with the timestamp of the update. Then user there expire functionality to expire the item if there hasn’t been any update for too long. UNDEF is probably a better choice than NULL.
NULL is intended to mean the Item has never been initialized. UNDEF is intended to mean “I don’t /can’t know what state the Item is in.” For example, when the MQTT binding loses connection to the broker, it will set all the items linked to Things under that broker thing to UNDEF.
In OH 1, NULL was actually called UNINITIALIZED. I was not happy with the change. Note that NULL is not null. NULL is an item state. null is what you are used to it meaning in programming.
I installed a DateTime Timestamp item to a „binary“ channel (contact); which works fine. The next DateTime Timestamp item to an „analog“ channel (temperature) failed saying there is time conversion problem
2021-01-04 11:00:46.708 [WARN ] [e.internal.SseItemStatesEventBuilder] - Exception while formatting value '2021-01-04T10:49:18.095052+0100' of item LS01BK_Temp_Timestamp with format '%.1f %unit%': Conversion = 'u'
I actually did - and it doesn’t work - but is it supposed to work? I saw that another answer that suggests creating separate items for last update but that seems a bit redundant if these values can be accessed through lastUpdate & persistence.