MQTT Setup new Code

I decided to rewrite my Arduino code in order for it to be more compatible with the new MQTT binding. I am still learning the ropes and there seems to be an error that I just cant figure.

My current setup ;

Type switch : relay3 "Braai Light" [ stateTopic="homie/megarelay/relay-state/r3", commandTopic="homie/megarelay/relay/r3" ]

Switch relay3 "Braai Light" (out_lights,Lights) { channel="mqtt:topic:mainBroker2018:relays:relay3" }

Switch item=relay3

so currently the cmnd to switch on the relay can be
1 for on
0 for off
2 for toggle
on the topic : homie/megarelay/relay/r3
it will then respond with the state of the relay with
1 for on
0 for off
on the topic; homie/megarelay/relay-state/r3

if I switch the relay via the sitemap it turns on but I get the following error ;
2019-01-03 12:59:11.333 [ome.event.ItemCommandEvent] - Item ‘relay3’ received command OFF

2019-01-03 12:59:11.345 [nt.ItemStatePredictedEvent] - relay3 predicted to become OFF

2019-01-03 12:59:11.359 [vent.ItemStateChangedEvent] - relay3 changed from ON to OFF

2019-01-03 12:59:11.364 [vent.ItemStateChangedEvent] - solarwatt changed from 776.8699951171875 to 742.0

==> /var/log/openhab2/openhab.log <==

2019-01-03 12:59:11.379 [WARN ] [eneric.internal.generic.ChannelState] - Command 'off' not supported by type 'OnOffValue': No enum constant

2019-01-03 12:59:11.605 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Text

Also if I switch the relay externally from the state does update but not via the UI page ( I need to refresh the page to see the state of the relay)
Now I assume that there is a problem with the format of my mqtt topic’s and or the payloads but even if I change it to “on” / “off” instead of 1 and 0 it does not make any difference.

Any ideas?

The error in the log is coming from your sitemap. You have an Item on your sitemap displayed with a Text element that doesn’t exist.

The warning is because a Switch needs ON or OFF and cannot handle on, or off, or 1, or 0. What ever you changes to convert the 0 to off needs to be adjusted to use OFF.

1 Like

You can learn a lot by doing it yourself, for sure. But especially Homie has some ready to use implementations.

Those topics are very un-Homie like. There is a validator on the website :slight_smile:

Thank you for the response David.
I have read through the Homie pages and was under the impression that it was a mqtt structure that was important but I realize that it is quite a bit more involved.
Unfortunately its above my average “coding” skills so it will need to be placed on the shelve for now until I have sorted the rest of the System.

Don’t miss the part from David’s post that they’re are libraries. You don’t have to code it yourself.

Made a hash for replieing to the previuos post

Sorry Rich - I replied to the wrong post grrrr

Not yet and when I do it will be in Python.

Cool , was worth a shot.
Just for fun why do you prefer Python?

Well, I have a Python script that runs on an RPi that lets me do stuff like scan for nearby Bluetooth devices, sense gpio pin states, change gpio pin states, etc. I use mqtt to get readings and control actuators from OH. I have it posted here :

I plan on implementing Homie so they can be automatically discovered.