What is the easiest way to toggle a switch item, if a command to “TOGGLE” is received on MQTT (on a channel that is not (yet) linked to the Switch item)).
I tried to configure the (receiving) mqtt channel as isCommand and linked a item that is also linked to an MQTT Dimmer, but this does not work.
Is there a way to do it without a rule? (If necessary with additional items/channels?)
At the end what I want is that I have two MQTT topics:
Topic1: A device sends a message that contains a string TOGGLE.
Topic2: Openhab sends a message that commands the new state (e.g. On or Off)
Actual Use case in detail:
I receive on MQTT:
Topic: tasmota/stat/kuCoffPwrCtrl/RESULT
Message: {"LedCtrlSw":{"Action":"TOGGLE"}} - my Channel is configured with JSON-Path Transformation so this will result in a received String.
I send on MQTT:
Topic: homie/kueche_thled/LED-RGBW/w/set
Message: TOGGLE or ON / OFF or 0 / 100 (whatever is easiest)
I had such a setup before using rules, but I wonder if it is possible to achieve this with configuration only (of Channels, Profiles, Items)?
TOGGLE is not a valid command for a Switch Item (yet? there was a PR open at one point but I don’t know if it ever went anywhere). So you’ll have to transform that TOGGLE command to either an ON command or an OFF command. For that you either need a transformation or a rule. One way or the other you’re going to need some code to make this happen.
To solve half your problem (receive on MQTT).
If you install the Basic Profiles Transformation (Administration → Add-on Store → Transformations) it has a ‘Generic Toggle Switch Profile’
Configure your MQTT channel to be a ‘trigger’ and link this to your switch item using the Generic Toggle Switch profile.
In the Profile Configuration enter TOGGLE (to correspond with the tasmota message)
This will now toggle your switch item.
As for the send on MQTT, I think you may need a rule, but there is also the ‘Generic Command Profile’ which may be worth investigating.