ESPHome 2026.5.1
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::sun {
11
12namespace internal {
13
14/* Usually, ESPHome uses single-precision floating point values
15 * because those tend to be accurate enough and are more efficient.
16 *
17 * However, some of the data in this class has to be quite accurate, so double is
18 * used everywhere.
19 */
20using num_t = double;
28
29struct Moment {
31
32 num_t jd() const;
33 num_t jde() const;
34};
35
43
51
52} // namespace internal
53
54class Sun {
55 public:
56 void set_time(time::RealTimeClock *time) { time_ = time; }
57 time::RealTimeClock *get_time() const { return time_; }
58 void set_latitude(double latitude) { location_.latitude = latitude; }
59 void set_longitude(double longitude) { location_.longitude = longitude; }
60
61 // Check if the sun is above the horizon, with a default elevation angle of -0.83333 (standard for sunrise/set).
62 bool is_above_horizon(double elevation = -0.83333) { return this->elevation() > elevation; }
63
64 optional<ESPTime> sunrise(double elevation);
65 optional<ESPTime> sunset(double elevation);
66 optional<ESPTime> sunrise(ESPTime date, double elevation);
67 optional<ESPTime> sunset(ESPTime date, double elevation);
68
69 double elevation();
70 double azimuth();
71
72 protected:
74 optional<ESPTime> calc_event_(bool rising, double zenith);
75 optional<ESPTime> calc_event_(ESPTime date, bool rising, double zenith);
76
79};
80
81class SunTrigger : public Trigger<>, public PollingComponent, public Parented<Sun> {
82 public:
84
85 void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
86 void set_elevation(double elevation) { elevation_ = elevation; }
87
88 void update() override {
89 double current = this->parent_->elevation();
90 if (std::isnan(current))
91 return;
92
93 bool crossed;
94 if (this->sunrise_) {
95 crossed = this->last_elevation_ <= this->elevation_ && this->elevation_ < current;
96 } else {
97 crossed = this->last_elevation_ >= this->elevation_ && this->elevation_ > current;
98 }
99
100 if (crossed && !std::isnan(this->last_elevation_)) {
101 this->trigger();
102 }
103 this->last_elevation_ = current;
104 }
105
106 protected:
108 double last_elevation_{NAN};
110};
111
112template<typename... Ts> class SunCondition : public Condition<Ts...>, public Parented<Sun> {
113 public:
114 TEMPLATABLE_VALUE(double, elevation);
115 void set_above(bool above) { above_ = above; }
116
117 bool check(const Ts &...x) override {
118 double elevation = this->elevation_.value(x...);
119 double current = this->parent_->elevation();
120 if (this->above_) {
121 return current > elevation;
122 } else {
123 return current < elevation;
124 }
125 }
126
127 protected:
128 bool above_;
129};
130
131} // namespace esphome::sun
Base class for all automation conditions.
Definition automation.h:459
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
This class simplifies creating components that periodically check a state.
Definition component.h:602
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:482
void set_above(bool above)
Definition sun.h:115
TEMPLATABLE_VALUE(double, elevation)
bool check(const Ts &...x) override
Definition sun.h:117
optional< ESPTime > calc_event_(bool rising, double zenith)
Definition sun.cpp:312
double elevation()
Definition sun.cpp:321
internal::HorizontalCoordinate calc_coords_()
Definition sun.cpp:280
void set_longitude(double longitude)
Definition sun.h:59
internal::GeoLocation location_
Definition sun.h:78
bool is_above_horizon(double elevation=-0.83333)
Definition sun.h:62
optional< ESPTime > sunset(double elevation)
Definition sun.cpp:318
void set_latitude(double latitude)
Definition sun.h:58
optional< ESPTime > sunrise(double elevation)
Definition sun.cpp:317
void set_time(time::RealTimeClock *time)
Definition sun.h:56
time::RealTimeClock * get_time() const
Definition sun.h:57
time::RealTimeClock * time_
Definition sun.h:77
double azimuth()
Definition sun.cpp:322
void set_sunrise(bool sunrise)
Definition sun.h:85
void update() override
Definition sun.h:88
void set_elevation(double elevation)
Definition sun.h:86
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint16_t x
Definition tt21100.cpp:5