MQTT Beginner problem

  • Platform information:
    • Hardware: raspberry pi3b+
    • OS: rasbian stretch
    • Java Runtime Environment: openjdk
    • openHAB version: 2

I want to switch my statusdisplay via MQTT. I need to add a switch which send “open” in the On position and “close” in the Off position. Therefore I created items

String Sonoff_Basic_01_String "Licht [%s]" {mqtt="<[mosquitto:statusdisplay/status/window/schlafzimmer:state:default]"}
Switch	Sonoff_Basic_01_Switch	["Lighting"] {mqtt=">[mosquitto:statusdisplay/status/window/schlafzimmer:command:*:OPEN]"}

and rule

rule "Update Switch Proxy Item"
when
    Item Sonoff_Basic_01_String received update
then
    if(Sonoff_Basic_01_Switch.state == ON) Sonoff_Basic_01_Switch.postUpdate(OFF)
    else Sonoff_Basic_01_Switch.postUpdate(ON)
end

What do I need to change that it works with the open and close command and the switch is switched if it recieved it

Its a sonoff switch, it will turn on and off. The two items above can be reduced to one. You can use MAP transformation to change ON to OPEN, OFF to CLOSED, ect…

What are you referring to as statusdisplay? If this is tasmota you can view all status info by setting Status to 0.

Instead using a rule, you can set this up directly in the item file:

Switch Sonoff_Basic_01_Switch ["Lighting"] {mqtt=">[ON:mosquitto:statusdisplay/status/window/schlafzimmer:command:*:OPEN] >[OFF:mosquitto:statusdisplay/status/window/schlafzimmer:command:*:CLOSE]"}

I use this in a http binding to fire up different put requests on ON or OFF state :slight_smile:


2018-11-29 22:32:46.350 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'mqtt' of item 'Sonoff_Basic_01_Switch' could not be parsed correctly.
org.eclipse.smarthome.model.item.BindingConfigParseException: Configuration 'ON:mosquitto:statusdisplay/status/window/schlafzimmer:command:*:OPEN] >[OFF:mosquitto:statusdisplay/status/window/schlafzimmer:command:*:CLOSE' is not a valid outbound configuration: Configuration requires 5 parameters separated by ':'

Any Idea???

try:

Switch	Sonoff_Basic_01_Switch "Licht_01"	["Lighting"] {mqtt=">[mosquitto:statusdisplay/status/window/schlafzimmer:command:*:MAP(Sonoff.map)], <[mosquitto:statusdisplay/status/window/schlafzimmer:state:MAP(Sonoff.map)]"}

$OPENHAB_CONF/transform/Sonoff.map (with MAP xform addon installed)

ON=open
OFF=close
open=ON
close=OFF
NULL=Unknown

You should NOT be using the same MQTT topic for state & command
Read on how Sonoff works and search for other examples in the forum
even this config that I give you here is wrong :slight_smile: (it creates a “loop”)

what do mean by this? what are you trying to achieve?

everything as expected now…but i still get one warning

2018-11-30 06:25:44.069 [WARN ] [mon.registry.AbstractManagedProvider] - Could not update element with key _tags:Sonoff_Basic_01_Switch in ManagedMetadataProviderImpl, because it does not exists.

What is the xform Addon???

When do you get this warning? At startup? Or when you try to perform a certain action? (e.g apply tags via REST)
Maybe you have the same Item name Sonoff_Basic_01_Switch created twice (once in PaperUI and once in the items file). OH2 is trying to apply a tag to the Item but it can’t because it’s not Managed (it has been created manually in items files.

Check using the OH2 console:

items list |grep Sonoff

It’s slang for “Transformation Services:slight_smile: In this specific case, you are going to use: the MAP xform addon.

Remember: the Item config that I gave you is wrong… it uses the same topic for publishing and subscribing.

It’s better if you describe what you want to achieve to avoid falling into the XY Problem trap.

How is then a corect syntax to publish “open” with a switch when it turned on in the topic “mosquitto:statusdisplay/status/window/schlafzimmer” and “closed” when it turned off?

the syntax that I gave you above is correct for doing this part (sending)
It’s not correct for the other part (receiving)
If you want to use one Item to do both, you should be publishing to one topic and subscribing to another
When you press the Button (ON), you will send (due to the xform) “open” MQTT payload to the topic, then the Item will receive the same message for it’s subscription (and will translate it from “open” to “ON” due to the map xform)

It’s pointless to receive the subscription since you already have the updated state when you send a Command.
Plus, in this way, the status is not necessarily the correct one since you don’t know if the device reacted and changed its status.

It will go like this:
ON->open->open->ON

So: to send only, use:

Switch	Sonoff_Basic_01_Switch "Licht_01"	["Lighting"] {mqtt=">[mosquitto:statusdisplay/status/window/schlafzimmer:command:*:MAP(Sonoff.map)]"}

with

ON=open
OFF=close
NULL=Unknown

But … again… this is not how Sonoff is supposed to work (you shouldn’t need to transform the payload and you should already have 2 different topics available=one for commands and one for statuses)

Which firmware are you running on the Sonoff? Vanilla or other?

1 Like