ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
demo_sensor.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome::demo {
8
10 public:
11 void update() override {
12 float val = random_float();
13 bool increasing = this->get_state_class() == sensor::STATE_CLASS_TOTAL_INCREASING;
14 if (increasing) {
15 float base = std::isnan(this->state) ? 0.0f : this->state;
16 this->publish_state(base + val * 10);
17 } else {
18 if (val < 0.1) {
19 this->publish_state(NAN);
20 } else {
21 this->publish_state(val * 100);
22 }
23 }
24 }
25};
26
27} // namespace esphome::demo
This class simplifies creating components that periodically check a state.
Definition component.h:602
void update() override
Definition demo_sensor.h:11
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:62
mopeka_std_values val[3]
@ STATE_CLASS_TOTAL_INCREASING
Definition sensor.h:35
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:198