Skip to content

Commit

Permalink
Add rounding option to PWM MQTT Output
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed Nov 25, 2024
1 parent 9d92c7d commit fa71a32
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mycodo/outputs/pwm_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# pwm_mqtt.py - Output for publishing PWM via MQTT
#
import copy
import math

from flask_babel import lazy_gettext
from sqlalchemy import and_
Expand Down Expand Up @@ -141,6 +142,19 @@
'name': 'Use Websockets',
'phrase': 'Use websockets to connect to the server.'
},
{
'id': 'round_integer',
'type': 'select',
'default_value': 'no',
'options_select': [
('no', 'No Rounding'),
('near', 'Round Nearest Whole'),
('up', 'Round Up'),
('down', 'Round Down')
],
'name': 'Round Integer',
'phrase': 'Round the payload value to an integer.'
},
{
'id': 'state_startup',
'type': 'select',
Expand Down Expand Up @@ -287,6 +301,14 @@ def output_switch(self, state, output_type=None, amount=None, output_channel=0):
else:
amount = 0

# Round before sending payload
if self.options_channels['round_integer'][0] == "near":
amount = int(round(amount))
elif self.options_channels['round_integer'][0] == "up":
amount = int(math.ceil(amount))
elif self.options_channels['round_integer'][0] == "down":
amount = int(math.floor(amount))

self.publish.single(
self.options_channels['topic'][0],
amount,
Expand Down

0 comments on commit fa71a32

Please sign in to comment.