Controlling services with exec binding

Hi everyone

I’m new here and I’ve been running OpenHab for quite a while with some Z-Wave components, presence detection using a Cisco Meraki AP with their API, and a few MySensors sensor with a MQTT gateway.

I’ve been trying to control some custom services from OpenHab, and I’m having some issue getting the service status back. I can start and stop the service, but how can I get a status from OpenHab?

For instance, I have a python script for my Meraki AP which periodically reports detected mobile devices. This is the running process:
3359 1.5 2.6 84540 24796 ? Ssl 21:01 0:02 /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/monitor /opt/meraki/cmx-api.js

How can I update this switch with the actual service status (ON or OFF)??

Switch Service_MERAKI “Meraki CMX” (Services) { exec=“OFF:/etc/init.d/meraki-cmx stop,ON:/etc/init.d/meraki-cmx start”}

Thank you!

If you are on a system running Upstart (e.g Raspian) and your service is configured to use Upstart you can run:

service meraki-cmx status

and parse the output. If it starts with “[FAIL]” the service is not running. Otherwise it will start with “[ ok ]”.

If you are on a system using systemd (e.g. the latest Ubuntu) you can run:

systemctl is-active meraki-cmx

which returns “active” if it is running and “inactive” if it isn’t.

Finally, you can run

ps -ef | grep meraki-cmx

and make sure you get two lines back (one for the grep).

You will want to make sure your Item is configured for an input and you will need to write a transformation rule. See the Exec binding wiki page and the Samples Rules to get started.

Rich

Hey Simon,

probably it’s much too late after 3 years, but I’ve got it up and running with the new Meraki CMX location and scanning API v.2.0. The Meraki cloud manager pushes the scanning results to one of my post URLs.

The endpoint for the corresponding post URL is based on a Meraki CMX node triggering a Node-Red-flow to publish the scanning results, WLAN and BLE clients, to my private MQTT broker.

The item definition looks like:
Switch deviceSeenMyPhone "Your mobile phone" { mqtt="<[<YourMqttBroker>:meraki/DevicesSeen/<WirelessDeviceMAC>:state:JS(DeviceSeen.js)]", expire="5m,state=OFF" }

While the initial discovery of just recently connected devices is published within about 2 minutes, the status updates of already connected devices might take up to 4 or 5 minutes. The Meraki CMX API does not push disconnect events so I use the expire binding to set the presence status back to “OFF” after 5 minutes.

Regards,
Michael

1 Like

I’ve just created a similar workflow in node red using Meraki’s CMX API. I have mine watching for 3 WiFi MAC addresses (iPhones of the family), and sending an MQTT message with a topic of “meraki/DevicesSeen/Ed” (for my phone) to Mosquitto. I haven’t quite finished the switch and binding in OpenHAB for this. I was curious what your “set msg topic” logic looked like, as well as your DeviceSeen.js script.

Hey Ed,

thank you for your feedback. The node “set msg.topic”, it’s a “change” node, sets the “msg.topic” using JSONata to this value: “meraki/” & msg.type & “/” & $replace(msg.payload.clientMac, “:”, “”). I suppose that will be the part you have to modify to fit your topic naming convention.

I created a file namen “DeviceSeen.js” in the “transform” directory, to let a switch item interpret the mqtt topics’ messages likes this:

(function(json){
    var response = JSON.parse(json);
    if (response.ipv4) {
        return "ON";
    }
    else {
        return "OFF";
    }
    })(input)

image

The script will return “ON” if the device reports a valid IPv4 address, otherwise it will return “OFF”.

Hopefully I could give you some hints to procedd.

Good luck.

Regards,
Michael

Ahh btw. The flow will report BLE beacons as well, if you switched on BLE client scanning on your Meraki APs. In that case the CMX scanning API will report such devices like this.
image

There ist no IPv4, so you would have to switch on an item if a special devices gets reported. If the item will not appear within about 2 up to 5 minutes consider the item to be out of range. The time period is related to the Meraki stuff and I know no way to adjust this period. I’m working on ESP32-based IoT BLE scanners to reduce the period to 30 secs, but the device will consume some power cause it’s scanning all the time.

Bye,
Michael

Thank you!