ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
counter_sensor.cpp
Go to the documentation of this file.
1#include "counter_sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome::counter {
5
6ESPHOME_LOG_TAG(TAG, "counter");
7
9 if (this->restore_) {
11 this->pref_.load(&this->value_);
12 }
13 this->publish_state(static_cast<float>(this->value_));
14}
15
16void CounterSensor::set_value(int64_t value) {
17 this->value_ = value;
18 this->publish_state(static_cast<float>(value));
19 if (this->restore_)
20 this->pref_.save(&this->value_);
21}
22
24 LOG_SENSOR("", "Counter", this);
25 ESP_LOGCONFIG(TAG, " Restore: %s", YESNO(this->restore_));
26}
27
28} // namespace esphome::counter
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "counter")