Skip to content

Commit

Permalink
Reduce DB Writes
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorSchirmer committed Nov 11, 2024
1 parent c35f788 commit b280631
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions Integrations/ESPHome/Core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,45 @@ sensor:
- platform: internal_temperature
name: "ESP Temperature"
id: sys_esp_temperature
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 5.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
- platform: uptime
name: Uptime
id: sys_uptime
update_interval: 60s
filters:
- lambda: |-
if (reduce_db_reporting) return {};
return x;
- platform: wifi_signal
name: RSSI
id: wifi_signal_db
update_interval: 60s
entity_category: "diagnostic"
filters:
- lambda: |-
if (reduce_db_reporting) return {};
return x;
- platform: scd4x
id: scd40
Expand All @@ -365,23 +393,92 @@ sensor:
measurement_mode: "periodic"
i2c_id: bus_a
ambient_pressure_compensation_source: dps310pressure


- platform: ld2410
moving_distance:
name: Radar Moving Distance
id: moving_distance
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 5.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
still_distance:
name: Radar Still Distance
id: still_distance
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 5.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
moving_energy:
name: Radar Move Energy
id: radar_moving_energy
filters:
- lambda: |-
if (reduce_db_reporting) return {};
return x;
still_energy:
name: Radar Still Energy
id: radar_still_energy
filters:
- lambda: |-
if (reduce_db_reporting) return {};
return x;
detection_distance:
name: Radar Detection Distance
id: radar_detection_distance
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 5.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
g0:
move_energy:
name: g0 move energy
Expand Down Expand Up @@ -451,15 +548,75 @@ sensor:
light:
name: "LTR390 Light"
id: ltr390light
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 20.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
uv_index:
name: "LTR390 UV Index"
id: ltr390uvindex
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 20.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
- platform: dps310
id: dps_310
pressure:
name: "DPS310 Pressure"
id: dps310pressure
filters:
- lambda: |-
static float last_reported_value = 0.0;
float current_value = x;
// Check if the reduce_db_reporting switch is on
if (id(reduce_db_reporting).state) {
// Apply delta filter: only report if the value has changed by 2 or more
if (abs(current_value - last_reported_value) >= 5.0) {
last_reported_value = current_value; // Update the last reported value
return current_value;
} else {
// Return the last reported value without updating if change is less than 2
return {}; // Discard the update
}
} else {
// If reduce_db_reporting is off, report the current value normally
last_reported_value = current_value;
return current_value;
}
temperature:
name: "DPS310 Temperature"
id: dps310temperature
Expand Down Expand Up @@ -536,6 +693,14 @@ switch:
- platform: factory_reset
id: factory_reset_switch
internal: true
- platform: template
name: "Reduce DB Reporting"
id: reduce_db_reporting
icon: mdi:database
restore_mode: RESTORE_DEFAULT_ON
optimistic: true
entity_category: "config"


text_sensor:
- platform: ld2410
Expand Down

0 comments on commit b280631

Please sign in to comment.