Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Mar 25, 2024
1 parent fad8a87 commit d352d1a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/sml2mqtt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def default_config() -> Settings:
operations=[
{'negative on energy meter status': True},
{'factor': 3}, {'offset': 100}, {'round': 2},
{'or': [{'type': 'change filter'}, {'heartbeat filter': 120}]}
{'type': 'change filter'},
{'refresh action': 600}
]
)
]
Expand Down
8 changes: 5 additions & 3 deletions src/sml2mqtt/sml_value/operations/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ def process_value(self, value: float | None, info: SmlValueInfo) -> float | None
if value is None:
return None

perc = abs(1 - value / self.last_value) * 100
if perc < self.delta:
return None
# If the last value == 0 we always let it pass
if self.last_value:
perc = abs(1 - value / self.last_value) * 100
if perc < self.delta:
return None

self.last_value = value
return value
Expand Down
7 changes: 3 additions & 4 deletions tests/config/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_default():
assert '\n' + yaml == '''
logging:
level: INFO # Log level
file: sml2mqtt.log # Log file path (absolute or relative to config file) or stdout
file: sml2mqtt.log # Log file path (absolute or relative to config file) or "stdout"
mqtt:
connection:
identifier: sml2mqtt-A1b2
Expand Down Expand Up @@ -56,7 +56,6 @@ def test_default():
- factor: 3 # Factor with which the value gets multiplied
- offset: 100 # Offset that gets added on the value
- round: 2 # Round to the specified digits
- or:
- type: change filter # Filter which passes only changes
- heartbeat filter: 120.0 # Interval
- type: change filter # Filter which passes only changes
- refresh action: 600.0 # Interval
'''
3 changes: 3 additions & 0 deletions tests/sml_values/test_operations/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def test_diff_percent():
f = PercDeltaFilterOperation(5)
check_operation_repr(f, '5%')

assert f.process_value(0, None) == 0
assert f.process_value(0.049, None) == 0.049

assert f.process_value(100, None) == 100
assert f.process_value(104.999, None) is None
assert f.process_value(95.001, None) is None
Expand Down

0 comments on commit d352d1a

Please sign in to comment.