[SOLVED] Switch and icon update via MQTT

Hi All.

It looks like a basic item, but cannot make it work.

What I want to do is show a switch with an icon to its left. When the switch is activated a command is sent via mqtt and state is received (to make a light on for instance). See this screen capture (Ceiling and Mirror):

1 Like

Share what you have attempted to make work, and hopefully someone here can offer advice. A good way to make your .items, .sitemap and .rules files legible to others is to wrap them in backticks like this:

```
contents of your .items file
```

```
contents of your .sitemap file
```

etc.

1 Like

Forgot that part, sorry :blush:

It’s in Spanish, but I guess it is easy to understand:

.sitemap

sitemap soldanet label="Soldanet" 
{
	Frame label="Garage" {
		Text item=luz_frente
	}
}   

.items

Switch luz_frente "Luz Frente" <switch> {mqtt="<[mosquitto:garage/switch1/state:state:default]"}

Your sitemap has Text item= but it should be Switch item=.

Ok, now I have a switch and a dynamic icon. How do I make it send commands (ON/OFF) to the appropriate topic? Thanks!

Please read the MQTT binding wiki on github. My answer would only be a copy/paste of what’s there.

If you plan many MQTT items in your Installation, I would recommend not to bother with individual bindings and instead configure MQTT event bus layer in openhab.cfg file. In this case you get automatically all topics for commands, states and feedback updates for each item.
See my post Node-RED as a rule/script engine for OpenHAB and MQTT wiki

Got it working.

Switch luz_frente "Luz Frente" <light> { mqtt="<[mosquitto:garage/switch1/state:*:default]", mqtt=">[mosquitto:garage/switch1/command:command:ON:1], >[mosquitto:garage/switch1/command:command:OFF:0]" }

The problem I’m facing now is that if I send the ON command (1) to garage/switch1/command, the relay will open but the UI won’t show the new state (light on, switch to the right). Is there something I am missing?

TIA

Try it this way (mqtt-binding only once, all Parameters for mqtt within the ":

Switch luz_frente "Luz Frente" <light> { mqtt="<[mosquitto:garage/switch1/state:*:default], >[mosquitto:garage/switch1/command:command:ON:1], >[mosquitto:garage/switch1/command:command:OFF:0]" }

Same thing :’(

If you refresh your browser, does the switch show correctly? If so, either the event path from server to browser is broken or there is a UI bug.

You might want to also add , autoupdate="false" inside the { } for the item, so that the switch will only change when someone publishes a new state to the broker. Without it, the switch will change state when it receives the command, which is often good for UI responsiveness, but is not a true reflection of the state as monitored from MQTT.

This code works:
Switch pileta_luces "Luces" <light> { mqtt=">[mosquitto:pileta/luces/command:command:ON:1], >[mosquitto:pileta/luces/command:command:OFF:0]" autoupdate="false" }

While this doesn’t:

Switch pileta_luces "Luces" <light> { mqtt="<[mosquitto:pileta/luces/state:*:default], >[mosquitto:pileta/luces/command:command:ON:1], >[mosquitto:pileta/luces/command:command:OFF:0]" autoupdate="false" }

Besides getting the state on the UI (which at this point would be irrelevant), I can’t even send commands to the item when the “<” direction part is present in the binding options. :frowning:

There is a comma missing in both Switch definitions before <space>autoupdate="false".

This will only work if the message published on that topic is the string ON or OFF. You would have to replace default with a transformation like MAP(onoff.map):

1=ON
0=OFF
ON=1
OFF=0

That’s very odd and I would like to see configuration and logs.

Switch pileta_luces "Luces" <light> { mqtt="<[mosquitto:pileta/luces/state:*:MAP(onoff.map)], >[mosquitto:pileta/luces/command:command:ON:1], >[mosquitto:pileta/luces/command:command:OFF:0]", autoupdate="false" }

conf/transform/onoff.map:

1=ON
0=OFF
ON=1
OFF=0

As soon as I save that change, I get this in the log file:

2016-12-19 11:38:20.254 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'soldanet.items'
2016-12-19 11:38:20.301 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'mqtt' of item 'pileta_luces' could not be parsed correctly
org.eclipse.smarthome.model.item.BindingConfigParseException: Configuration 'mosquitto:pileta/luces/state:*:MAP(onoff.map)' is not a valid inbound configuration: nvalid type.
        at org.openhab.core.binding.internal.BindingConfigReaderDelegate.processBindingConfiguration(BindingConfigReaderDelegate.java:50)[190:org.openhab.core.comat1x:2.0.0.b4]
        at org.eclipse.smarthome.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:312)[117:org.eclipse.smarthome.model.itm:0.9.0.b1]
        at org.eclipse.smarthome.model.item.internal.GenericItemProvider.internalDispatchBindings(GenericItemProvider.java:284)[117:org.eclipse.smarthome.model.itm:0.9.0.b1]
        at org.eclipse.smarthome.model.item.internal.GenericItemProvider.processBindingConfigsFromModel(GenericItemProvider.java:167)[117:org.eclipse.smarthome.moel.item:0.9.0.b1]
        at org.eclipse.smarthome.model.item.internal.GenericItemProvider.modelChanged(GenericItemProvider.java:347)[117:org.eclipse.smarthome.model.item:0.9.0.b1]
        at org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl.notifyListeners(ModelRepositoryImpl.java:210)[116:org.eclipse.smarthome.model.core:0.9.0.1]
        at org.eclipse.smarthome.model.core.internal.ModelRepositoryImpl.addOrRefreshModel(ModelRepositoryImpl.java:124)[116:org.eclipse.smarthome.model.core:0.9..b1]
        at org.eclipse.smarthome.model.core.internal.folder.FolderObserver.checkFile(FolderObserver.java:267)[116:org.eclipse.smarthome.model.core:0.9.0.b1]
        at org.eclipse.smarthome.model.core.internal.folder.FolderObserver.access$1(FolderObserver.java:261)[116:org.eclipse.smarthome.model.core:0.9.0.b1]
        at org.eclipse.smarthome.model.core.internal.folder.FolderObserver$WatchQueueReader.processWatchEvent(FolderObserver.java:142)[116:org.eclipse.smarthome.mdel.core:0.9.0.b1]
        at org.eclipse.smarthome.core.service.AbstractWatchQueueReader.run(AbstractWatchQueueReader.java:95)[96:org.eclipse.smarthome.core:0.9.0.b1]
        at java.lang.Thread.run(Thread.java:745)[:1.8.0_65]

Sure, let me know which file(s)

1 Like

Your inbound configuration is incorrect! It should be

<[mosquitto:pileta/luces/state:state:MAP(onoff.map)]

In English, the above means “for the mosquitto broker, when some client publishes on the pileta/luces/state topic, update the state of the item, but first transform the incoming message using the MAP(onoff.map) transformation.” So you must have the MAP transformation service installed (if on openHAB 2), and the file onoff.map must be in your transform directory.

See this section of the wiki for examples.

3 Likes

This did the trick!

Thank you for your support and help with this matter!

1 Like

This worked for me too. Had the same issue of HABPanel UI not updating.
Steps followed on openHAB2

  1. Install MAP transform from Paper UI
  2. Modify the mqtt inbound configuration like <[broker:esp/125603/state:state:MAP(onoff.map)]
  3. Adding the onoff.map on /etc/openhab2/transform

onoff.map file is
1=ON
0=OFF

Thanks and regards.

I had the same problem and managed to solve it because of this discussion. Thank you all.

1 Like

same here! - Thank you!

i am using a SONOFF 10A with this setting - just to let you know :slight_smile:

Switch Sonoff_1_Stehlampe "Stehlampe"
<light> (gLight,OG_Office) [ "Lighting" ]

		{ mqtt=">[broker:/Sonoff_1/GPIO/12:command:ON:1],
			    >[broker:/Sonoff_1/GPIO/12:command:OFF:0],
		        <[broker:/Sonoff_1/lightState/Switch:state:MAP(onoff.map)]" }