ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
motion_event.cpp
Go to the documentation of this file.
1#include "motion_event.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
5
6namespace esphome::motion {
7
8ESPHOME_LOG_TAG(TAG, "motion.event");
9
11 LOG_EVENT("", "Motion Event", this);
12 ESP_LOGCONFIG(TAG,
13 " Threshold: %.3f\n"
14 " Cooldown: %" PRIu32 " ms",
15 this->threshold_, this->cooldown_);
16}
17
19 float ax = data.acceleration[X_AXIS];
20 float ay = data.acceleration[Y_AXIS];
21 float az = data.acceleration[Z_AXIS];
22 if (std::isnan(ax) || std::isnan(ay) || std::isnan(az)) {
23 // Reset the baseline so the next valid sample doesn't jerk-compare across the gap.
24 this->last_accel_[0] = NAN;
25 this->last_accel_[1] = NAN;
26 this->last_accel_[2] = NAN;
27 return;
28 }
29
31
32 if (!std::isnan(this->last_accel_[0])) {
33 float dx = ax - this->last_accel_[0];
34 float dy = ay - this->last_accel_[1];
35 float dz = az - this->last_accel_[2];
36 float jerk_mag = std::sqrt(dx * dx + dy * dy + dz * dz);
37
38 if (jerk_mag > this->threshold_) {
39 if (now - this->last_trigger_time_ >= this->cooldown_) {
40 this->trigger("shake");
41 this->last_trigger_time_ = now;
42 }
43 }
44 }
45
46 this->last_accel_[0] = ax;
47 this->last_accel_[1] = ay;
48 this->last_accel_[2] = az;
49}
50
51} // namespace esphome::motion
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void trigger(const char *event_type)
Trigger an event; the type is matched against the configured types by string compare.
Definition event.cpp:10
void process_motion_data(const MotionData &data)
ESPHOME_LOG_TAG(TAG, "motion.binary_sensor")
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t