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

4 Likes

hi Matt,

Old thread I know but this looks great and exactly what I want in some ways however I don’t understand how to set up the webhooks for this to work; all the rest I think I get.

What I was trying to do was get a sorta dashboard thing going that tells me what’s connected or disconnected with my thought for doing that (before finding this) was to use the MQTT side of things to do it.

Can you tell me how/where you set up the webhooks though please?

Add Webhooks to the plugins list on netAlertx then use the script above to monitor the webhook adding your own ID and in the url put
yourhassaddress/api/webhook/yourwebhook
in the url field.

2 Likes

Hey Kenneth,

Sorry, I’ve been working 12-hour shifts at work, but now I’m on holiday break. @jane-t is correct. If you haven’t figured it out completely, yet, here’s an explanation - In your NetAlertX settings, go to the LOADED_PLUGINS field under the Core Settings and add “WEBHOOK” by clicking “Change”, now go to “Publishers” and you’ll see the “webhook publishers” plugin. Click on it. Under the target URL field, add your full webhook URL from the HA automation. This is what sends the json payload back to HA.

1 Like

If you want to show all the devices on an HA dashboard, you will need to use the MQTT option which will add all devices and status as MQTT entities, but you will need to have an MQTT broker set up and install the MQTT plugin in NetXAlert.
This video shows the basics. https://youtu.be/_q03e06l1wo?si=YCBs0Ie9G8n7MHgc

1 Like

The netalertX dev made some changes to the syntax of the json on the latest release. Just wanted to share how I’m structuring the notification for new_device, down_device, and down_reconnected with these changes -

New Device -

{% if payload.new_devices | length > 0 %}
  {% for device in payload.new_devices %}
    Name: {{ device.get("devName", "Unknown") }}
    MAC: {{ device.get("eveMac", "Unknown") }}
    IP: {{ device.get("eveIp", "Unknown") }}
    Time: {{ device.get("eveDateTime", "Unknown") }}
    Event Type: {{ device.get("eveEventType", "Unknown") }}
    {% if not loop.last %}--------------------{% endif %}
  {% endfor %}
{% endif %}

Down Device -

{% if payload.down_devices | length > 0 %}
  {% for device in payload.down_devices %}
    Name: {{ device.get("devName", "Unknown") }}
    MAC: {{ device.get("eveMac", "Unknown") }}
    IP: {{ device.get("eveIp", "Unknown") }}
    Time: {{ device.get("eveDateTime", "Unknown") }}
    Event Type: {{ device.get("eveEventType", "Unknown") }}
    {% if not loop.last %}--------------------{% endif %}
  {% endfor %}
{% endif %}

Down Reconnected -

{% if payload.down_reconnected | length > 0 %}
  {% for device in payload.down_reconnected %}
    Name: {{ device.get("devName", "Unknown") }}
    MAC: {{ device.get("eveMac", "Unknown") }}
    IP: {{ device.get("eveIp", "Unknown") }}
    Time: {{ device.get("eveDateTime", "Unknown") }}
    Event Type: {{ device.get("eveEventType", "Unknown") }}
    {% if not loop.last %}--------------------{% endif %}
  {% endfor %}
{% endif %}

There are a couple other fields that can be added. I don’t have any use for them at this time, but for those that want to know, you can include devVendor and devComments.