diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java index 41482b93e29..832f94397e1 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java @@ -28,6 +28,8 @@ import org.openhab.binding.mqtt.generic.values.TextValue; import org.openhab.binding.mqtt.generic.values.Value; import org.openhab.core.io.transport.mqtt.MqttBrokerConnection; import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; import org.openhab.core.thing.ChannelUID; import org.openhab.core.types.Command; @@ -378,7 +380,13 @@ public class ChannelState implements MqttMessageSubscriber { // Outgoing transformations for (ChannelStateTransformation t : transformationsOut) { - String commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null); + Command cValue = mqttCommandValue; + // Only pass numeric value for QuantityType. + if (mqttCommandValue instanceof QuantityType qtCommandValue) { + cValue = new DecimalType(qtCommandValue.toBigDecimal()); + + } + String commandString = mqttFormatter.getMQTTpublishValue(cValue, "%s"); String transformedValue = t.processValue(commandString); if (transformedValue != null) { mqttFormatter = new TextValue(); @@ -395,7 +403,13 @@ public class ChannelState implements MqttMessageSubscriber { // Formatter: Applied before the channel state value is published to the MQTT broker. if (config.formatBeforePublish.length() > 0) { try { - commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, config.formatBeforePublish); + Command cValue = mqttCommandValue; + // Only pass numeric value for QuantityType of format pattern is %s. + if ((mqttCommandValue instanceof QuantityType qtCommandValue) + && ("%s".equals(config.formatBeforePublish) || "%S".equals(config.formatBeforePublish))) { + cValue = new DecimalType(qtCommandValue.toBigDecimal()); + } + commandString = mqttFormatter.getMQTTpublishValue(cValue, config.formatBeforePublish); } catch (IllegalFormatException e) { logger.debug("Format pattern incorrect for {}", channelUID, e); commandString = mqttFormatter.getMQTTpublishValue(mqttCommandValue, null); diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java index cfece9d67bf..3ae6ee2e427 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java @@ -84,7 +84,11 @@ public class NumberValue extends Value { public String getMQTTpublishValue(Command command, @Nullable String pattern) { String formatPattern = pattern; if (formatPattern == null) { - formatPattern = "%s"; + if (command instanceof DecimalType || command instanceof QuantityType) { + formatPattern = "%.0f"; + } else { + formatPattern = "%s"; + } } return command.format(formatPattern);