OpenHAB v3 Rules for your WQHD INSTAR MQTTv5 Broker

MQTT Binding Rules & Scripts

A: Part 1 of this tutorial installed the OpenHAB MQTT binding and connected it to the internal broker in our INSTAR WQHD camera using the OpenHab 3 Main UI. The Part II did the same by editing the configuration files of our OpenHab 3 installation. Now it is time to add some automations using the OpenHab Rules feature.

Rules

Rules are used for automating processes: Each rule can be triggered, which invokes a script that performs any kinds of tasks, e.g. turn on lights by modifying your items, do mathematical calculations, start timers etcetera.

In camera surveillance time-of-day is often an important trigger to pan your camera to a different position, change the sensitivity of the motion detection, etc. Here we can use CRON trigger that fire at a specific time of the day:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

configuration: {}
triggers:
  - id: "2"
    configuration:
      time: 18:00
    type: timer.TimeOfDayTrigger
conditions: []
actions:
  - id: "1"
    configuration:
      itemName: PrivacyAreas
      command: OFF
    type: core.ItemCommandAction

A more elegant option is the Astro Binding that allows us to use the sunrise or sunset as triggers, as it is often the changing light condition that require the change in camera configuration. To use this function go to Settings/Bindings and install Astro:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

We now need to create a Thing from the Sun Data we are now receiving from the Astro Binding:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

This Thing comes pre-configured with a Channels for the Sunrise and Sunset that we can use:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

Now, instead of using the UI, we can write down our rule - since we only have the Privacy Areas toggle, let's switch the privacy mode on once sunrise begins and turn it off again at the end of sunset:

rules/astro_scenes.rules

rule "Astro Privacy ON"

when
      Channel 'astro:sun:home:rise#event' triggered START or Item  TEST received command ON
then
      PrivacyAreas.sendCommand(ON)
      logInfo("Astro Privacy, astroScenes.rules", "Info message :: Privacy ON")
end

rule "Astro Privacy OFF"

when
      Channel 'astro:sun:home:set#event' triggered END or Item  TEST received command OFF
then
      PrivacyAreas.sendCommand(OFF)
      logInfo("Astro Privacy, astroScenes.rules", "Info message :: Privacy OFF")
end

OpenHAB with your WQHD INSTAR MQTTv5 Broker

So that I do not have to wait for sunset or sunrise to see if the rule set is working, I added a second trigger with an or - a simple Switch Item:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

Toggling this switch now toggles the PrivacyAreas switch that will turn the privacy mode on and off. This debug switch can be removed once you verified that it is working.

DEBUGGING: If something isn't working right away and you set up OpenHab as described in this tutorial, you can check the OpenHab Log with tail -f /opt/openhab/openhab_userdata/logs/openhab.log.