Switch sends '0/1' or 'OFF/ON' over MQTT [SOLVED]

  • Platform information:
    • Hardware: RaspberryPi Model B Rev 2
    • OS: openHABian 2.5.6-2
    • Java Runtime Environment:
    • openHAB version: 2.5.6-2
  • Issue of the topic:
    • MQTT switch messages arrived as ‘0’ or ‘1’ at clients

    • I did an update of the system and MQTT switch messages arrived as ‘ON’ or ‘OFF’ at clients

    • changed mqtt switch item to mqtt number item for 1 item => no messages are send with no error for that one item.
      Type number : CompRoomLightSwitch "Lampschakelaar Opkamer" [ stateTopic="openhab/command/86BB81_Switch", commandTopic="openhab/command/86BB81_Switch" ]

    • changed mqtt number item back to switch number => ALL mqtt clients receive switch messages as ‘0’ and ‘1’ again.
      Type switch : CompRoomLightSwitch "Lampschakelaar Opkamer" [ stateTopic="openhab/command/86BB81_Switch", commandTopic="openhab/command/86BB81_Switch" ]
      However, in the logs it still shows as ‘ON’/‘OFF’
      2020-07-05 17:18:09.638 [vent.ItemStateChangedEvent] - LampschakelaarOpkamer changed from OFF to ON

How can I make it consistent ‘0’ and ‘1’?

Have you tried a transformation? If you configure the Thing via PaperUI it’s easy to appy.

Within openHAB, a switch is either ON or OFF. That’s it, nothing else. However, you can map this ON and OFF to something else when sending and receiving to something externally. I’m not quite certain of the behaviour you want, but if you want to send 0 and 1 instead of ON and OFF then try:

Type switch : CompRoomLightSwitch "Lampschakelaar Opkamer" [ stateTopic="openhab/command/86BB81_Switch", commandTopic="openhab/command/86BB81_Switch", on="1", off="0"]

(You may not need the “” around the numbers - try both!)

Note that in the openHAB log it will still say that the switch is OFF or ON, but if you watch your MQTT topic you would hopefully see that you’re sending 1 and 0


Thanks for both answers, they both give me what I needed.
@H102 Needed to fiddle a bit to get it working through PaperUI but it works now.
@hafniumzinc That is the easiest solution looking how I currently set it up.

Glad you got it working. :+1:

Please click the square box on the post that provided the solution to mark the topic as solved.