Timestamp when item has changed

I have definded a “lastupdate” item for each of my oudoor hue motion sensors. The idea was to set a timestamp every time a motion has been detected. By doing that I will be able to see when the last motion was detected on each motion sensor. The problem is that it seems like the “ParkingHueMotion_Occupancy” is updated with the same value everytime the hue sensor is reporting a temperature or lux change. I will like to update the timestamp only when “ParkingHueMotion_Occupancy” is changed from “false” to “true”.

Any suggestion on how to do that? If not, is there any other way to retrieve a timestamp from when an item was changed last time?

Rule example:

configuration: {}
triggers:

  • id: “1”
    configuration:
    itemName: ParkingHueMotion_Occupancy
    type: core.ItemStateUpdateTrigger
    conditions:
    actions:

  • inputs: {}
    id: “2”
    configuration:
    type: application/vnd.openhab.dsl.rule
    script: |2-

         ParkingHueMotion_LastMotion.postUpdate(new DateTimeType())
    

    type: script.ScriptAction

I tried to change it to the following (adding a condition). I hope this will solve my problem.

configuration: {}
triggers:

  • id: “1”
    configuration:
    itemName: EntranceHueMotion_Occupancy
    type: core.ItemStateChangeTrigger
    conditions:

  • inputs: {}
    id: “3”
    configuration:
    itemName: EntranceHueMotion_Occupancy
    operator: =
    state: “true”
    type: core.ItemStateCondition
    actions:

  • inputs: {}
    id: “2”
    configuration:
    type: application/vnd.openhab.dsl.rule
    script: |2-

         EntranceHueMotion_LastMotion.postUpdate(new DateTimeType())
    

    type: script.ScriptAction

Starting from openhab 5.0 every item has a built in timestamp of last update and last change

https://github.com/openhab/openhab-core/pull/4351

It is also available in rulesdsl event:

Sounds interesting. I have tried to find script examples (DSL/Javascript), but without any luck. Also the documentation of getLastStateChange is very sparse.

It’s quite simple. In your rulesDSL, ParkingHueMotion_Occupancy.lastStateChange will give you a ZonedDateTime

I am not familiar with jsscripting, but open Item - openHAB JS and search for “laststatechange” you’ll see the method name.

in JRuby it’s just ItemName.last_state_change which returns a ZonedDateTime much like in DSL.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.