diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 05534f80..00e1741c 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -16,6 +16,10 @@ #define SENSOR_USE_INDEX 0 // Use the index in topic (i.e. temperature/0) // even if just one sensor (0 for backwards compatibility) +#ifndef SENSOR_POWER_CHECK_STATUS +#define SENSOR_POWER_CHECK_STATUS 1 // If set to 1 the reported power/current/energy will be 0 if the relay[0] is OFF +#endif + #ifndef SENSOR_TEMPERATURE_CORRECTION #define SENSOR_TEMPERATURE_CORRECTION 0.0 // Offset correction #endif diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 8540a761..aeafec2f 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -833,6 +833,11 @@ void sensorLoop() { // Pre-read hook _sensorPre(); + // Get the first relay state + #if SENSOR_POWER_CHECK_STATUS + bool relay_off = (relayCount() > 0) && (relayStatus(0) == 0); + #endif + // Get readings for (unsigned char i=0; i<_magnitudes.size(); i++) { @@ -840,17 +845,34 @@ void sensorLoop() { if (magnitude.sensor->status()) { - unsigned char decimals = _magnitudeDecimals(magnitude.type); - current = magnitude.sensor->value(magnitude.local); + + // Completely remove spurious values if relay is OFF + #if SENSOR_POWER_CHECK_STATUS + if (relay_off) { + if (magnitude.type == MAGNITUDE_POWER_ACTIVE || + magnitude.type == MAGNITUDE_POWER_REACTIVE || + magnitude.type == MAGNITUDE_POWER_APPARENT || + magnitude.type == MAGNITUDE_CURRENT || + magnitude.type == MAGNITUDE_ENERGY_DELTA + ) { + current = 0; + } + } + #endif + magnitude.filter->add(current); // Special case - if (magnitude.type == MAGNITUDE_EVENTS) current = magnitude.filter->result(); + if (magnitude.type == MAGNITUDE_EVENTS) { + current = magnitude.filter->result(); + } current = _magnitudeProcess(magnitude.type, current); _magnitudes[i].current = current; + unsigned char decimals = _magnitudeDecimals(magnitude.type); + // Debug #if SENSOR_DEBUG { diff --git a/code/platformio.ini b/code/platformio.ini index 4eff7f2c..66066ea7 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -729,7 +729,6 @@ board_flash_mode = dout lib_deps = ${common.lib_deps} lib_ignore = ${common.lib_ignore} build_flags = ${common.build_flags_1m} -DITEAD_SONOFF_SV -monitor_baud = 115200 upload_port = "192.168.4.1" upload_flags = --auth=fibonacci --port 8266 monitor_baud = 115200