MQTT Camera for Home Assistant

This is how you add an INSTAR Full HD MQTT camera to Home Assistant.

MQTT Broker

You start by configuring the MQTT broker inside the cameras webUI under Network. Here you can set:

  • MQTT Server Address: Leave at 127.0.0.1 to use the internal broker
  • MQTT Server Port: Default 1883, adjust if needed
  • Username /Password: Set your own login

Now we can add our MQTT broker to Home Assistant under Settings / Integrations / MQTT via the Lovelace UI. The server address is the IP of our INSTAR Full HD camera. Add the port and login you choose in the earlier step.

MQTT Interface

The complete MQTT API is documented here. It basically mirrors the cameras webUI. E.g. to switch the motion detection areas you have to navigate the webUI to Alarm / Areas. The MQTT topic to switch Area 1 on or off is alarm/area1/enable. The payload of this topic is:

  • {“val”:”0”} /switch off
  • {“val”:”1”} /switch on

Since we can have more than one camera on our network, we also need to add an ID for the camera we want to contact. This ID always starts with instar followed by local (to contact the camera that runs the MQTT broker), all (to send the message to all INSTAR cameras on the MQTT network) or the LAN MAC address of a camera to target it specifically:

  • instar/local/alarm/area1/enable
  • instar/all/alarm/area1/enable
  • instar/000389888811/alarm/area1/enable

And one more thing – every of those, so called COMMAND Topics, also has a corresponding STATE Topic:

  • instar/local/alarm/area1/enable
  • instar/local/status/alarm/area1/enable

We use the first one to update / switch something on our camera and – once this happened successfully – receive a state update to set the state of our Lovelace UI switch.

Lovelace UI Switches

Now we want to create switches that allow us to control our camera from Home Assistant. Create a file named switches.yaml next to the main configuration.yaml file in the .homeassistant folder and import it into the latter switch: !include switches.yaml . Now add the following content to the switches file:

- platform: mqtt
  name: 'Alarm Area 1'
  state_topic: 'instar/local/status/alarm/area1/enable'
  command_topic: 'instar/local/alarm/area1/enable'
  qos: 1
  payload_on: '{"val":"1"}'
  payload_off: '{"val":"0"}'
  optimistic: false
  retain: false
- platform: mqtt
  name: 'Alarm Area 2'
  state_topic: 'instar/local/status/alarm/area2/enable'
  command_topic: 'instar/local/alarm/area2/enable'
  qos: 1
  payload_on: '{"val":"1"}'
  payload_off: '{"val":"0"}'
  optimistic: false
  retain: false
- platform: mqtt
  name: 'Alarm Area 3'
  state_topic: 'instar/local/status/alarm/area3/enable'
  command_topic: 'instar/local/alarm/area3/enable'
  qos: 1
  payload_on: '{"val":"1"}'
  payload_off: '{"val":"0"}'
  optimistic: false
  retain: false
- platform: mqtt
  name: 'Alarm Area 4'
  state_topic: 'instar/local/status/alarm/area4/enable'
  command_topic: 'instar/local/alarm/area4/enable'
  qos: 1
  payload_on: '{"val":"1"}'
  payload_off: '{"val":"0"}'
  optimistic: false
  retain: false

Back on the Lovelace UI click to add a new Entity Card and as entities select the switches you just added:

  • switch.alarm_area1
  • switch.alarm_area2
  • switch.alarm_area3
  • switch.alarm_area4

Lovelace UI Push Buttons

In some cases we don’t need switches but stateless buttons, for example to manually trigger an alarm on our camera or have it move between preset positions. This can be done with a script. First create a file called scripts.yaml and import it into the main configuration script: !include scripts.yaml . Open this file and add the following lines:

9010_pushalarm:
  sequence:
    - service: mqtt.publish
      data_template:
        topic: instar/local/alarm/pushalarm
        payload: '{"val":"1"}'
        qos: 1
9010_gotopos1:
  sequence:
    - service: mqtt.publish
      data_template:
        topic: instar/local/features/ptz/preset
        payload: '{"val":"0"}'
        qos: 1
9010_moveright:
  sequence:
    - service: mqtt.publish
      data_template:
        topic: instar/local/features/ptz/move
        payload: '{"val":"right"}'
        qos: 1
9010_movestop:
  sequence:
    - service: mqtt.publish
      data_template:
        topic: instar/local/features/ptz/move
        payload: '{"val":"stop"}'
        qos: 1

The first one of those topics triggers an alarm on your camera, the second turns your camera into preset position 1, the third has it moving horizontally to the right and the fourth stops the movement.

Just like with the switches add another entity card to the Lovelace UI and select those scripts as entities from the drop down menu.

Automations

Please refer to the tutorial on Github for automation examples like having your camera automatically turn between a day and night position and using different motion detection areas depending on the position your camera is in.

2 Likes

On all Full HD camera models with the current firmware you can now set the alarm server interval via CGI command. The default value is 60s:

/param.cgi?cmd=setmdalarm&-aname=server2&-switch=on&-interval=60

A reduction down to one second is possible - but we recommend to leave it at least 10-15s (if not needed otherwise on the smarthome side). For example, to set the interval between alarm triggers for a camera with the IP 192.168.178.102 and the HTTP port 80 to 15s

http:// 192.168.178.102:80/param.cgi?cmd=setmdalarm&-aname=server2&-switch=on&-interval=15

The alarm server is used to inform Home Assistant when motion is detected by the camera.

The INSTAR MQTT service is now regularly available via the System/Update menu and some improvements/bugfixes have been added:

  • Further MQTT Topics added (e.g. for step-by-step control of the camera’s P&T)
  • User logins are now excluded from MQTT state topics (can only be set via command topic)
  • All special characters, which are allowed for the camera login, can now also be used for MQTT.
  • The ports used for the MQTT service were changed to the default 1883/8883
  • In the beta version, no distinction was made between “local” and “all” in topics. The first now only addresses the camera on which the broker is running and the latter addresses all cameras in the MQTT network
  • The use of own SSL certificates for the MQTT service was simplified. We already have a guide for self-signed certs online, for CA Certs (Let’s Encrypt) will follow shortly. But I did not yet look into how to get this to connect with Home Assistant - if someone has experience with using MQTT (TLS) please let me know :slightly_smiling_face: In Node-RED you simply upload the public key and you are done

The update also brings the extended alarm server configuration - so you no longer have to set intervals by CGI command (as described above).

Ok, it is just as easy to use your own self-signed certificate with Home Assistant. All you need to do is to upload your public key and add it to your HA configuration:

configuration.yaml

mqtt:
  broker: 192.168.2.117
  port: 8883
  certificate: ./mqtt/client.pem
  client_id: homeassistant
  username: !secret mqtt_username
  password: !secret mqtt_password
  tls_insecure: true

secrets.yaml

mqtt_username: admin
mqtt_password: instar

I tested it with all allowed special characters - !#()*-./<?@[]^_{|} - for the MQTT broker login and works just fine:

mqtt_username: admin
mqtt_password: '!#()*-./<?@[]^_{|}'

Day Night Position

My pan&tilt camera should use one preset position during the day, keeping an eye on my front door when I am at work. But use the second preset position to watch my car in the carport when I am at home.

I need to configure different motion detection areas (different areas of interest for the software-based motion detection) for both preset positions.

scripts.yaml

To do this we will first add a prototype service to our scripts.yaml file. This service should be able to receive a MQTT Topic (target) and MQTT Payload (message) from an automation in the automations.yaml file:

# MQTT Publish Service

send_mqtt_command:
  sequence:
    - service: mqtt.publish
      data_template:
        topic: "{{ target }}"
        payload: "{{ message }}"
        qos: 1

automations.yaml

Then switch to the Lovelace UI, open the Configuration Panel and select Automation:

Day Set

As a trigger we can use the Sunrise - add an offset to optimize the timing for your camera location:

Now we have to add the MQTT topics to activate Alarm Detection Area 1 & 2, while deactivating Areas 3 & 4 as well as moving our camera into preset position 1. Note that when you select Action Type Call Service you can now select the script.send_mqtt_command that we created before from the list of services:

My camera has the MQTT ID 10D1DC218F96 - make sure to substitute this with your cameras ID:

message: '{"val":"1"}'
target: instar/10D1DC218F96/alarm/area1/enable

message: '{"val":"1"}'
target: instar/10D1DC218F96/alarm/area2/enable

message: '{"val":"0"}'
target: instar/10D1DC218F96/alarm/area3/enable

message: '{"val":"0"}'
target: instar/10D1DC218F96/alarm/area4/enable

message: '{"val":"0"}'
target: instar/10D1DC218F96/features/ptz/preset

The Night Automation is the opposite - triggered by Sunset and deactivates Areas 1 & 2, while activating Areas 3 & 4 and moving to preset positon 2.

Adding your camera’s Live Video

Home Assistant comes with a Camera Module that we can use to add our camera’s Live Video. Lets start by accessing the configuration.yaml. Open the configuration file and add the line:

camera: !include cameras.yaml

Now create the cameras.yaml file next to the configuration file and add the following lines:

- platform: mjpeg
  name: '117'
  mjpeg_url: http://192.168.2.117:80/mjpegstream.cgi?-chn=11
  verify_ssl: false
  authentication: 'basic'
  username: 'admin'
  password: 'instar'

This will add a camera with the IP 192.168.2.117, HTTP port 80 and the user login admin / instar. In case you need to restrict the bandwidth this live stream is requiring, use chn=12 or chn=13 instead of chn=11.

Now test your configuration and reload Home Assistant to be able to see the changes you just made. Once the UI is back up, click on Configure UI in the top right of your dashboard. Then click on the big + Button to add a new card. Here we have to add a new Entity and select the camera that we just added:

Save the entity to return to your dashboard. Clicking the camera card will now open your cameras live video:

The MQTT Service has been updated and the update can be downloaded and installed from the camera webUI. The most requested features that were added are:

  1. MQTT topic prefix and device ID can now be set in the webUI
  2. The MQTT Alarmserver now returns to an idle state 5s after a trigger event (payload 0)
  3. The camera now adds a last will that will be published by your broker if the connection between your camera and the broker is severed for more than 60s.

Changelog:

INSTAR Full HD WebUI Update

What’s new in version 3.1 (347) ?

  • MQTT adapter was revised
    • MQTT prefix and camera ID can now be set by the user
    • After the alarm is triggered (after 5s), the alarm server automatically returns to an idle state (with payload 0).
    • The camera now leaves a Last-Will Topic with your MQTT broker. Should the camera lose connection to the broker, the broker will send the LWT to all subscribers.
    • The device ID with which the camera registers with the broker was previously generated randomly. As a result, certain smarthome systems have detected the camera as a new device after each reboot. The Device ID is set equal to the Camera ID (settable in the MQTT menu).
    • General bug fixes and improvements
  • Alexa Support - INSTAR Cloud Skill now available with which you can play recordings from your cloud account as well as directly accessing your individual cameras and have Alexa control your camera.
  • IFTTT Support - On the IFTTT Platform, we provided an INSTAR Applet. The INSTAR Applets provides a way for you to control some settings of your INSTAR Camera as well as your INSTAR Cloud account through IFTTT scripts.
  • In order to use Alexa and IFTTT you have to create a free INSTAR Cloud account. Sign-up NOW and enjoy all paid features for 30 days without any costs.
  • For Alexa and IFTTT a new sub-category called Smarthome has been added to your cameras webUI. Alarm server and MQTT settings have been moved to Smarthome.
  • General bug fixes and improvements