[SOLVED] Showing date/time of button press fails

I am trying to make something work that I have seen various people do. I followed examples but I still cant get it to work.
I want to show the date and time of the last moment someone pressed my front door bell.
When my doorbell is pressed it sends an MQTT signal onto my network.

I have the bell defined in my items file as this:

DateTime Doorbell_update "Last pressed [%1$tm/%1$td %1$tH:%1$tM]" <doorbell> (GF_Corridor) {mqtt="<[mosquitto:hal/bel:state:default]"}

My rules file looks like this

import java.util.Random
rule "Last Updated"
when
  Item Doorbell_update received update
then
  Doorbell_update.postUpdate( new DateTimeType() )
end

yet it is not working. Though I understand the rules file is read upon craetion, just to be sure I restarted Openhab as well, but no prevail.
obviously somewhere I made a mistake or forgot something.
can anybody give me a hint?

I think this will loop.

Doorbell_update.postUpdate( new DateTimeType() ) will cause an new “received update” and then it will loop?

I think it is better to use a different item for the date
Look here something similar:

@HomeAutomation is right. As written you will enter an infinite loop. I don’t think the two Items on one line will be much help for you though.

If the MQTT message is getting set to something that can be parsed into a DateTime type (i.e. epoc or an ISO8601 formatted String) then you simply need to eliminate the rule.

I believe this is unlikely in which case you need to eliminate the MQTT binding from the DateTime Item and apply it to some other Item. Then your Rule becomes:

rule "Door bell pressed"
when
    Item Doorbell received update
then
    Doorbell_update.postUpdate( new DateTimeType() )
end

Even better, since someone ringing a doorbell is a command for something to happen, your mqtt should be <[mosquitto:hal/bel:command:default] and your rule should trigger on received command.

Thanks Rich, Home Automation, but I am still a bit at a loss here.
My mqtt= {mqtt="<[mosquitto:hal/bel:state:default]"}, which is as Rich suggests
I renamed my item in the items file from “Doorbell_update” to “Doorbell” and changed the datetime format to contact so now that is:
itemfile:
Contact Doorbell "Last pressed [%1$tm/%1$td %1$tH:%1$tM]" <doorbell> (GF_Corridor) {mqtt="<[mosquitto:hal/bel:state:default]"}

My rules file is:

    import java.util.Random
    import java.text.SimpleDateFormat
    rule "Last Updated"
    when
      Item Doorbell received update
    then
      Doorbell_update.postUpdate( new DateTimeType() )
    end

But now the moment I MQTT the CLOSED command the empty datetime bars change into “Err”

tried changing the received update line in the rule to
Item Doorbell received command
Same result
tried changing
Doorbell_update.postUpdate( new DateTimeType() )
to
Doorbell.postUpdate( new DateTimeType() )
Same result (Err)
Hmm in my openhab.log file I see:
Exception while formatting value 'CLOSED' of item Doorbell with format '%1$tm/%1$td %1$tH:%1$tM': java.util.IllegalFormatConversionException: m != java.lang.String

I am seeing in fact a few more errors that might be linked:

2017-05-25 11:51:16.628 [WARN ] [rm.AbstractFileTransformationService] - Could not transform '-' with the file 'cl.map' : Target value not found in map for '-'
2017-05-25 11:51:16.638 [WARN ] [rm.AbstractFileTransformationService] - Could not transform '-' with the file 'en.map' : Target value not found in map for '-'
2017-05-25 11:51:16.647 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value 'CLOSED' of item Doorbell with format '%1$tm/%1$td %1$tH:%1$tM': java.util.IllegalFormatConversionException: m != java.lang.String
2017-05-25 11:51:45.753 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'CurrentDate' for widget org.eclipse.smarthome.model.sitemap.Text
2017-05-25 11:51:45.755 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item 'CurrentDate' for widget org.eclipse.smarthome.model.sitemap.Text
2017-05-25 11:51:45.758 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text

When I make the mqtt command
{mqtt="<[mosquitto:hal/bel:command:default]"}
then just nothing happens anymore, no error no time

OK a bit of progress.
rather than a Contact, new defined my Item as a string and then I see the time and date appear.

However, as I feared in using ‘command’ rather than ‘state’ the update is now when the bell is pressed, but subsequently every time it is not pressed as well.
But if I go back to ‘state’ in my mqtt string I get an error again. something is not parsing right
So how can I make sure there is only an update on the ‘CLOSED’ command

Yes … if your Doorbell Item is a Contact type, then it is not sensible to try and format its OPEN or CLOSED state for display as a series of date & time elements. The stuff in [] in an Item definition is about how you want to display that items state.

Two Items:
Doorbell, as a contact type, state can be open or closed. You could use [%s] to display that state if you wanted, but you probably won’t need to.
Doorbell_update, as a DateTime type, here is where the %1$tm/%1$td (time as month, time as day) etc. stuff is useful and appropriate.

Sitemap:
Put both items on your sitemap while playing, but later you might only want the timestamp item

Rules:
You might find using a rule trigger like
Item Doorbell changed to CLOSED
does what you want. I’m guessing you don’t care when they stop pressing the bellpush.

Thanks rossko
I never had problem with displaying the doorbell as contact, so I know that was working :slight_smile:
changing rule to:
Item Doorbell changed to CLOSED
indeed only updates on CLOSED command,
but then when the mqtt command “OPEN” comes later, the last update time gets replaced by ‘Err’ again
hmm I would think that if the Conatct is OPEN, the rule should just not do anything, certainly not update with “Err”

Yes. You MUST fix your Items. Show us your two Item definitions if you don’t understand how.

Contact Doorbell_GF_Corridor "deurbel [MAP(cl.map):%s]"  <doorbell> (GF_Corridor) {mqtt="<[mosquitto:hal/bel:state:default]"}
String Doorbell "Last pressed [%1$tm/%1$td %1$tH:%1$tM]" <doorbell> (GF_Corridor) {mqtt="<[mosquitto:hal/bel:command:default]"}

Okeydoke, neither of those are called Doorbell_update so heaven knows what your rule does now.
I would start from

Contact Doorbell_GF_Corridor "deurbel [MAP(cl.map):%s]"  <doorbell> (GF_Corridor) {mqtt="<[mosquitto:hal/bel:state:default]"}

which is fine as you have it, a Contact type item llinked to an MQTT “device”

and

DateTime Doorbell_update "Last pressed [%1$tm/%1$td %1$tH:%1$tM]" <doorbell> (GF_Corridor) 

a timestamp item that isn’t linked to anything, and so will only be updated by rules.

Now have your rule look for changes to Doorbell_GF_Corridor and then update Doorbell_update
On your sitemap, display Doorbell_update

Thank you, but I think we have a misunderstanding. Just to clarify: my rule file currently is and recently was:

import java.util.Random
import java.text.SimpleDateFormat
rule "Last Updated"
when
  //Item Doorbell received command
  Item Doorbell changed to CLOSED
then
  Doorbell.postUpdate( new DateTimeType() )
end

So it is linked. it was one of the edits made a long the way implementing previous advices
The Contact doorbel item works 'Doorbell_update" is not of relevance, it is no longer in my rules and the contact does not need a rule,
Only the time stamp needs a rule and that is linked. “Doorbell” is the item name as it appears in the rules file as well
Doorbell_GF_Corridor does not need the rule, it just displays the state, it is just linked to an mqtt command.
my only issue is with the time update on CLOSED which works, but need to get rid of the “Err” when the OPEN command follows,
So, currently it is as you suggest, albeit I use “String” rather than “DateTime”. I used that at the beginning of my question, but that never worked so along the way I made that into “String”.
Anyway, the dtae gets displayed correctly, it is just “Err” I get when the "OPEN"command follows, I just need the rule not to do anything then.

The Item is called “Doorbell” and has that name in the rules file too

the Items I suggested will work with this rule

rule "Last Updated"
    when
      Item Doorbell_GF_Corridor changed to CLOSED
    then
      Doorbell_update.postUpdate( new DateTimeType() )
    end

The timestamp item should not be linked to anything. The timestamp item should not be used as its own rule trigger.

You can of course rename anything, with care to keep everything in order.

Thank you,
Works
after another edit: see below
my rules file is:

import java.util.Random
import java.text.SimpleDateFormat
rule "Last Updated"
when
  //Item Doorbell received command
  //Item Doorbell changed to CLOSED
 Item Doorbell_GF_Corridor changed to CLOSED
then
  Doorbell.postUpdate( new DateTimeType() )
end

and my items are:

Contact Doorbell_GF_Corridor "deurbel [MAP(cl.map):%s]"  <doorbell> (GF_Corridor) {mqtt="<[mosquitto:hal/bel:state:default]"}
DateTime Doorbell "Last pressed [%1$tm/%1$td %1$tH:%1$tM]" <doorbell> (GF_Corridor)

OK! Succes
the cl.map that I use translates OPEN/CLOSED to off/on
Removed the map now it works

now I just have to keep the one item that I do not want (Doorbell_GF_Corridor) from displaying (by removing the Group tag).
Thank you for your help

1 Like

And let me add, you didnt just help me with getting this to work, but You, Rikoshak and HomeAutomation, through your comments, also gave me a deeper insight in the functioning of Openhab and I appreciate that very much