ESPHome 2025.5.0
Loading...
Searching...
No Matches
sun.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/time.h"
7
9
10namespace esphome {
11namespace sun {
12
13namespace internal {
14
15/* Usually, ESPHome uses single-precision floating point values
16 * because those tend to be accurate enough and are more efficient.
17 *
18 * However, some of the data in this class has to be quite accurate, so double is
19 * used everywhere.
20 */
21using num_t = double;
29
30struct Moment {
32
33 num_t jd() const;
34 num_t jde() const;
35};
36
44
52
53} // namespace internal
54
55class Sun {
56 public:
57 void set_time(time::RealTimeClock *time) { time_ = time; }
58 time::RealTimeClock *get_time() const { return time_; }
59 void set_latitude(double latitude) { location_.latitude = latitude; }
60 void set_longitude(double longitude) { location_.longitude = longitude; }
61
62 // Check if the sun is above the horizon, with a default elevation angle of -0.83333 (standard for sunrise/set).
63 bool is_above_horizon(double elevation = -0.83333) { return this->elevation() > elevation; }
64
69
70 double elevation();
71 double azimuth();
72
73 protected:
75 optional<ESPTime> calc_event_(bool rising, double zenith);
76 optional<ESPTime> calc_event_(ESPTime date, bool rising, double zenith);
77
80};
81
82class SunTrigger : public Trigger<>, public PollingComponent, public Parented<Sun> {
83 public:
85
86 void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
87 void set_elevation(double elevation) { elevation_ = elevation; }
88
89 void update() override {
90 double current = this->parent_->elevation();
91 if (std::isnan(current))
92 return;
93
94 bool crossed;
95 if (this->sunrise_) {
96 crossed = this->last_elevation_ <= this->elevation_ && this->elevation_ < current;
97 } else {
98 crossed = this->last_elevation_ >= this->elevation_ && this->elevation_ > current;
99 }
100
101 if (crossed && !std::isnan(this->last_elevation_)) {
102 this->trigger();
103 }
104 this->last_elevation_ = current;
105 }
106
107 protected:
109 double last_elevation_{NAN};
111};
112
113template<typename... Ts> class SunCondition : public Condition<Ts...>, public Parented<Sun> {
114 public:
115 TEMPLATABLE_VALUE(double, elevation);
116 void set_above(bool above) { above_ = above; }
117
118 bool check(Ts... x) override {
119 double elevation = this->elevation_.value(x...);
120 double current = this->parent_->elevation();
121 if (this->above_) {
122 return current > elevation;
123 } else {
124 return current < elevation;
125 }
126 }
127
128 protected:
129 bool above_;
130};
131
132} // namespace sun
133} // namespace esphome
Base class for all automation conditions.
Definition automation.h:75
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
This class simplifies creating components that periodically check a state.
Definition component.h:301
void trigger(Ts... x)
Definition automation.h:96
bool check(Ts... x) override
Definition sun.h:118
void set_above(bool above)
Definition sun.h:116
TEMPLATABLE_VALUE(double, elevation)
optional< ESPTime > calc_event_(bool rising, double zenith)
Definition sun.cpp:309
double elevation()
Definition sun.cpp:318
internal::HorizontalCoordinate calc_coords_()
Definition sun.cpp:277
void set_longitude(double longitude)
Definition sun.h:60
internal::GeoLocation location_
Definition sun.h:79
bool is_above_horizon(double elevation=-0.83333)
Definition sun.h:63
optional< ESPTime > sunset(double elevation)
Definition sun.cpp:315
void set_latitude(double latitude)
Definition sun.h:59
optional< ESPTime > sunrise(double elevation)
Definition sun.cpp:314
void set_time(time::RealTimeClock *time)
Definition sun.h:57
time::RealTimeClock * get_time() const
Definition sun.h:58
time::RealTimeClock * time_
Definition sun.h:78
double azimuth()
Definition sun.cpp:319
void set_sunrise(bool sunrise)
Definition sun.h:86
void update() override
Definition sun.h:89
void set_elevation(double elevation)
Definition sun.h:87
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
A more user-friendly version of struct tm from time.h.
Definition time.h:15
uint16_t x
Definition tt21100.cpp:5