Mqtt sensor and last-seen-timer in dashboard

Dear community,

I am playing around with a D1_mini, mqtt, a DTH-sensor and DeepSleepMode. Everything works fine, except timestamp is shown as unknown. Over mqtt, I get a timestamp and I want to show this in dasboard, so that I know how old the data is.

data recieved via mqtt:

Nachricht 9 empfangen auf tele/D1mini_Wohnzimmer/SENSOR um 21:03:

{
    "Time": "2022-03-23T21:02:59",
    "ANALOG": {
        "A0": 895
    },
    "AM2301": {
        "Temperature": 22.8,
        "Humidity": 40.6,
        "DewPoint": 8.7
    },
    "TempUnit": "C"
}

QoS: 0 - Retain: false 

My sensor config (see last sensor)

  - platform: mqtt
    name: "Wohnzimmer Temperatur"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    device_class: "temperature"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.AM2301.Temperature | float }}"
    
  - platform: mqtt
    name: "Wohnzimmer Feuchtigkeit"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    device_class: "humidity"
    unit_of_measurement: "%"
    value_template: "{{ value_json.AM2301.Humidity | float}}"  

  - platform: mqtt
    name: "Wohnzimmer Taupunkt"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    device_class: "temperature"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.AM2301.DewPoint | float}}" 

  - platform: mqtt
    name: "Wohnzimmer Akku Ladung"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    device_class: "voltage"
    unit_of_measurement: "V"
    value_template: "{{ ( value_json.ANALOG.A0|float * 4.5 / 1023 ) | round(2) }}"

  - platform: mqtt
    name: "Wohnzimmer last seen"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    value_template: >
        {% set s = value_json.Time | timestamp_custom('%Y-%m-%dT%H:%M:%S%z') %}
        {{ s[:-2] ~ ':' ~ s[-2:] }}
#    value_template: "{{ as_timestamp( value_json.Time ) | timestamp_custom('%Y-%m-%dT%H:%M:%S') }}"
    device_class: "timestamp"

two questions:

  • I see errors in log, that timezone is missing in timestamp.
  • I don’t understand, what format the timestamp is provided in and how to parse it into a real timestamp

any help is very much appreciated,
Chris

EDIT: the D1 mini is running on Tasmota 10.0.0

EDIT2: current dashboard card
grafik

Well, I got one step further:
Within Tasmota, there is an option to get a correct timestamp, including the timezone-offset: “SetOption52 1”

This is needed, so that HomeAssistent accepts the string as a timestamp. (Templating → Time)

Knowing this, I am still struggling with my sensor.

  - platform: mqtt
    name: "Wohnzimmer last seen"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    value_template: "{{ as_timestamp( value_json.Time )  }}"
    device_class: "timestamp"

Can anybody help?

MQTT delivers the following JSON:

{
    "Time": "2022-04-02T23:40:01+02:00",
    "ANALOG": {
        "A0": 952
    },
    "AM2301": {
        "Temperature": 22.3,
        "Humidity": 39.5,
        "DewPoint": 7.8
    },
    "TempUnit": "C"
}

Finally, I figured it out myself.

When I started this thread, the timestamp delivered by JSON was missing the timezone details. Yesterday, I was able to re-configure Tasmota and now, the timestamp is complete. (see post)
All there is to do for my template sensor:

  - platform: mqtt
    name: "Wohnzimmer last seen"
    state_topic: "tele/D1mini_Wohnzimmer/SENSOR"
    value_template: "{{ value_json.Time }}"
    device_class: "timestamp"

grafik

3 Likes

nice. i need a “last seen” stat as well but editing yaml for every device seems a lot of work. lets hope the tasmota integrations adds this status sensor native.

1 Like

Hi @zuz242 ,

well, in my case, I only have two of those devices. They are “custom made sensors, battery powered”. The are running with deep sleep mode, which is managed by an automation. This works very stable.
The only downside: when using the Tasmota integration and my devices going to deep sleep, the values in dasboard gets emptied. (MQTT receives some “power down” value.) That is, why I used templates for these two sensors.
All other sensors use the Tasmota integration and are running stable, without the need for a last seen / last updated timestamp.