ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
uptime_timestamp_sensor.cpp
Go to the documentation of this file.
2
3#ifdef USE_TIME
4
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
8
9namespace esphome::uptime {
10
11static const char *const TAG = "uptime.sensor";
12
14 this->time_->add_on_time_sync_callback([this]() {
15 if (this->has_state())
16 return; // No need to update the timestamp if it's already set
17
18 auto now = this->time_->now();
19 const uint32_t ms = millis();
20 if (!now.is_valid())
21 return; // No need to update the timestamp if the time is not valid
22
23 time_t timestamp = now.timestamp;
24 uint32_t seconds = ms / 1000;
25 timestamp -= seconds;
26 this->publish_state(timestamp);
27 });
28}
31 LOG_SENSOR("", "Uptime Sensor", this);
32 ESP_LOGCONFIG(TAG, " Type: Timestamp");
33}
34
35} // namespace esphome::uptime
36
37#endif // USE_TIME
bool has_state() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void add_on_time_sync_callback(std::function< void()> &&callback)
ESPTime now()
Get the time in the currently defined timezone.
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:29
const char *const TAG
Definition spi.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
uint16_t timestamp
Definition tt21100.cpp:2