Skip to content

Commit fa71a32

Browse files
committed
Add rounding option to PWM MQTT Output
1 parent 9d92c7d commit fa71a32

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

mycodo/outputs/pwm_mqtt.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pwm_mqtt.py - Output for publishing PWM via MQTT
44
#
55
import copy
6+
import math
67

78
from flask_babel import lazy_gettext
89
from sqlalchemy import and_
@@ -141,6 +142,19 @@
141142
'name': 'Use Websockets',
142143
'phrase': 'Use websockets to connect to the server.'
143144
},
145+
{
146+
'id': 'round_integer',
147+
'type': 'select',
148+
'default_value': 'no',
149+
'options_select': [
150+
('no', 'No Rounding'),
151+
('near', 'Round Nearest Whole'),
152+
('up', 'Round Up'),
153+
('down', 'Round Down')
154+
],
155+
'name': 'Round Integer',
156+
'phrase': 'Round the payload value to an integer.'
157+
},
144158
{
145159
'id': 'state_startup',
146160
'type': 'select',
@@ -287,6 +301,14 @@ def output_switch(self, state, output_type=None, amount=None, output_channel=0):
287301
else:
288302
amount = 0
289303

304+
# Round before sending payload
305+
if self.options_channels['round_integer'][0] == "near":
306+
amount = int(round(amount))
307+
elif self.options_channels['round_integer'][0] == "up":
308+
amount = int(math.ceil(amount))
309+
elif self.options_channels['round_integer'][0] == "down":
310+
amount = int(math.floor(amount))
311+
290312
self.publish.single(
291313
self.options_channels['topic'][0],
292314
amount,

0 commit comments

Comments
 (0)