Setting Virtual Contact Sensor state via MQTT Command

I am running into a wall with a project I am working on. I am trying to use MQTT/smartthings-mqtt-bridge to set the state of a virtual contact sensor.

Is the command ‘contact’ read-only?

I have a real contact sensor, it reports as


When i try to issue either command to a virtual contact sensor, it never changes. The smartthings sample one has a open/closed press action, but trying to set it via MQTT never changes the state.

Is there something I am missing, or does ST not allow this?

my log looks like this:

Received device event from bridge: [name:Office Window, value:close, command:true, type:contact]

Essentially I have combined rtl_433 with MQTT and a python script to re-use about 15 SimpliSafe 433mhz sensors. Everything is working except for the state of the contact changing.

edit 1: This works perfectly fine with a “Simluated Switch” so I know everything up and into SmartThings is working. I am not sure where to proceed…

What smartapp are you using to change the state of the virtual contact sensor? Also, why don’t you just use the MQTT Bridge device to set up a device that would update directly from MQTT rather than having a virtual device?

There are details on how to set up the smartthings devices over in the github for the bridge.

I think that’s what I am doing.

My config is as follows.

rtl_433 publishes an MQTT message as follows

smartthings/Office Window/contact open

MQTT Bridge is running as a device in smartthings called z_MQTT and is subscribing to all events.

The config for the MQTT device requires that I choose my devices to be updated when event shows come in via MQTT, e.g. locks, sensors, alarms, etc.

So the MQTT bridge/smartapp/device is trying to set my virtual contact sensor Office Window ‘contact’ to ‘open’

That device doesn’t seem to accept that input. If I change Office Window to a switch and issue the command ‘switch’ ‘on’ that works perfectly fine.

Is there a link to what you were referring to with making devices another way?

Okay, let me re-phrase. What DTH are you using for the “virtual contact sensor”?

I’ve tried a handful, but mainly had been trying these:

That’s because the smartapp is going to share your ST devices TO MQTT and the events you subscribe to in the bridge will be displayed in the MQTT device. If you want to then change the status of other devices, you’d have to use WebCore to do it based on the events logged in your MQTT device.

It works both directions. I am using this smart app to do so.

Note the architecture where commands can go both directions as well as device statuses.

Like I said, if I replace the “Virtual/Simulated Contact Sensor” with a “Virtual/Simulated Switch” and issue the on/off it works fine. I see the switch turn from on to off and back again in ST when I issue the “smartthings/Office Windows/switch on” command. I’ve correctly setup the MQTT Bridge and Bridge Device/SmartApp

it’s only the DTH of “Contact Sensor” that I can’t set.

Oh… Because the contact sensor should be open close not on/off.

Yes, when testing type contact I am issuing the open/close command. No status change.

When testing type switch, I’ve modified it to use on/off. Successful status change.

I think this issue has to do with ST handling of contact switches.

Try on/off with the virtual contact sensor. I know the DTH i use responds to both.

If anyone is coming across this post as I did trying to do the same thing, I found the following post with an updated smartapp that allows contact sensor state changes. Just tested it and it works great!

@Patrick_V is correct… some device types did not report BACK TO SmartThings… that’s why I changed @stjohn’s code to include things like temperature, humidity and contacts to be able to feed back to ST and update the device state accordingly. I wish my pull request (or the other similar one) would just be merged on GitHub but I believe @stjohn abandoned the MQTT Bridge a while ago — hence my fork :slight_smile:

General FYI for everyone interested in “generic” home brew devices… I highly recommend using ESPHome (with Home Assistant as well if possible). If anybody is interested in the Heltec Wifi Kit 32 code that I have for that board and the OLED that’s mounted to it… I can share it so LMK… the code has logic for 2 switches (momentary or stateful), 2 contact sensors and BME280 or DHT22 for climate stuff. Obviously the OLED is also used showing status of all of those things. I highly recommend that Heltec board for all sorts of solutions.

That makes no sense since the device is virtual and is IN smartthings. it doesn’t have to report back when it’s part of the system. You just have to use it correctly.

Also, your HomeBrew devices…how are you planning on integrating those with ST? Via a Groovy driver? Not for long you wont.

I thought I made it pretty clear but I’ll provide a bit more info. The original MQTT code that’s still on github has been somewhat abandoned. It never had contact sensors, temperature or humidity come BACK TO ST… That means ST can update MQTT on state changes for those 3 capabilities but when a message comes into MQTT from the device or some other HA system (like Home Assistant) it would never make it into ST. See the following PR’s where you can see the difference between my modifications vs. the original.

Temperature and humidity and contact sensors.

Notice the fact that this code is from 2.5 years ago and obviously with Groovy. Do you have a definitive sunset date when Groovy devices & smartapps will no longer be supported by ST?