28
28
import org .openhab .binding .mqtt .generic .values .Value ;
29
29
import org .openhab .core .io .transport .mqtt .MqttBrokerConnection ;
30
30
import org .openhab .core .io .transport .mqtt .MqttMessageSubscriber ;
31
+ import org .openhab .core .library .types .DecimalType ;
32
+ import org .openhab .core .library .types .QuantityType ;
31
33
import org .openhab .core .library .types .StringType ;
32
34
import org .openhab .core .thing .ChannelUID ;
33
35
import org .openhab .core .types .Command ;
@@ -378,7 +380,13 @@ public CompletableFuture<Boolean> publishValue(Command command) {
378
380
379
381
// Outgoing transformations
380
382
for (ChannelStateTransformation t : transformationsOut ) {
381
- String commandString = mqttFormatter .getMQTTpublishValue (mqttCommandValue , null );
383
+ Command cValue = mqttCommandValue ;
384
+ // Only pass numeric value for QuantityType.
385
+ if (mqttCommandValue instanceof QuantityType <?> qtCommandValue ) {
386
+ cValue = new DecimalType (qtCommandValue .toBigDecimal ());
387
+
388
+ }
389
+ String commandString = mqttFormatter .getMQTTpublishValue (cValue , "%s" );
382
390
String transformedValue = t .processValue (commandString );
383
391
if (transformedValue != null ) {
384
392
mqttFormatter = new TextValue ();
@@ -395,7 +403,13 @@ public CompletableFuture<Boolean> publishValue(Command command) {
395
403
// Formatter: Applied before the channel state value is published to the MQTT broker.
396
404
if (config .formatBeforePublish .length () > 0 ) {
397
405
try {
398
- commandString = mqttFormatter .getMQTTpublishValue (mqttCommandValue , config .formatBeforePublish );
406
+ Command cValue = mqttCommandValue ;
407
+ // Only pass numeric value for QuantityType of format pattern is %s.
408
+ if ((mqttCommandValue instanceof QuantityType <?> qtCommandValue )
409
+ && ("%s" .equals (config .formatBeforePublish ) || "%S" .equals (config .formatBeforePublish ))) {
410
+ cValue = new DecimalType (qtCommandValue .toBigDecimal ());
411
+ }
412
+ commandString = mqttFormatter .getMQTTpublishValue (cValue , config .formatBeforePublish );
399
413
} catch (IllegalFormatException e ) {
400
414
logger .debug ("Format pattern incorrect for {}" , channelUID , e );
401
415
commandString = mqttFormatter .getMQTTpublishValue (mqttCommandValue , null );
0 commit comments