How could I control a systemd service with openhab?

  • Platform information:
    • Hardware: Raspberry 2 B ARM v7 CPU/1GiB RAM/1GiB storage
    • OS: Raspbian 9
    • Java Runtime Environment: OpenJDK 1.8
    • openHAB version: 2.4.0

I have a Raspberry Pi running openHAB. It is also running motion which is managing a single RTSP security camera. I would like to be able to toggle this camera on and off via openHAB (or at least disable motion and therefore disable recording, which is what I’m describing below).

My thinking is that I could use systemd to toggle it via openHAB. I guess there’s two parts to my question. First - should I even do this or is there a better way? Second, how can I do it?

My thinking for what I’d do is:

  1. Grant openHAB access to manage the motion systemd service
  2. Add a switch which displays on/off depending on whether the service is on (not sure how to do this bit)
  3. Activate/deactivate motion via a rule attached to said switch

I like the idea of doing this via systemd because I think it could be useful in other situations. But I’m not set on it if there’s a better way.

There is a better way:

Switch CamVideo_1  { http=">[ON:GET:] >[OFF:GET:]" }

You can simply send your commands via http binding.

Btw, it is also possible to send command from motion to openHAB through REST:

on event start config:

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" ""

You need a virtual/proxy switch item for Motion_Event_frontcam .This gives you ON if motions occurs. You can do the same thing with OFF, of course.

If you have motion on the same server as openHAB make sure you change the motion (or openHAB) ports accordingly, as motion config options are served through port 8080, the default openHAB port.

Thanks @sihui. I didn’t know about the motion REST API.

That gets me about 90% there so I’m using it for now.

I did end up expanding it a little so it would correctly determine the status if motion detection was enabled/disabled externally. So I have this in a file called cameras.items:

Switch Switch_CamVideo_Kitchen  { http=">[ON:GET:http://motion.localdomain/0/detection/start] >[OFF:GET:http://motion.localdomain/0/detection/pause] <[http://motion.localdomain/0/detection/status:15000:MAP(]"}

and a corresponding file:

Camera\ 0\ Detection\ status\ ACTIVE\n=ON
Camera\ 0\ Detection\ status\ PAUSE\n=OFF

There is still an issue I’m not sure I can solve using the API. It looks like I can stop the camera from saving files on motion events, but I want to stop it from streaming as well. I.e. when the camera is “off” it shouldn’t be possible to visit the page motion provides for viewing the camera. The API does have a quit action for the camera, but that seems to stop motion altogether so it can’t be turned back on without restarting the service.

I think that is not possible. My solution is a bit of a fake: a virtual switch with a visibility tag on the sitemap:

Switch CamControl_1_Proxy (gRestore)

Video url="" encoding="mjpeg" visibility=[CamControl_1_Proxy!=OFF]

It still streams but you won’t see it :sunglasses: