How to integrate relay board by MQTT

I have already installed mosquit MQTT broker on the PI4, now it’s online. my relay board support MQTT. it have 8 relay output, there is command and state topic. but the payload have feedback all relays states, not individual. this board, i have already integrate with home assistant with MQTT. now can you tell me the fast way use for openHAB.

this is I config with home assistant:
switch:
– platform: mqtt
name: ‘relay1’
unique_id: relay1
state_topic: ‘relay1/xxxxxxxxxxxxxxxxxxxxxxxx/state’
command_topic: ‘relay1/xxxxxxxxxxxxxxxxxxxxxxxx/set’
payload_on: ‘{“relay1”:{“on”:1}}’
payload_off: ‘{“relay1”:{“on”:0}}’
value_template: ‘{{ value_json.relay1.on }}’
state_on: 1
state_off: 0

xxxxxxxxxxxxxx is board ID

can you tell me how to config with openHAB, thanks.

You can use the MQTT binding and then create a Generic MQTT thing to connect with respective channels and json transformation (as it looks like your device is giving json output).

This is my relay board feedback, there are ON/OFF state for relay and input sensor port.

{“relay1”:{“on”:0},“relay2”:{“on”:0},“relay3”:{“on”:0},“relay4”:{“on”:0},“relay5”:{“on”:0},“relay6”:{“on”:0},“relay7”:{“on”:0},“relay8”:{“on”:0},“relay9”:{“on”:0},“relay10”:{“on”:0},“relay11”:{“on”:0},“relay12”:{“on”:0},“relay13”:{“on”:0},“relay14”:{“on”:0},“relay15”:{“on”:0},“relay16”:{“on”:0},“relay17”:{“on”:0},“relay18”:{“on”:0},“relay19”:{“on”:0},“relay20”:{“on”:0},“relay21”:{“on”:0},“relay22”:{“on”:0},“relay23”:{“on”:0},“relay24”:{“on”:0},“relay25”:{“on”:0},“relay26”:{“on”:0},“relay27”:{“on”:0},“relay28”:{“on”:0},“relay29”:{“on”:0},“relay30”:{“on”:0},“relay31”:{“on”:0},“relay32”:{“on”:0},“input1”:{“on”:0},“input2”:{“on”:0},“input3”:{“on”:0},“input4”:{“on”:0},“input5”:{“on”:0},“input6”:{“on”:0}}

This is turn ON relay1 , payload: {“relay1”:{“on”:0}

Could you tell me how to update to openHAB’s device by jason? sorry, i am beginner by openHAB.

Once you installed the MQTT binding and created a generic MQTT thing, you can then define the channels. I.e. for relay 1 you would then use the JSON Transformation (you need to install it under Settings) by selecting it in the options with the key $.relay1.on which should allow you to read/control the relay 1 on (1)/ off (0) signal.

edit: Link to the OH documentation

is " JSONPath Transformation" ?


whether as this ? not working.

Here is an example I used in my setup:

i can’t find the guide of document or YoutTube video for explain how to write the value of jsonpath.
my relay “This is turn ON relay1 , payload: {“relay1”:{“on”:0}”
i just want to know how I write? Thank you very much for your help.

Use outgoing value format. It’s like a string formatter, substituting your command.

If that’s the message you wanted, use
{“relay1”:{“on”:%s}}

could take an screen setting photo how to set, that’s will be great. :joy:

You can see it in my screenshot, the last field to enter called “Outgoing Value Format”

hi, do you have whatsapp or messenger? I want to ask you some questions,thanks

nope. This board allows direct messages, but I don’t do private tuition. If you’re struggling with something, it might benefit someone else later to see how to deal with it.

i have use this, but not work. my 1 and 0 is a string.

That’s the wrong location to apply this.

Go back to your Thing Channel and find the field with wording similar to

Check @chrismast 's screenshot for a hint to the correct location - it’s the second field down.

1 Like

now i can control relay on/off, but i don’t know how to update the relay state. here is my relay1 turn on/off.

this is mqtt feedback:
{“relay1”:{“on”:0},“relay2”:{“on”:0},“relay3”:{“on”:0},“relay4”:{“on”:0},“relay5”:{“on”:0},“relay6”:{“on”:0},“relay7”:{“on”:0},“relay8”:{“on”:0},“relay9”:{“on”:0},“relay10”:{“on”:0},“relay11”:{“on”:0},“relay12”:{“on”:0},“relay13”:{“on”:0},“relay14”:{“on”:0},“relay15”:{“on”:0},“relay16”:{“on”:0},“relay17”:{“on”:0},“relay18”:{“on”:0},“relay19”:{“on”:0},“relay20”:{“on”:0},“relay21”:{“on”:0},“relay22”:{“on”:0},“relay23”:{“on”:0},“relay24”:{“on”:0},“relay25”:{“on”:0},“relay26”:{“on”:0},“relay27”:{“on”:0},“relay28”:{“on”:0},“relay29”:{“on”:0},“relay30”:{“on”:0},“relay31”:{“on”:0},“relay32”:{“on”:0},“input1”:{“on”:0},“input2”:{“on”:0},“input3”:{“on”:0},“input4”:{“on”:0},“input5”:{“on”:0},“input6”:{“on”:0}}
one command will include 32 relays state feedback, how to update the relay state?

You’ll need to know what topic the relay boards sends that to, and us it as stateTopic in your channel(s).

You’d need 32 channels to represent all of them in openHAB as ON/OFF Switch Items. You don’t have to have all 32 if you are not interested in allof them.

Each channel has the same stateTopic, and a different Incoming Value Transformation of a JSONPATH, adjusted to each relay number.
Remember to change the Outgoing Value Format for each relay number as well.