How does OH2 Work

I was wondering if someone could explain, in a fairly high level way (not too technical) how OH2 works.

I currently have a working OH2 system with various wireless (SONOFF) devices,

For this excercise lets pretend I only have one working device.

i have a physical lamp with a sonoff attached, i also have an item defined in the items file

Switch Lamp  "Lamp"  <light> [ "Switchable" ] {mqtt=">[broker:cmnd/sonoff-12/power:command:OFF:0],>[broker:cmnd/sonoff-12/power:command:ON:1],<[broker:stat/sonoff-12/POWER:state:default]"}

When you click ON/OFF in the OH2 UI the lamp switches ON & OFF.

  1. How does OH know which MQTT command to process ON or OFF
  2. what happens when the “<” MQTT command is processed.

Thanks in advanced

I recommend reading

These docs are about as high a level as is possible to fully explain the all the parts.

  1. Because that is what you configured that Item to do.

  2. The state of the Item is updated to match the state sent by the Sonoff. This keeps your Item in sync with the Sonoff if you press the button to toggle the switch.

1 Like

I don’t think I explained it properly.

With point 1 what I was meaning was when in the OH2 UI and I click a switch ON does OH look for an ON command in the items file. Is ON for want of a better word a reserved word. If not how does it distinguish between ON and OFF mqtt commands

You defined that command to use with your binding param command:ON.
ON command is send to the internal bus and received by mqtt binding, which gets respective instructions from your config.

OH is made up of layers.

  1. The UI processes the mouse click and executes some JavaScript code.

  2. The JavaScript code makes a REST API call to send the ON command to the Item that the UI element represents.

  3. openHAB receives the command and changes the state of the Item

  4. openHAB publishes the fact that the Item received an ON command on the Event Bus

    a. The MQTT Binding (in this case) receives the fact that an Item with an MQTT config received an ON command

     i. The MQTT Binding uses the Binding Config String on that Item to determine the broker, topic, transformation that needs to be applied, and whether or not it needs to publish a message in response to that ON command

    b. The Rules engine triggers any Rules that are configured to trigger on a change, update, or command on that Item

    c. The Persistence engine saves the state to the database if configured to do so.

    d. I’m not sure on this one but the UIs may receive an event indicating the Item changed state based on this event.