ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uptime_text_sensor.cpp
Go to the documentation of this file.
2
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome::uptime {
8
9ESPHOME_LOG_TAG(TAG, "uptime.sensor");
10
11static size_t append_flash_str(char *buf, size_t buf_size, size_t pos, const char *str) {
12#ifdef USE_ESP8266
13 return buf_append_str_p(buf, buf_size, pos, str);
14#else
15 return buf_append_str(buf, buf_size, pos, str);
16#endif
17}
18
19static void append_unit(char *buf, size_t buf_size, size_t &pos, const char *separator, unsigned value,
20 const char *label) {
21 if (pos > 0) {
22 pos = append_flash_str(buf, buf_size, pos, separator);
23 }
24 pos = buf_append_printf(buf, buf_size, pos, "%u", value);
25 pos = append_flash_str(buf, buf_size, pos, label);
26}
27
29
30// Inlined into update(), so firmware has no extra function
31ESPHOME_ALWAYS_INLINE inline void UptimeTextSensor::publish_uptime_(uint32_t uptime) {
32 const UptimeFormat *format = this->format_;
33 unsigned interval = this->get_update_interval() / 1000;
34
35 // Calculate all time units
36 unsigned seconds = uptime % 60;
37 uptime /= 60;
38 unsigned minutes = uptime % 60;
39 uptime /= 60;
40 unsigned hours = uptime % 24;
41 uptime /= 24;
42 unsigned days = uptime;
43
44 // Determine which units to display based on interval thresholds
45 bool seconds_enabled = interval < 30;
46 bool minutes_enabled = interval < 1800;
47 bool hours_enabled = interval < 12 * 3600;
48
49 // Show from highest non-zero unit (or all in expand mode) down to smallest enabled
50 bool show_days = progmem_read_byte(&format->expand) || days > 0;
51 bool show_hours = hours_enabled && (show_days || hours > 0);
52 bool show_minutes = minutes_enabled && (show_hours || minutes > 0);
53 bool show_seconds = seconds_enabled && (show_minutes || seconds > 0);
54
55 // If nothing shown, show smallest enabled unit
56 if (!show_days && !show_hours && !show_minutes && !show_seconds) {
57 if (seconds_enabled) {
58 show_seconds = true;
59 } else if (minutes_enabled) {
60 show_minutes = true;
61 } else if (hours_enabled) {
62 show_hours = true;
63 } else {
64 show_days = true;
65 }
66 }
67
68 // Build output string on stack
69 // Home Assistant max state length is 255 chars + null terminator
70 char buf[256];
71 size_t pos = 0;
72 buf[0] = '\0'; // Initialize for empty case
73
74 if (show_days)
75 append_unit(buf, sizeof(buf), pos, format->separator, days, format->days);
76 if (show_hours)
77 append_unit(buf, sizeof(buf), pos, format->separator, hours, format->hours);
78 if (show_minutes)
79 append_unit(buf, sizeof(buf), pos, format->separator, minutes, format->minutes);
80 if (show_seconds)
81 append_unit(buf, sizeof(buf), pos, format->separator, seconds, format->seconds);
82
83 this->publish_state(buf, pos);
84}
85
86void UptimeTextSensor::update() { this->publish_uptime_(static_cast<uint32_t>(millis_64() / 1000)); }
88
90void UptimeTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Uptime Text Sensor", this); }
91
92} // namespace esphome::uptime
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
void publish_state(const std::string &state)
Definition text_sensor.h:42
float get_setup_priority() const override
void publish_uptime(uint32_t uptime)
Publish uptime (in seconds) as text, keeping only the units the update interval can resolve.
const char * format
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
ESPHOME_LOG_TAG(TAG, "uptime.sensor")
size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str)
Safely append a PROGMEM string to buffer, returning new position (capped at size).
Definition helpers.h:1359
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
Definition helpers.h:1382
uint32_t value
Definition hal.cpp:41
uint64_t millis_64()
Definition hal.cpp:29
size_t size_t pos
Definition helpers.h:1333
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
Emitted by codegen in PROGMEM once per distinct format.