MQTT command to control other items

Hi,
I am beginner at openhab2.
I have 2 pieces arduino. ¨

1x - for switches of lights.
1x - for relays.

If I press any switch arduino will send mqtt message to topic house/switch/PIN_NUMBER in format:
{"info":{"id":37,"status":"ON","intensity":"0","step":2}}

id - PIN NUMBER … in this example 37
status - status of switch
intensity - it for dimmable light
step - number of pressing

My idea:
When received MQTT message at topic house/switch/37
I would like to change status in Openhab2 and send MQTT message to control relay.
I need read original MQTT message in rules because I have to edit new MQTT message.

For example if received msq {“id”:37,“status”:“ON”,“intensity”:“0”,“step”:1}} with step 1 I will switch on/off just one relay. But if I received msg with step 2 I would like to switch on/off more relays.

I dont know how to do it :frowning:
Thank you for your help

Items:
Switch Boiler_Light "Light" <light> (Boiler, gLight) ["Lighting"] {mqtt="<[mosquitto:house/switch/37:state:JSONPATH($.info.stav)]"}

Rules:

rule "TEST"
when
         Item Boiler_Light received command
then
      logInfo("Recived command",receivedCommand.toString)
end

Not sure exactly how the out going msg should look like but try this. Also create a sitemap with your item Boiler_Light and if the mqtt msg is correct you should be able to turn the relay on and off from there.

Switch Boiler_Light "Light" <light> (Boiler, gLight) ["Lighting"] {mqtt=">[mosquitto:house/switch/37:command:*:1], <[mosquitto:house/switch/37:state:JSONPATH($.info.status)]"}

You will need three Items:

  1. Item to receive the MQTT message
  2. Item that controls one relay
  3. Item that controls the other relay

When Item 1 receives an update you need a Rule to trigger and send the commands to the one or both of the relays based on the contents of the MQTT message.

If the only thing that you need from the MQTT message is the step value, then Item 1 would be

 Number Boiler_Message { mqtt="<[mosquitto:house/switch/37:state:JSONPATH($.info.status)]" }

The Rule would then look something like:

rule "Boiler Message"
when
    Item Boiler_Message received update
then
    if(Boiler_Message.state == 1) {
        // do stuff
    }
    else if(Boiler_Message.state == 2){
        // do stuff
    }
    else {
        logWarn("Boiler", "Received unknown state: " + Boiler_Message.state)
    }
end

Your rule working but I need to catch original MQTT message at topic house/switch/37 not only state.

Is it possible to receive MQTT message in rules?
I would like create something like that …

rule "Boiler Message"

topicCMD = "house/relay/37"

when
    Item Boiler_Light received update
then
    if(receivedCommand != null){
      String mqttMessage = receivedCommand.toString
      val String steps = transform("JSONPATH", "$.info.step", mqttMessage)
      val String status = transform("JSONPATH", "$.info.status", mqttMessage)
      val String intensity = transform("JSONPATH", "$.info.intensity", mqttMessage)
      if(status == 1 && intensity == 9999){
          if(steps == 1){
              publish("mosquitto",topicCMD,'{"relay":1,"status":1}')
          }else if(steps == 2){
              publish("mosquitto",topicCMD,'{"relay":5,"status":1}')
              publish("mosquitto",topicCMD,'{"relay":7,"status":1}')
          }else{
              publish("mosquitto",topicCMD,'{"relay":1,"status":1}')
              publish("mosquitto",topicCMD,'{"relay":5,"status":1}')
              publish("mosquitto",topicCMD,'{"relay":7,"status":1}')
          }
        }else{
            if(steps == 1){
              publish("mosquitto",topicCMD,'{"relay":1,"status":0}')
            }else if(steps == 2){
                publish("mosquitto",topicCMD,'{"relay":5,"status":0}')
                publish("mosquitto",topicCMD,'{"relay":7,"status":0}')
            }else{
                publish("mosquitto",topicCMD,'{"relay":1,"status":0}')
                publish("mosquitto",topicCMD,'{"relay":5,"status":0}')
                publish("mosquitto",topicCMD,'{"relay":7,"status":0}')
            }
      }else{
        // some rules
      
      }
    }else{
      logInfo("Boiler_Light","MQTT is empty")
    }   
end

Only with an Item. You don’t have to use a transform and you will get the full message.

NOTE: if you have received update as a Rule trigger receivedCommand will always be null.

If I use this:

{mqtt="<[mosquitto:house/switch/37:state:default]"} in Item

and this rule

when
    Item Boiler_Light received command
then

  logInfo("te",receivedCommand.toString)
end

I get only error: given new state is NULL, couldn’t post update for ‘Boiler_Light’

Boiler_Light is a String Item? The message is a String.

No, it is switch.