Set up a MQTT temperature sensor with deep sleep

I want to manage MQTT sensors with HA to measure things like temperature and humidity.

As I understand the sensor does not need to send the time when the measure was taken in the MQTT message. Now, the sensor publish one value then sleep for five minutes. Does it mean HA will display a temperature value that was updated four minutes ago as a current value?

I think I can I write a template so its displayed the date when the sensor value was updated, but is this date set by HA when the MQTT is received? What happens if it was a retained message sent maybe 30 minutes ago? Wouldnt it be better the sensor send the timestamp in the MQTT payload?

Well, yes. What would you expect if the sensor only updates every 5 minutes?

I would expect HA to show the date for that value (like “4 minutes ago”). But as I understand if I make a template to show a date for some value this date is set by HA itself when received the MQTT message. Does it take in account retained messages?

That the “last changed” date

The only difference a retained message does, here, is that the last value for the sensor will be taken from the message after an HA restart, rather than the sensor being “unknown” until an update.

Where this last changed date comes? Its set by HA when the last message arrives?

I agree with that, but imagine I have a sensor that published a value 30 minutes ago in a retained message. Then I add this sensor to HA. Then HA will show the last changed date as now thought it was published 30 minutes ago right? If I dont want that I could make the sensor to not send retained messages so its being unknown until an update. Im ok with that

It is set when the state of the sensor changes, literately.
That also means that if the device sends the same state, the “last changed” time wont change, as no new state will be recorded by HA.

If you add a timestamp as an attribute, the “last updated” time will change, though, as that one is updated whenever the state or an attribute is changed.

Indeed, the very first time you add the sensor to HA

So the “last changed” date is not the date it was communicated that value. So if a temperature sensor publish a value each 5 minutes, it will be displayed as a current value thought maybe the value was updated four minutes ago right?

Ok, thanks for clarifying it!

Yes it is. Exactly that, the last time the state changed, not the date when there was last a communication from the device.

Yes, of course. I don’t understand your focus on that.
If your device has a 5 minute resolution, so will the associated sensor.
The “current” value is only the state lastly changed by the device.

Ok, thanks again for clarifying. I need the sensors to save power so they can last some months with batteries, and for that I need them to sleep most of the time.
I dont need to know the pools water PH each minute, its ok if the value is published twice a day but still I would like to see the date the value was published by the sensor.
I guess I can use the last changed date for that or the timestamp attribute you proposed.