Skip to content

openHAB: timestamp from last Item update

If openHAB has a Persistence service configured, the time of the last Item update (and a couple other times) is available in Rules. This makes it quite handy to check if a certain item hasn't been updated in a long time. As example I have the Tankerkoenig Binding installed, and this data is persisted in InfluxDB. This way I can see historic gas prices in Grafana.

When the Telegram Bot answers the "/tanken" question, it appends the timestamp of the last gas prices update.


In this case I might have different update timestamps from different stations, because the result returns the top N prices. I just pick the last price here.

    if (telegramLastMessage.state.toString == "Diesel") {
        var reply = "Dieselpreise:"
        var DateTime lastUpdate

        val HashMap<String, DecimalType> DieselMap = newHashMap
{% for tk in tankerkoenig %}
        if (TK_{{ tk[0] }}_Diesel.state != NULL && TK_{{ tk[0] }}_Diesel.state != UNDEF && TK_{{ tk[0] }}_Open.state == OPEN) {
            DieselMap.put("{{ tk[1] }}", (TK_{{ tk[0] }}_Diesel.state as DecimalType))
            lastUpdate = TK_{{ tk[0] }}_Diesel.lastUpdate("influxdb")
{% endfor %}
        for (Price : DieselMap.entrySet.sortBy[value]) {
            var String key = Price.getKey().toString
            var String value = Price.getValue().toString
            reply = reply + String::format("\n%s: `%s`€", key, value)
        reply = reply + String::format("\n\nvon: %s", lastUpdate.toString("yyyy-MM-dd HH:mm"))

        logInfo("Dieselpreise", reply)
        telegramAction.sendTelegramAnswer(Long::parseLong(telegramLastMessageChatId.state.toString), telegramLastMessageReplyId.state.toString, reply)

The entire code is explained in this blog posting. The relevant part is this:

var DateTime lastUpdate

lastUpdate = TK_{{ tk[0] }}_Diesel.lastUpdate("influxdb")

reply = reply + String::format("\n\nvon: %s", lastUpdate.toString("yyyy-MM-dd HH:mm"))

This defines a "DateTime" variable. While looping through the results, the lastUpdate timestamp from the "influxdb" persistance service is stored. And later when the result string is created, the formatted timestamp is appened.

Keep in mind that openHAB can have multiple Persistance services configured. The one you want to use ("influxdb") is specified here.


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options