Device's last powered on time

Is there any way to determine the time when my device was last powered off?

any help will be appreciated.

@pcr @martin.peeters @Denis


what are you trying to achieve? because under the attribute, there is the last update.


actually, client requirement is how much time device is power on .

You mean the Uptime?

In my case I use MQTT Last Will to know when it goes offline, then in case I have a script on my device that logs the time every minute it’s on (so I know if it just lost connection but didn’t turn off)

yes need uptime value.

Your Device got millis? then you could convert millis to hh:mm:ss and publish it.

i will try to do it and i will update here.

but watch out, unsigned long is only 32bit, so the max millis is about 50 days.

Hi @Denis
i got the time in seconds , refer beloe sc

and now i am thinking how i can convert into hours min and second


without proof, but something like this… your attribute has to be a text ofc.

unsigned long startTime;

void setup() {
  startTime = millis();
void loop() {
  unsigned long currentMillis = millis();
  unsigned long uptimeMillis = currentMillis - startTime;

  unsigned long seconds = uptimeMillis / 1000;
  unsigned long minutes = seconds / 60;
  unsigned long hours = minutes / 60;

  seconds = seconds % 60;
  minutes = minutes % 60;
  hours = hours % 24;

  String uptimeString = String(hours) + ":" + String(minutes) + ":" + String(seconds);
  client.publish(topic, uptimeString);

i got the time in seconds so
unsigned long seconds = uptimeMillis / 1000; should remove.

  1. I do not know your code
  2. This is just an example… there is a little bit more of your own initiative.

Thanks @Denis

i got which i need

1 Like

Glad to hear that. :wink: