OpenHAB v3 Blockly Scripts 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 Scripts feature.

Scripts

For slightly more complicated triggers we can also use Javascript or the assembly block version of it called Blockly. Before I start I am going to install the MQTT Block Library that will allow us to use our camera's MQTT interface to publish configuration updates:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

We can also use the OpenHab Rules to connect our script to an item - allowing us to trigger it manually with a dummy switch:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

I can add this switch as a trigger for both the ON and OFF state:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

And as Action we now have to choose Run Script:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

Here we can select the scripting language we want to use - choose Design with Blockly:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

This will open the Blockly Editor:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

Here we can create the same function we build earlier using the Things, Channels and Rules. As a trigger we will check if our Test-Button has the state ON or OFF. If this is true I want to publish updates to the Privacy Masks topics and activate or deactivate them all at once:

OpenHAB with your WQHD INSTAR MQTTv5 Broker

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: MqttTest
      command: ON
    type: core.ItemCommandTrigger
  - id: "3"
    configuration:
      itemName: TEST
      command: OFF
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="controls_if" id="cSbJV5#tLW22bEPP#K,R" x="-111" y="-459"><mutation
        elseif="1"></mutation><value name="IF0"><block type="logic_compare"
        id="6kg-ekZ-uf22de+9R$Eb"><field name="OP">EQ</field><value
        name="A"><block type="oh_getitem_state" id="H8Q{j,gYqdU(H(%W`q{i"><value
        name="itemName"><shadow type="oh_item" id="Fm@Ca0#CJ(+RR]CS,1#C"><field
        name="itemName">TEST</field></shadow></value></block></value><value
        name="B"><block type="text" id="h?oaJtVOKd,_:O7az7L|"><field
        name="TEXT">ON</field></block></value></block></value><statement
        name="DO0"><block type="mqtt_publish" id="LH#Ceyo=C)p6kSqWRENK"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id="Vg|L$du5$O`+t5@oji6U"><field
        name="TEXT">1</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="_.q@u{=@x]E8#-o7eEaS"><field
        name="TEXT">cameras/115/multimedia/privacy/region1/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="nm[U)Yo0oW=$LWm9k1C~"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value><next><block
        type="mqtt_publish" id="]N@-[=+R7QJW+}e1i}{4"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id="UZOJ*R(wnI](:_v+MF$|"><field
        name="TEXT">1</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="#_LE`Q-C!vb;l~Z4#sFZ"><field
        name="TEXT">cameras/115/multimedia/privacy/region2/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="`ARr3SX6FJPI_mI@HvwF"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value><next><block
        type="mqtt_publish" id="l6z|Bfjy=gw)mn^#Ge$Q"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id=")dN3H}?E!Vy)k-PX0i-S"><field
        name="TEXT">1</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="XR@I55_PYr6wNnZsY,=e"><field
        name="TEXT">cameras/115/multimedia/privacy/region3/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id=":C!gTth1Q;-eXuK*}NME"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value><next><block
        type="mqtt_publish" id="FFH0!nzU=]}vHQ27aLU+"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id="xBCTX?W][){iIBw$c/s4"><field
        name="TEXT">1</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="k}-3zEP{6s8}$J)~a|tJ"><field
        name="TEXT">cameras/115/multimedia/privacy/region4/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="0QQ%UDrhFFv83|^-;PIP"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value></block></next></block></next></block></next></block></statement><value
        name="IF1"><block type="logic_compare" id="CFBz8y(CANbJ~6JvCLDz"><field
        name="OP">EQ</field><value name="A"><block type="oh_getitem_state"
        id="hi`bv3dO@kDX%hjx?`le"><value name="itemName"><shadow type="oh_item"
        id="~A@HRXzSkA]*7U~_=O9w"><field
        name="itemName">TEST</field></shadow></value></block></value><value
        name="B"><block type="text" id="Rw~iwC%:oc6Y3_u+gUt1"><field
        name="TEXT">OFF</field></block></value></block></value><statement
        name="DO1"><block type="mqtt_publish" id="^x4{d9[T=:9OaN0RzIpI"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id="QetksPK5^XJ]5oz,mVE~"><field
        name="TEXT">0</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="%MECdsP`[Q?D@?e:1+kP"><field
        name="TEXT">cameras/115/multimedia/privacy/region1/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="=WE-0+QfYl8~J4]:1IfI"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value><next><block
        type="mqtt_publish" id="%Fd4l:Ueh-T^@a)ke2L="><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id=")~ogNT6#T:ytXIg8|`^k"><field
        name="TEXT">0</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="lBbVb6}WtmH;8,QM|KA^"><field
        name="TEXT">cameras/115/multimedia/privacy/region2/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="@$/#Z}f;AJK;)mJ**0i_"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value><next><block
        type="mqtt_publish" id="~uig{I~X1z9S,0DK,t1t"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id=";;6(@Czch6^Y!uo/N+oD"><field
        name="TEXT">0</field></shadow></value><value name="TOPIC"><shadow
        type="text" id="NGvpIToFT=|:U}ui4?:`"><field
        name="TEXT">cameras/115/multimedia/privacy/region3/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="-Q!n!(g!*,tY.kp-oPMG"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value><next><block
        type="mqtt_publish" id="$U[Wo=635Y-lNQbkGnD8"><field
        name="RETAIN">FALSE</field><value name="MESSAGE"><shadow type="text"
        id="$aQt[J!qmmgh[tW?G0RF"><field
        name="TEXT">0</field></shadow></value><value name="TOPIC"><shadow
        type="text" id=",y$q4b6CO2wCSM29~VT9"><field
        name="TEXT">cameras/115/multimedia/privacy/region4/enable/raw</field></shadow></value><value
        name="BROKER"><shadow type="oh_thing" id="wUKer5jv(80p1hn?l5Cv"><field
        name="thingUid">mqtt:broker:cameraBroker115</field></shadow></value></block></next></block></next></block></next></block></statement></block></xml>
      type: application/javascript
      script: >
        var things = Java.type('org.openhab.core.model.script.actions.Things');



        if (itemRegistry.getItem('TEST').getState() == 'ON') {
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region1/enable/raw', '1', 'FALSE' === 'TRUE');
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region2/enable/raw', '1', 'FALSE' === 'TRUE');
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region3/enable/raw', '1', 'FALSE' === 'TRUE');
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region4/enable/raw', '1', 'FALSE' === 'TRUE');
        } else if (itemRegistry.getItem('TEST').getState() == 'OFF') {
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region1/enable/raw', '0', 'FALSE' === 'TRUE');
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region2/enable/raw', '0', 'FALSE' === 'TRUE');
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region3/enable/raw', '0', 'FALSE' === 'TRUE');
          things.getActions('mqtt', 'mqtt:broker:cameraBroker115').publishMQTT('cameras/115/multimedia/privacy/region4/enable/raw', '0', 'FALSE' === 'TRUE');
        }
    type: script.ScriptAction

Now all you have to do is to toggle the test switch to either activate or deactivate all privacy masks! You can now take control over every MQTT camera that is connected to your MQTT broker. Replace the MQTT Topic with any topic you want to use. And of course you can also replace the triggere with Item channels or other automations.