Formatting state for MQTT. Shelly Outputs

Hi,

I’m trying to use MQTT to control some Shellys.

I have the input sorted and can read that fine as I get a dedicated MQTT topic:

The output only seems to be available in the full message in ‘switch:0’ and ‘switch:1’.

I am using text configuration files for my MQTT things. Here is my current version:

Thing mqtt:topic:shelly:H1_MainAttic_CeilingLight "Switch,H1,Main Attic,Ceiling Light" (mqtt:broker:aab6726def) [ availabilityTopic="shelly:H1_MainAttic/online", payloadAvailable="true", payloadNotAvailable="false" ]  {
    Channels:
    Type switch : input "Switch On/Off" [ 
        stateTopic="shelly:H1_MainAttic/status/input:0", 
        transformationPattern= "JSONPATH:$.state", 
        on= "true", 
        off= "false"
    ]
    Type switch : output "Output On/Off" [ 
        stateTopic="shelly:H1_MainAttic/status/switch:0", 
        commandTopic="shelly:H1_MainAttic/command/switch:0",
        formatBeforePublish= ""{'id':0, 'source': 'switch', 'output': %s, 'apower': JSONPATH:$.apower, 'voltage': JSONPATH:$.voltage, 'freq': JSONPATH:$.freq, 'current': JSONPATH:$.current, 'aenergy':{'total': JSONPATH:$.aenergy.total,'by_minute':[0.000,0.000,0.000],'minute_ts':1726649820}, 'ret_aenergy':{'total':0.000,'by_minute': JSONPATH:$.aenergy.by_minute,'minute_ts': JSONPATH:$.aenergy.minute_ts},'temperature':{'tC': JSONPATH:$.temperature.tC, 'tF': JSONPATH:$.temperature.tF}}", 
        transformationPattern= "JSONPATH:$.output", 
        on= "true", 
        off= "false"
    ]

I am unsure about the ‘commandTopic’ and the ‘formatBeforePublish’. I can read the value, so the state topic is working, but I can’t send a command.

Can anyone help? I found some posts in the forum that I thought would help, but this is as far as I can get on my own.

Why are you not using the easy mqtt interface?

Thing mqtt:topic:shelly:H1_MainAttic_CeilingLight "Switch,H1,Main Attic,Ceiling Light" (mqtt:broker:aab6726def) [ 
    availabilityTopic="shelly:H1_MainAttic/online", 
    payloadAvailable="true", 
    payloadNotAvailable="false" 
 ] {
    Channels:
    Type switch : input "Switch On/Off" [ 
        stateTopic="shelly:H1_MainAttic/status/input:0", 
        transformationPattern= "JSONPATH:$.state", 
        on= "true", 
        off= "false"
    ]
    Type switch : output "Output On/Off" [ 
        stateTopic="shelly:H1_MainAttic/status/switch:0", 
        commandTopic="shelly:H1_MainAttic/command/switch:0",
        formatBeforePublish= "{\"output\": %s}"
        transformationPattern= "JSONPATH:$.output", 
        on= "true", 
        off= "false"
    ]
 }

You don’t have to send all those values (especially if unknown…). afaik output will suffice.

Please be aware that valid JSON needs double quotes " around each string.
As you need those double quotes to setup the thing itself, you have to escape them with \
Please also be aware that true and false, although delivered as strings, inside JSON aren’t strings but boolean values, so they must not be surrounded with double quotes.

Thanks for replying, but I’m still stuck.

After reading your posts, the following is my best guess:

Thing mqtt:topic:shelly:H1_Kitchen_SinkLight "Switch,H1,Kitchen,Sink Light" (mqtt:broker:aab6726def) [ availabilityTopic="shelly:H1_Kitchen_Sink/online", payloadAvailable=true, payloadNotAvailable=false ]  {
    Channels:
    Type switch : input "Switch On/Off" [ 
        stateTopic="shelly:H1_Kitchen_Sink/status/input:0", 
        transformationPattern= "JSONPATH:$.state", 
        on= true, 
        off= false
    ]
    Type switch : output "Output On/Off" [ 
        stateTopic="shelly:H1_Kitchen_Sink/status/switch:0", 
        commandTopic="shelly:H1_Kitchen_Sink/command/switch:0",
        formatBeforePublish= "{\"output\": %s }",
        transformationPattern= "JSONPATH:$.output",
        on= true, 
        off= false
    ]
}

The input channel is working fine.
I am reading the output channel in openhab, but still cannot command.

Think I cracked it:

Thing mqtt:topic:shelly:H1_Kitchen_SinkLight "Switch,H1,Kitchen,Sink Light" (mqtt:broker:aab6726def) [ availabilityTopic="shelly:H1_Kitchen_Sink/online", payloadAvailable=true, payloadNotAvailable=false ]  {
    Channels:
    Type switch : input "Switch On/Off" [ 
        stateTopic="shelly:H1_Kitchen_Sink/status/input:0", 
        transformationPattern= "JSONPATH:$.state", 
        on= true, 
        off= false
    ]
    Type switch : output "Output On/Off" [ 
        stateTopic="shelly:H1_Kitchen_Sink/status/switch:0", 
        commandTopic="shelly:H1_Kitchen_Sink/command/switch:0",
        transformationPatternOut="MAP:trueFalse.map",
        transformationPattern= "JSONPATH:$.output",
        on= true, 
        off= false
    ]
}

trueFalse.map

true=on
false=off

Yes, ugly but might be the only solution, as shelly uses different values for state and command (shame on shelly!)