Like the OP here, I wanted the Uptime Sensor in ESPHome to do the formatting and not have to have Home Assistant do the work. So, I offer this code snippet to @Klagio (and everyone else).
esphome code:
sensor:
- platform: uptime
name: "Uptime"
id: uptime_s
update_interval: 15s
text_sensor:
- platform: template
name: "Uptime (formatted)"
lambda: |-
uint32_t dur = id(uptime_s).state;
int dys = 0;
int hrs = 0;
int mnts = 0;
if (dur > 86399) {
dys = trunc(dur / 86400);
dur = dur - (dys * 86400);
}
if (dur > 3599) {
hrs = trunc(dur / 3600);
dur = dur - (hrs * 3600);
}
if (dur > 59) {
mnts = trunc(dur / 60);
dur = dur - (mnts * 60);
}
char buffer[17];
sprintf(buffer, "%ud %02uh %02um %02us", dys, hrs, mnts, dur);
return {buffer};
icon: mdi:clock-start
update_interval: 15s