NetAlertX notification automation with json correctly parsed

Hello! I just figured out how to parse the NetAlertX json correctly when using a webhook and wanted to share my automation for those who are using NetAlertX and want notifications for new devices that connect to their networks. This automation only looks for new devices and is not designed for every event that happens within NetAlertX. The automation also includes a disabled persistent notification that can be enabled to debug the json payload.

alias: Notify - NetAlertX Notifications
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
    local_only: true
    webhook_id: "your_secret_webhook_id"
conditions: []
actions:
  - action: persistent_notification.create
    metadata: {}
    data:
      title: Webhook Data
      message: "{{ trigger.json }}"
    enabled: false
  - variables:
      payload: "{{ trigger.json.attachments[0].text | from_json }}"
  - action: notify.mobile_app_your_device
    metadata: {}
    data:
      title: "NetAlertX Notice:"
      message: |
        {% if payload.new_devices | length > 0 %}
          {% for device in payload.new_devices %}
            MAC: {{ device.MAC }}
            Time: {{ device.Datetime }}
            IP: {{ device.IP }}
            Device Name: {{ device["Device name"] | default("Unknown") }}
            {% if not loop.last %}--------------------{% endif %}
          {% endfor %}
        {% else %}
          No new devices detected.
        {% endif %}
    enabled: true
mode: single

I knew Home Assistant’s “Choose” action was awesome, as I’ve used it with trigger ID’s. I didn’t know just how awesome it is until I learned that it can be used to parse specific json attributes as a condition. Holy cow… Here is the final result of my automation. This version will notify you regarding new devices, down devices, and when down devices reconnect. Make sure in NetAlertX you go to Settings > Notification Processing > Notify on and have “new_devices”, “down_devices”, and “down_reconnected” selected.

alias: Notify - NetAlertX Notifications
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - POST
      - PUT
      - GET
    local_only: true
    webhook_id: "your_secret_webhook_id"
    id: netalertx
conditions: []
actions:
  - action: persistent_notification.create
    metadata: {}
    data:
      title: Webhook Data
      message: "{{ trigger.json }}"
    enabled: false
  - variables:
      payload: "{{ trigger.json.attachments[0].text | from_json }}"
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ payload.new_devices is defined and (payload.new_devices |
              length) > 0 }}
        sequence:
          - action: notify.mobile_app_your_device
            metadata: {}
            data:
              title: "NetAlertX: New Devices"
              message: |
                {% if payload.new_devices | length > 0 %}
                  {% for device in payload.new_devices %}
                    MAC: {{ device.MAC }}
                    Time: {{ device.Datetime }}
                    IP: {{ device.IP }}
                    Device Name: {{ device["Device name"] | default("Unknown") }}
                    {% if not loop.last %}--------------------{% endif %}
                  {% endfor %}
                {% endif %}
      - conditions:
          - condition: template
            value_template: >-
              {{ payload.down_devices is defined and (payload.down_devices |
              length) > 0 }}
        sequence:
          - action: notify.mobile_app_your_device
            metadata: {}
            data:
              title: "NetAlertX: Device Down"
              message: |
                {% if payload.down_devices | length > 0 %}
                  {% for device in payload.down_devices %}
                    Name: {{ device.get("devName", "Unknown") }}
                    MAC: {{ device.get("eve_MAC", "Unknown") }}
                    Vendor: {{ device.get("devVendor", "Unknown") }}
                    IP: {{ device.get("eve_IP", "Unknown") }}
                    Time: {{ device.get("eve_DateTime", "Unknown") }}
                    Event Type: {{ device.get("eve_EventType", "Unknown") }}
                    {% if not loop.last %}--------------------{% endif %}
                  {% endfor %}
                {% endif %}
      - conditions:
          - condition: template
            value_template: >-
              {{ payload.down_reconnected is defined and
              (payload.down_reconnected | length) > 0 }}
        sequence:
          - action: notify.mobile_app_your_device
            metadata: {}
            data:
              title: "NetAlertX: Device Reconnect"
              message: |
                {% for device in payload.down_reconnected %}
                  Name: {{ device.get("devName", "Unknown") }}
                  MAC: {{ device.get("eve_MAC", "Unknown") }}
                  Vendor: {{ device.get("devVendor", "Unknown") }}
                  IP: {{ device.get("eve_IP", "Unknown") }}
                  Reconnected At: {{ device.get("eve_DateTime", "Unknown") }}
                  {% if not loop.last %}--------------------{% endif %}
                {% endfor %}
mode: queued
max: 10