How trigger automations on Axis MQTT events

I have some Axis cameras and need to trigger automations based on various events like motion detection and fence guard. It used to be that these sensors showed up as extra entities for the device, but somehow that doesn’t work any more. The device now has only one entity.

Fortunately, Axis supports MQTT, and I can configure it to send MQTT messages. For example, when in the home-assistant MQTT configuration, if I follow topic “my-cam/event”, I get an event like this every time someone crosses my fence:

Message 5 received on my-cam/event at 8:27 PM:
{
    "topic": "axis:CameraApplicationPlatform/FenceGuard/Camera1Profile1",
    "timestamp": 1648438035342,
    "message": {
        "source": {},
        "key": {},
        "data": {
            "active": "1"
        }
    }
}
QoS: 0 - Retain: false

My problem is that I have no idea how to trigger an automation based on this information. In particular, I’m confused by the two different topics. All the messages from this particular camera have MQTT topic my-cam/event, but I only want to do something when the topic inside the event is axis:CameraApplicationPlatform/FenceGuard/Camera1Profile1 and the data goes from "active": "0" to "active": "1".

Also, I should say that the camera does not show up under MQTT devices, even though other devices do show up. Can someone tell me how to configure this, or point me to more comprehensive documentation than just the basic home-assistant MQTT documentation, which I can’t map onto the events I’m getting? Thanks.

Try this:

  trigger:
    - platform: mqtt
      topic: "my-cam/event"
  condition:
    - condition: template
      value_template: "{{ trigger.payload_json['topic'] == 'axis:CameraApplicationPlatform/FenceGuard/Camera1Profile1' }}"
    - condition: template
      value_template: "{{ trigger.payload_json['message']['data']['active'] == '1' }}"

Thanks, that seems to work and is super helpful. Now I actually have multiple triggers on this automation, and was kind of hoping to put the conditions in the trigger, rather than the conditions (which would apply to all triggers). Is there any way to use the Payload field of the MQTT automation trigger? If not, it isn’t a huge deal because I can just have one automation activate another.

I’m not sure if this will work:

  trigger:
    - platform: mqtt
      topic: "my-cam/event"
      value_template: "{{ (value_json['topic'] == 'axis:CameraApplicationPlatform/FenceGuard/Camera1Profile1') and (value_json['message']['data']['active'] == '1') }}"
      payload: 'true'

Yes, that new trigger works perfectly. Thank you so much! What can I read to be able to figure stuff like this out myself?

This

And this:

Also searching the forum for examples always helps.

This would suggest something not working properly when mqtt is enabled. This would be an issue with the integration as it switches over to use mqtt if that is configured.

Yes, it’s definitely the case that the Axis integration broke on me, but it wasn’t caused by enabling MQTT. I only enabled MQTT after I gave up on figuring out what was going on with the Axis integration, as a workaround. Arguably, this isn’t the “right” way to solve the problem. However, it seems more robust, since a lot of things can use MQTT, so if I figure out how to make MQTT automations work I won’t have to worry as much about other integrations.

Also, I figured a lot more people use MQTT than Axis cameras, so it would be better to ask about MQTT. Should anyone want to help me debug my problems with the Axis integration, of course, I’d be delighted to provide whatever information is helpful. The main thing is that now there’s only one entity for the Axis device, even if I remove and re-add the device, and even if I rename the device (which some people suggested might help).

I will revisit most aspects of the Axis integration some time in the near future, there are a lot of things to do. Can you report an issue on the Axis integration on HASS github? Helps tracking issues better than through the forums.

So now it seems to be working and I’m not sure what changed. I had a bunch of old entities with a red “restored” icon that were disabled. After deleting all entities except the camera, then when I sent an event like a fence crossing, it created a new working entity.

How did you connect thecamera to homeassistant?
Running MQTT by itself in a seperate container or using the builtin mqtt in HASSOS ?
Im on a raspberrypi, with hassos, and zigbee2mqtt installed, and mqtt broker - how to configure the axis cam 192.168.200.11 and homeassitant 192.168.201.175 ?

Please share your inspiration if still available.