Skip to content

Commit

Permalink
Set reported power & current values to 0 if relay is OFF
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Apr 3, 2018
1 parent 2c5662b commit 2b445ec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -833,24 +833,46 @@ 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++) {

sensor_magnitude_t magnitude = _magnitudes[i];

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
{
Expand Down
1 change: 0 additions & 1 deletion code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2b445ec

Please sign in to comment.