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