OpenHab MQTT Inbound and Outbound

Hello, I’m using openhab with a MQTT, and I would like to have one switch change is state when I click in the openHab GUI and when it receives a MQTT message. When I do this, Inbound and Outbound in the same switch, I create a infinite loop, because when I click the button, openhab as a subscriber picks the MQTT message and resends it, and then picks it up again, and the loop goes on and on.

I tried to use rules to controle this, but the “received command”, but I have the same problem. How can I solve this, send and receive MQTT in the same switch?

Thank you all

1 Like

You want to send a command when the user moves the switch in the GUI. For example:

>[broker:/your/topic:command:OFF:0],>[broker:/your/topic:command:ON:1]

You also want to change the state of the switch (state is different concept from command), when the far end device publishes to that topic. For example:

<[broker:/your/topic:state:MAP(binary.map)]

If you want to have the state of the switch only change when the remote switch itself changes, and not just the act of the user changing the switch in the UI, then add autoupdate="false".

So, all together, it would be something like

Switch MySwitch { mqtt=">[broker:/your/topic:command:OFF:0],>[broker:/your/topic:command:ON:1],<[broker:/your/topic:state:MAP(binary.map)]", autoupdate="false" }

Does that work for you?

7 Likes

You are my new hero :slight_smile: It worked as intended. Thank you for your help :slight_smile:

1 Like

Hi i have a temperature value that im recieving via this item.

Number ThermostatLoungeCurrentTemp “Temperature [%.1f °C]” {mios=“unit:house,device:48/service/TemperatureSensor1/CurrentTemperature”}

How to i send that value to my MQTT server i have created my topic and can send to it using MQTTLens but everything i try within my items file just fails.

number ThermostatLoungeCurrentTemp { mqtt=">[192.168.31.220:/loungetemp:state"} is one of the many attempts with just no luck.

Any help watou would be great.

Thanks

Have a look at the MQTT binding wiki to make sure you have setup the binding correctly in the .cfg file. Study the wiki until it makes sense. :wink:

Then, try something like the following item:

Number ThermostatLoungeCurrentTemp "Temperature [%.1f °C]"  { mios="unit:house,device:48/service/TemperatureSensor1/CurrentTemperature", mqtt=">[broker:/loungetemp:state:default]" }

It ought to publish a temperature value to your MQTT broker topic every time it is updated from MiOS (Vera).

Thanks Watou i do read the wiki and give them a go before asking as a last resort, a lot of the wikis just dont make sense to me they have a couple of examples etc. do you have any core openhab pages i should read to understand items. sitemaps, maps (thats my next problem im having) and their use and options?

Start with the start page of the wiki :slight_smile:
On the right hand side under “Configurations” you will find links to explanations for items and sitemaps.

What do you mean by “maps”? Google maps? Take a look here.
Or “mapping” in sitemaps? Then take a look here.

Have fun.

1 Like

I had a lot of trouble with the emoncms MQTT server but seem to be able to send via HTTP ok from a browser but an having trouble sending this from open hab is my variable cored here?

Number ThermostatLoungeCurrentTemp “Temperature [%.1f °C]” {mios=“unit:house,device:52/service/TemperatureSensor1/CurrentTemperature”, http=">[*:POST:http://192.168.31.220/emoncms/input/post.json?node=loungetemp&csv=%.1f&apikey=e9f2acc3f9f333f91111111111]" }

its not updating it correctly I’m a little unsiure about the %.1f usage I’m the http string.

Have a look at this part of the HTTP binding wiki page that shows the proper format to replace into the URL. You would want to use %2$.1f to substitute the current numeric command or state into the URL.

Sorry, where this variable is explained in the guide??

Dynamic URL enhancement

The given URL can be enhanced using the well known Syntax of the java.util.Formatter. The !HttpOutBinding currently adds to parameters to the String.format() automatically

  1. the current date (as java.util.Date)
  2. the current Command or State

To reference these values the indexed format syntax is used. A well prepared URL look like this:

http://www.domain.org/home/lights/23871/?status=%2$s&date=%1$tY-%1$tm-%1$td

Each format string starts with ‘%’ followed by an optional index e.g. ‘2$’ whereas ‘2’ is the index of the parameter arg given to the format(format, args…) method. Besides the index you have to specify the format to be applied to the argument. E.g. ‘s’ to format a String in the given example or ‘d’ to format an Integer, or ‘.1f’ to format a Float with one decimal fraction.

For anyone who copied-pasted this please make sure you have that file:

$ cat /etc/openhab2/transform/binary.map
key=value
1=ON
0=OFF
ON=1
OFF=0

I was also tearing my hair out with the following two errors in the openhab.log

given new state is NULL, couldn’t post update for ‘Sprinkler_Z1’
Cannot get service reference for transformation service of type MAP

Then I realised I had to install the Transformation add-on “Map Transformation” , then bingo all worked perfectly!

I have a sprinkler system that is at the other end of a mysensors network. I wanted the mysensors node to report back the status of the sprinkler valve rather than let openhab assume it was activated.

My Config Files

/etc/openhab2/transform/mqtt.map

key=value
1=ON
0=OFF
ON=1
OFF=0

/etc/openhab2/items/sprinkler.items

Switch Sprinkler_Z1 {mqtt=">[mosquitto:mysensors/in/100/1/0/0/2:command:ON:1],>[mosquitto:mysensors/in/100/1/0/0/2:command:OFF:0],<[mosquitto:mysensors/out/100/1/1/0/2:state:MAP(mqtt.map)]", autoupdate="false" }

Hello,

I let show me by MQTT my temperature and humidity of the living room.
Switches via MQTT work also.

But I have problems, when my Sunset Rule switches my switch item to ON, then unfortunately nothing happens via MQTT. But if I switch the switch in the sitemap by hand to ON or OFF, then MQTT is the right one.

I hope you can help me with my problem.

My items

DateTime SunsetStart_Time "Sunset Start [%1$tH:%1$tM]" { channel="astro:sun:local:set#end" }
Switch SunsetStart "Sunset Event" {mqtt=">[mosquitto:Aussen/Eingang/Licht:command:ON:1],>[mosquitto:Aussen/Eingang/Licht:command:OFF:0],<[mosquitto:Aussen/Eingang/Licht:state:MAP(binary.map)]", autoupdate="false"}

My rule

rule "SunsetNeu"
when 
    Time cron "0 * * * * ?"
then
    val dtSunset = new DateTime(SunsetStart_Time.state.toString)
    val iTimerSunsetStunde = TimerSunsetStunde.state as DecimalType
    val iTimerSunsetMinute = TimerSunsetMinute.state as DecimalType
    val iMinuteCurrent = now.getMinuteOfDay
    val iMinuteSunset = dtSunset.getMinuteOfDay
    val iMinuteTimer = iTimerSunsetStunde.intValue * 60 + iTimerSunsetMinute.intValue
   
    if ((iMinuteCurrent >= iMinuteSunset) && (iMinuteCurrent < iMinuteTimer))
    {
        logInfo("SunsetNeu", "Lichter eingeschaltet")
        postUpdate(SunsetStart, ON)
    }
    else   
    {    
        logInfo("SunsetNeu", "Lichter ausgeschaltet")
        postUpdate(SunsetStart, OFF)
    }
end

My sitemap

	Frame label="Sonnenuntergang"{
		Text item=SunsetStart_Time icon="sunset"
		Switch item=SunsetStart icon="switch"
		Setpoint item=TimerSunsetStunde	icon="clock"   minValue=0 maxValue=23 step=1
		Setpoint item=TimerSunsetMinute	icon="clock"   minValue=0 maxValue=59 step=1
	}

Sitemap:

MQTT:


(The events in the MQTT image come only when I switch the switch in the sitemap)

Hello,

I could solve the problem by myself.
The error is in the

postUpdate(SunsetStart, ON)

command.
The command

sendCommand(SunsetStart, ON)

has solved the problem and mosquitto now receives the messages.