Integration to show AISHub data of own AIS station in Home Assistant

Under the roof of my house I’m hosting an AIS receiver station with a RaspberryPi (currently a 3B), a dAISy HAT and a Sirio GP3E antenna. This station receives AIS signals from passing ships and feeds the NMEA data into the ship data networks AIS Hub, MarineTraffic, VesselFinder and FleetMon.

In addition to the above mentioned hardware you need to have an active, free subscription with https://www.aishub.net/. AISHub members are allowed to access AISHub webservices and retrieve AISHub data in XML, JSON or CSV format. The data can then be retrieved via AISHub’s API.

Now I want to see the AIS data of my own station in Home Assistant for which I have developed the following small integration: In configuration.yaml I have the following entries:

# Loads default set of integrations. Do not remove.
default_config:
python_script:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

command_line:
    - sensor:
        name: aishub
        command: "/usr/bin/python3 /home/homeassistant/.homeassistant/python_scripts/aishub.py"
        json_attributes:
          - ships
          - distinct
        value_template: "{{ value_json }}"
        scan_interval: 120

template:
  - sensor:
      - name: "AisHub ships in coverage"
        unit_of_measurement: ""
        state: "{{ state_attr('sensor.aishub', 'ships') }} "
        unique_id: aishub.ships
      - name: "AisHub unique ships"
        unit_of_measurement: ""
        state: "{{ state_attr('sensor.aishub', 'distinct') }} "
        unique_id: aishub.distinct

Then in my /home/homeassistant/.homeassistant/ folder (I’m running HA Core on another RaspberryPi 4 with 2GB) I created a /python_scripts folder. In this folder I placed the aishub.py file containing the python code to poll the AISHub API. The code is as follows (with U being the username and X being the station ID):

#!/usr/bin/python3

import json
import requests
url ="https://data.aishub.net/stations.php?username=U&format=1&output=json&compress=0&id=X"

try:
   resp = requests.get(url)
   #print(resp.status_code)
   #print(resp.text)
   #print(resp)
except BaseException as err:
   # Log out the error to the console for better investigation
   print("https://data.aishub.net/:", "POST request failed: {0}".format(err))
else:
   content=resp.text

import re
#print("resp=",resp.text)
#print("content=",content)
ships = re.findall(r'"SHIPS":([0-9\.]+)',content)[0]       # AisHub ships in coverage of station 3017
distinct = re.findall(r'"DISTINCT":([0-9\.]+)',content)[0] # AisHub unique ships in coverage of station X

#print(ships,distinct)
print(f'{{ "ships":{ships},"distinct":{distinct} }}')

With this python code and the trigger in configuration.yaml I’m polling the AISHub API every two minutes. In Home Assistant I created a simple card to display the essential values:

The yaml for these two cards is as follows:

type: history-graph
entities:
  - entity: sensor.aishub_ships_in_coverage
  - entity: sensor.aishub_unique_ships
title: AisHub API data Python

and

type: entities
entities:
  - entity: sensor.aishub_ships_in_coverage
  - entity: sensor.aishub_unique_ships
title: AisHub Station Realtime Data

This set-up works fine for me and I can easily see the reception quality of my AIS station in Home Assistant. :slight_smile:

1 Like