Sonoff/Tasmota - DateTime item - How?

Dears,
I do have some Sonoff basics properly running on OH2 on Pi3.
I do use some of these connected to PIRs and when it would be possible to
get the timestamp for the last detection in OH2 this would be great.
JSON Transformation is installed as well for some sensors.

Now I realized that there is an item call “DateTime” for switches as documented like:
DateTime Light_Switches_Left_Button_State_Stamp “Left button stamp [%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS]” { plugwise="[lightswitches:left-button-state-stamp]" }

Can I somehow adopt this for my Sonoff/Tasmotas under MQTT, or is it a difficult miracle?

I do this to monitor some doors it the house:

Items:

Group:Contact:AND(CLOSED,OPEN) EntryDoors        "Entry Doors [%s]" <door>    (Doors)
Contact  House_FrontDoor                    "Front Door [%s]"               <frontdoor> (EntryDoors, testdoors)               { mqtt="<[mybroker:RFLink/House/FrontDoor:state:default], <[mybroker:House/FrontDoor:state:default]"}
DateTime House_FrontDoor_LastOpen           "Last Opened [%1$td/%1$tm - %1$tH:%1$tM:%1$tS]" <clock>

rules:

rule "Entry Doors Update"
when
    Member of EntryDoors changed
then
    Thread::sleep(100) // give persistence time to catch up
    if (previousState == NULL) return;
    val door = triggeringItem
    if (door.state == OPEN) {
        postUpdate(door.name + "_LastOpen", now.toString)
    } else {
        postUpdate(door.name + "_LastClosed", now.toString)
    }
end

Hi Vincent, Boom this is high sophisticated :slight_smile:

when I am trying to understand and see if I can adopt some of that for my purpose, I am getting the following questions:

Items:

DateTime House_FrontDoor_LastOpen “Last Opened [%1$td/%1$tm - %1$tH:%1$tM:%1$tS]”

Is it not only this line I would require to add to my items for reading a time stamp? But how the hell is this assigned to the relevant MQTT-Topic in my case TH03?
In that line you don´t mention the mqtt command, or does that line still belong to the line before starting with Contact…?

And when I would like to add that to my sitemap it is just House_FrontDoor_LastOpen what I display as text?

It is not. You have an item with an MQTT binding to receive your PIR data I assume
In the following I call this Item TH03

Create a rule triggered when TH03 changes

Then in the rule do TH03_LastUpdate.postUpdate(now.toString)

DateTime TH03_LastUpdate           "Last Update [%1$td/%1$tm - %1$tH:%1$tM:%1$tS]" <clock>
rule "TH03 Triggered"
when
    TH03 changed to ON //or whatever you get then the PIR is triggred
then
    TH03_LastUpdate.postUpdate(now.toString)
end

Yes:

Text item=TH03_LastUpdate

I’m using below combination for recording last update/change time of (any) item. Below is an example of recording time of last activity detected by PIR sensor.

Items file:

Number Bathroom_Switch "Bathroom Switch [%d]" <text> (gEE205,gPomLazienkaPietro) {mqtt="<[wrt:easy/205/PIR/Switch:state:default]"}

DateTime easySwitchTime "Bathroom Last [%1$tm-%1$td, %1$tH:%1$tM:%1$tS]" <clock> (gEE205,gPomLazienkaPietro) {mqtt="<[wrt:easy/205/PIR/Switch:state:JS(lastupdate.js)]"}

Transformation, lastupdate.js file:

(function(i){
    var now = new Date();
    var isoString = now.toISOString(); 
    return isoString; 
})
(input)

Every time mqtt is publishing to easy/205/Switch/PIR item easySwitchTime is updated with current time thanks to JS transformation service. Nice and easy, working reliable and transformation file can be reused, no necessity to write separate rules for each item.

Thanks to JavaScript function .toISOString() output string is compatible with DateTime item format. So we can apply date/time formating when displaying on sitemap. Or do any time calculation using standard Java functions.

1 Like

I like the use of the JS transformation. That is a clever approach and works great for MQTT. Vincent’s approach is a little more generic in that it will work for all Bindings, including those that don’t support transformations. But if you are only dealing with MQTT this is a great way to do it and you can avoid the extra Group and Rule.

JS function .toISOString returns UTC time. For local time one has to use modified version of transformation file:

(function(i){
    var now = new Date();
    isoString = new Date(now.getTime() - (now.getTimezoneOffset() * 60000)).toISOString();
    return isoString; 
})
(input)