How do I know whether a esp temp sensor is still working when the temperature stays the same?

Hello,

I was wondering how the “Last Changed” and “Last Updated” exactly work regarding ESPHome AM2320 component sensor.

I have a AM2320 temp sensor connected to a Wemos D1 Mini. Update interval set to 5s, running for 15s and deepsleep for 5min.

It works, but I noticed the value is only updated in HA if the temperature has changed since the last data point. The “Last Updated” also does not change until the temperature has gone up or down by 0.1C.

How could I know, if I see the temperature has not changed in a while, if the node is still active?

Is it possible to let the “Last Updated” timestamp change every time the ESP wakes up and sends a new value? Or have a “Last Seen Online” sensor for each esphome node? Any suggestions?

Ideally, I would like a green/yellow/red dot showing if a sensor is online/online-in-the-past-10min/offline-longer-than-10min.

Thanks in advance.

This will update every time the esp connects or disconnects:

If its state is off for a bit longer than 5 minutes you could trigger an alert.

Thanks. Your suggestion works perfectly.

Is there a way to script the icon colors to change based on the binary sensor state?

Edit: Actually, the binary sensor also only seems to update its state when either temp or humidity change. Once those values stabilise, the status sensor also doesn’t get updated in HA. Help?

You can add a Ping binary sensor:

binary_sensor:
  - platform: ping
    host: !secret ip_address
    name: "PING - AM2320"
    count: 2
    scan_interval: 60

And if you set the “Last Updated” option for the sensor you will always see when the ESP device was online for the last time.

Thanks. The ping method seems to work as expected.

However, since my ESP only comes out of deep sleep for 10s, I have to keep pinging it at a fairly high rate not to miss the sensor when it comes online. Does this have influence on the HA performance?

Could I, as an alternative l, make a dummy sensor, for example called “esp_last_online_time”. And have ESPHome update it after it comes online with the current time? So basically have 2 sensors in the esphome yaml: The AM2320 one, and a timestamp one.

You could try this (not tested):

mqtt:
  broker: !secret ip_mqtt_broker
  username: !secret user_mqtt
  password: !secret pwd_mqtt
  id: mqtt_client

esphome:
  on_boot:
    priority: 600
    then:
      - lambda: |-
          char str[45];
          time_t currTime = id(sntp_time).now().timestamp;
          strftime(str, sizeof(str), "Last online at: %Y-%m-%d %H:%M:%S", localtime(&currTime));
          id(mqtt_client).publish("esp_am2320/notify_lasttime", str );

time:
  - platform: sntp
    timezone: Europe/Amsterdam
    servers: !secret ip_ntpsrv
    id: sntp_time

Another (tested) alternative is:

mqtt:
  broker: !secret ip_mqtt_broker
  username: !secret user_mqtt
  password: !secret pwd_mqtt
  id: mqtt_client

time:
  - platform: sntp
    timezone: Europe/Amsterdam
    servers: !secret ip_ntpsrv
    id: sntp_time
    on_time_sync:
      then:
        - lambda: |-
            char str[45];
            time_t currTime = id(sntp_time).now().timestamp;
            strftime(str, sizeof(str), "Last online at: %Y-%m-%d %H:%M:%S", localtime(&currTime));
            id(mqtt_client).publish("esp_am2320/notify_lasttime", str );

Perhaps have an automation in the ESP that you set to trigger on boot and it toggles a switch?

In your case (deep sleep enabled node) you might just want to use the force_update option on your esphome temperature sensor so it will even send the same value again to home assistant.

Under normal circumstances it’s not wanted because in creates unnecessary traffic and fills the ha db, but in your use case it sounds acceptable.

1 Like