Mqtt binding not triggering when state is changed via rules

Hi folks,

Probably a noob question. I’ve got a switch bound to an mqtt trigger. If I use the GUI the mqtt binding is triggered and the state is published to the mqtt broker. However if the state of this switch is changed via a rule I’ve got the mqtt binding doesn’t seem to get triggered. The rule I have is watching the Item temperature and turning on or off the heater & cooler switches.

How do I get the mqtt binding triggered when I change the state based on rules?

My Items config looks like this:
Switch Heating_GF_Kitchen "Kitchen heater" <heating> (GF_Kitchen,Heating) { mqtt="> [mosquitto:/myhouse/kitchen/Heating_GF_Kitchen:command:ON:1],>[mosquitto:/myhouse/kitchen/Heating_GF_Kitchen:command:OFF:0]" } Switch Cooler_GF_Kitchen "Kitchen cooler" <heating> (GF_Kitchen,Heating) { mqtt=">[mosquitto:/myhouse/kitchen/Cooler_GF_Kitchen:command:ON:1],>[mosquitto:/myhouse/kitchen/Cooler_GF_Kitchen:command:OFF:0]" }

My rules file looks like this:
`import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

var int tempHigh = 26
var int tempLow = 25

/* Turn on Kitchen heater when temp is below tempLow */
rule "Control Kitchen heater"
when
Item Temperature_GF_Kitchen received update
then
if (Temperature_GF_Kitchen.state < tempLow)
postUpdate(Heating_GF_Kitchen, ON)
else if (Temperature_GF_Kitchen.state >= tempLow)
postUpdate(Heating_GF_Kitchen, OFF)
end

/* Turn on the cooler if above tempHigh */

rule "Control Kitchen Cooler"
when
Item Temperature_GF_Kitchen received update
then
if (Temperature_GF_Kitchen.state > tempHigh)
postUpdate(Cooler_GF_Kitchen, ON)
else if (Temperature_GF_Kitchen.state <= tempHigh)
postUpdate(Cooler_GF_Kitchen, OFF)
end`

Thanks in advance.

Regards
Kheeran

(Unwanted space between > and [.)

Change your rules to sendCommand instead of postUpdate, since your items are bound to publish on topics when commands are received, not when state is changed.

Thanks watou, that was the bit of my understanding that was missing. It works now.

1 Like