How to reset a DateTime item


I’m updating a DateTime item using:

postUpdate(ItemName, new DateTimeType())

The item definition is:
DateTime ItemName “Time is [%1$tH:%1$tM]”

Question is, how do I reset it (i.e. back to null?) so the UI will show “-:-” as the time. To clarify, I don’t want to set it to “00:00” as that’s a valid time.

Thanks for any help! :smile:

Best regards,

I have no idea if this is possible but I would try to pass either null or a new UnDefType and see what happens. Good luck!


Passing in null gives me this log entry.

2015-08-21 19:22:45.888 [WARN ] [] - given new state is NULL, couldn’t post update for ‘ItemName’

I’m not sure how to use UnDefType to be honest… I tried:

import org.openhab.core.types.UnDefType

postUpdate(ItemName, UnDefType.NULL)

But the Designer tells me:

  • Couldn’t resolve reference to JvmIdentifiableElement ‘UnDefType’.
  • Couldn’t resolve reference to JvmIdentifiableElement ‘NULL’.

Any ideas? :slight_smile:

I don’t believe it is possible to set the state of an item to undefined. I think that UnDefType is only used as the default state when openHAB starts, but you can’t set it back to this state manually.

(I might be wrong, but I think this is correct).


Those ideas were just stabs in the dark.

This would be crazy ugly but you could do something like the following:

In your Items:

String DisplayItemName 
DateTime ItemName "[%1tH:%1$tM]"

In your rules:

rule "Update DisplayItemName"
    Item ItemName updated
    DisplayItemName. postUpdate("Time is " + ItemName.state)"

Where you want to set it back to null just

DisplayItemName.postUpdate("Time is -:-")

On your sitemap you would put DisplayItemName instead of ItemName.

I think this will work, based on the info you have provided for your given usecase.

Edits: Fixed typos in code


Bindings are permitted to post item updates to UnDefType.NULL at any point, so would it make sense to allow rule code to do the same? Maybe if you ignore the Designer’s complaint about importing org.openhab.core.types.UnDefType it would work?

No idea if the Designer will have a whinge or not, but this correctly resets the original Item to UnDefType.NULL:

import org.openhab.core.library.items.*                                                                                             

rule "test rule 2"
    Time cron "0/5 * * * * ?"
    var nullValue = new DateTimeItem("trashTalking").state
    postUpdate(ZWaveZWLastUpdate, nullValue)

I’ve been tryping to reset a datetime in jsr223 javascript using the method above. Unfortunately, I get an error that I cannot postUpdate null to a datetime item. Does anyone have an idea how to do it in a javascript?
Here’s my script:

var nullValue = new DateTime().state;
postUpdate(‘RandomTimeItem’, nullValue);
and here’s the log output:
Error during evaluation of script ‘file:…js’: The argument ‘state’ must not be null.

Well, I now use a standard rule for this

    var nullValue = new DateTimeItem("soll leer sein").state
    postUpdate(Teichpumpe_Zeit_An, nullValue)

but it would still be nice if this could somehow be added to the jsr so I can reduce the number of depedent rules.

1 Like