How to add text data to Home Assistant Logbook that comes from DIUN

I recently added DIUN to my docker stack to keep track of images with updates. I wanted these notifications to go to Home Assistant so all my notifications can come from one place.

DIUN will call a Webhook Automation, and the automation will set a Text Helper variable (that our Logbook will display), and then the automation will send a notification to my devices.

I like the idea of the Logbook on my dashboard so I can go through the history of updates that I need to install.

For this to work, we need a couple of things that we will go through below.

Add a Text Helper

Go to Settings, then Devices and Services, then click on Helpers, and add the text helper. The text helper is basically a variable that we can add text data into.

Home Assistant Text Helper


For the purposes of this tutorial, name it “diun_text_helper” and set the maximum length to 200 characters.

Home assistant text helper name and text length


Create the Home Assistant Webhook Automation

Next we will create the webhook automation. Basically, DIUN (Docker Image Update Notifier) will call this webhook every time a new image is available, and the automation will do the following:

  • Create a bunch of variables from the incoming JSON
  • Set the helper text “input_text.diun_last_update” to a concatenated string using our variables
  • Push a notification to my phone using those variables

Here is the full code for this home assistant webhook automation:

alias: Webhook - DIUN
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - POST
    local_only: true
    webhook_id: any-webhook-id-here
actions:
  - variables:
      img: "{{ trigger.json.image }}"
      status: "{{ trigger.json.status }}"
      link: "{{ trigger.json.hub_link | default('') }}"
      provider: "{{ trigger.json.provider }}"
      platform: "{{ trigger.json.platform | default('') }}"
      created: "{{ trigger.json.created | default('') }}"
  - data:
      entity_id: input_text.diun_last_update
      value: >-
        {{ now().strftime('%Y-%m-%d %H:%M:%S') }} | {{ status | upper }} | {{
        img }} | {{ platform }}
    action: input_text.set_value
  - data:
      title: "DIUN: {{ status | upper }} — {{ img }}"
      message: >-
        Provider: {{ provider }} Platform: {{ platform }} Created: {{ created }}
        {% if link %}Link: {{ link }}{% endif %}
    action: notify.my_devices
mode: queued

Take Note: By chaning the value of “input_text.diun_last_update”, Home Assistant will automatically create a Logbook entry because setting input_text.set_value changes the entity’s state, and the Logbook shows one entry per state change. So there is no need to call logbook.log like the below. If you do you will get duplicate entries in your logbook:

- action: logbook.log
    data:
      name: SOMETHING_HERE
      message: >-
        {{ now().strftime('%Y-%m-%d %H:%M:%S') }} | {{ status | upper }} | {{
        img }} | {{ platform }}
      entity_id: input_text.diun_last_update


Here is what the input text from DIUN looks like:

{
  "status": "new",
  "provider": "file",
  "image": "docker.io/crazymax/diun:latest",
  "hub_link": "https://hub.docker.com/r/crazymax/diun",
  "platform": "linux/amd64",
  "created": "2020-03-26T12:23:56Z",
  "metadata": { "ctn_names": "diun", "ctn_status": "Up ..." }
}

You can call your Webhook Automation via Postman to test that it works properly before configuring DIUN.

Here is the cURL you can import into Postman:

curl --location 'http://192.168.1.109:8123/api/webhook/any-webhook-id-here' \
--header 'Content-Type: application/json' \
--data '{
  "status": "new",
  "provider": "file",
  "image": "docker.io/crazymax/diun:latest",
  "hub_link": "https://hub.docker.com/r/crazymax/diun",
  "platform": "linux/amd64",
  "created": "2020-03-26T12:23:56Z",
  "metadata": { "ctn_names": "diun", "ctn_status": "Up ..." }
}'

Logbook card on a Dashboard

Next, we can put a logbook card on a dashboard, and set the entity to “input_text.diun_last_update“.

Just search for the Logbook Card type:

Home Assistant Logbook card type


And then use this YAML to define your card:

type: logbook
hours_to_show: 168
target:
  entity_id:
    - input_text.diun_last_update

This is what the Logbook card will look like:

It will display a list of all the values that “input_text.diun_last_update” had.

Home Assistant Logbook card


DIUN Portainer File

You can use this stack file and environment variables to spin up DIUN:

services:
  diun:
    container_name: diun
    image: crazymax/diun:latest
    command: serve
    volumes:
      - ${DATADIR}/data:/data
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - TZ=${TZ}
      - DIUN_WATCH_SCHEDULE=0 21 * * 3
      - DIUN_PROVIDERS_DOCKER=true
      - DIUN_PROVIDERS_DOCKER_WATCHBYDEFAULT=true
      - DIUN_WATCH_FIRSTCHECKNOTIF=true
      - DIUN_NOTIF_WEBHOOK_ENDPOINT=${WEBHOOK_HOME_ASSISTANT}
      - DIUN_NOTIF_WEBHOOK_METHOD=POST
      - DIUN_NOTIF_WEBHOOK_HEADERS_CONTENT-TYPE=application/json
      - DIUN_NOTIF_WEBHOOK_TIMEOUT=10s
      - DIUN_DEFAULTS_WATCHREPO=false
    restart: unless-stopped


And these environment variables: (modify it for your environment)

DATADIR=/opt/diun
TZ=Africa/Johannesburg
WEBHOOK_HOME_ASSISTANT=http://192.168.1.109:8123/api/webhook/any-webhook-id-here

necrolingus

Tech enthusiast and home labber