Switch state?

Hi,

This maybe is a weird question but, looking at the docs and I can’t figure what is the use of the ‘state’ for switches with MQTT (except for rules)

Any pointers on what its for ?

Thank you :slight_smile:

If it is an outgoing configured MQTT binding when you toggle the switch it will send text to the topic. So you can use, for example, MQTT to trigger a remote actuator.

What I mean is the mqtt="<[broker:/path/to/determined/switch:state*:default]"

that part in a Switch with mqtt binding… what does it do ?

thanks :wink:

tl;dr: It will update the state of the switch based on the receipt of MQTT messages. It is useful if you have a remote switch or contact that you want to represent in OH. For example, I use this sort of MQTT binding config to have Contacts (it would work with Switches too) to represent the OPEN/CLOSED state of my exterior doors which publish their state over MQTT.

Per the MQTT Binding wiki page:

< - direction of the communication, < means incoming so this switch doesn’t publish anything, only receives updates

broker - name of the broker as configured in openhab.cfg

/path/to/determined/switch - the MQTT topic where messages are published for this switch to read

state - interpret any incoming messages as the new state (i.e. processed as an update) as opposed to a command (i.e. trigger other bindings that may be configured on this Switch)

    • this is an error and should not be here I think

default - pass the incoming message through unchanged, since this is a Switch your messages must be something that the Switch Item can parse into an ON/OFF state: i.e. the numbers 1 or 0, or the String “ON” or “OFF”. If the message is of some other format you need to replace default with a transform to convert what is coming in to one of these values.

So, any messages that get published to that topic is read by OH and used to set the current state of the Switch without issuing commands.

1 Like

I ask because i manually sent a “ON” and “OFF” to a MQTT switch and the state did not change.

Configured like this :
Switch swGarageLights “Garage Lights” (grpGarage,Lights) {mqtt="

[mosquitto:/openhab/grpGarage/swGarageLights/command:command:ON:ON],
[mosquitto:/openhab/grpGarage/swGarageLights/command:command:OFF:OFF],<[mosquitto:/openhab/grpGarage/swGarageLights/state:state:*:default"}

did a : mosquitto_pub -t ‘/openhab/grpGarage/swGarageLights/state’ -m ON

and the light did not turn off. (and vice versa)

“state” just unpdates the state of the Item. It doesn’t not trigger the other parts of the binding. If you want the turn around and publish an ON to /openhab/grpGarage/swGarageLights/command when /openhab/grpGarage/swGarageLights/state receives an ON message you need to change “state” to “command”.

But beware because if your GarageLights sends an ON message to the state topic in response to that you will be stuck in an infinite loop.

Typically in this sort of situation the behavior you see is the desired behavior. When you manually trigger the Switch it will publish the ON message. But when the light reports its state only the Item’s state is updated and not triggered. This way you can keep the switch matching the state of the light without hitting an infinite loop.

1 Like

ok, i see… very useful information :slight_smile:

on question… to change the switch’s position on the server based on the switch state on the device, I should write a rule that will change it… right ?

Assuming that your remote switch reported its state to <[mosquitto:/openhab/grpGarage/swGarageLights/state:state:*:default] just adding it to your binding will manage that.

You can also (and sometimes need to) have a separate Item which gets the updates in which case yes, you would write a rule and use postUpdate to update your main switch Item.

Hi,

Thank for the info… I made a MAP translating 1=ON, 0=OFF (as the physical switch gives out ONES and ZEROS.

and it works… switch states on the server are updated with the values coming in for the physical switch :slight_smile:

Thanks !

1 Like