I started looking the other day for a way to integrate the notifications from this forum into my Home Assistant instance so I could see if there was anything new for me to check out (despite the fact I visit the forum a million times a day anyway). To my amazement, there was no way of doing this.
Therefore, im going to show how to get your latest notifications into Home Assistant, like so;
The forum is run on Discourse and I found some snippets of info at Notifications of Replies in this Forum to HA however there is lots of confision reading about Discourse between an Admin API and a User API.
I found a fantastic tool at Generate user API key for Discourse / Robin van Boven / Observable which allows you to enter the HA Community URL (https://community.home-assistant.io) and then click to request permission. This will bring you to this site and you can click to authorise the creation of a Read Only access api. Clicking authorise will generate a block of code which needs to be copy and pasted back into the API generating website.
You should then have a valid response and a new “User-Api-Key:”
Your API access token can be viewed and removed should you wish by going into Preferences > Apps on this site. Unless your change the name on the API Generating page, it will show as “SourceCred Discourse Plugin”
Now you are ready to pull your notification settings into your HA using a rest sensor
rest:
- resource: https://community.home-assistant.io/notifications.json
headers:
User-Api-Key: !secret community_api_key
scan_interval: 60
sensor:
- name: "Community Notification - 0"
json_attributes_path: "$.notifications[0]"
value_template: '{{ value_json.notifications[0].created_at }}'
json_attributes:
- notification_type
- read
- post_number
- topic_id
- fancy_title
- slug
- data
Ive then created a template sensor to set up the correct icons, urls etc depending on the type of notification types
template:
- sensor:
- name: Community Notification - 0 - Post
unique_id: sensor.community_notifcation_0_post
icon: >
{% set nt = state_attr('sensor.community_notification_0', 'notification_type') %}
{% if nt == 1 %} {# Mentioned #}
mdi:at
{% elif nt == 2 %} {# New Reply #}
mdi:reply
{% elif nt == 3 %} {# Quoted #}
mdi:format-quote-close
{% elif nt == 4 %} {# Edited #}
mdi:pencil-outline
{% elif nt == 5 %} {# New Like #}
mdi:cards-heart
{% elif nt == 6 %} {# New Private Message #}
mdi:email-outline
{% elif nt == 8 %} {# Invite Accepted #}
mdi:account
{% elif nt == 9 %} {# New Post #}
mdi:reply
{% elif nt == 11 %} {# Linked #}
mdi:link-variant
{% elif nt == 12 %} {# Badge Granted #}
mdi:decagram
{% elif nt == 14 %} {# Solution #}
mdi:checkbox-marked-outline
{% else %} {# Catch All #}
mdi:help-circle
{% endif %}
state: >
{% set nt = state_attr('sensor.community_notification_0', 'notification_type') %}
{% set title = state_attr('sensor.community_notification_0', 'fancy_title') %}
{% set username = states.sensor.community_notification_0.attributes.data.display_username %}
{% set badge = states.sensor.community_notification_0.attributes.data.badge_name %}
{% if nt in [1, 2, 3, 4, 5, 6, 9, 11, 14] %}
{{ username }} - {{ title}}
{% elif nt in [8] %}
'{{ username }}' accepted your invitation
{% elif nt in [12] %}
Earned '{{ badge }}'
{% else %}
{# Catch All #}
Unknown Notification
{% endif %}
attributes:
url: >
{% set nt = state_attr('sensor.community_notification_0', 'notification_type') %}
{% set slug = state_attr('sensor.community_notification_0', 'slug') %}
{% set topic = state_attr('sensor.community_notification_0', 'topic_id') %}
{% set post = state_attr('sensor.community_notification_0', 'post_number') %}
{% set display_username = states.sensor.community_notification_0.attributes.data.display_username %}
{% set badge_id = states.sensor.community_notification_0.attributes.data.badge_id %}
{% set badge_slug = states.sensor.community_notification_0.attributes.data.badge_slug %}
{% set username = states.sensor.community_notification_0.attributes.data.username %}
{% if nt in [1, 2, 3, 4, 5, 6, 9, 11, 14] %}
https://community.home-assistant.io/t/{{ slug }}/{{ topic }}/{{ post }}/
{% elif nt in [8] %}
https://community.home-assistant.io/u/{{ display_username }}
{% elif nt in [12] %}
https://community.home-assistant.io/badges/{{ badge_id }}/{{ badge_slug }}?username={{ username }}
{% else %}
{# Catch All #}
{% endif %}
read: '{{ state_attr(''sensor.community_notification_0'', ''read'') }}'
created_at: '{{ states(''sensor.community_notification_0'') }}'
This will pull a sensor into Home assistant with the correct URL and Icons based upon the notification type.
I am only aware of the following notification types. I know I will be missing some so if anyone can add any info to this list I will update the sensor
- Mentioned
- New Reply
- Quoted
- Edited
- New Like
- New Private Message
- New Post
- Linked
- Badge Granted
- Solution - Added 26/5/21
- Invite Accepted - Added 26/5/21
Personally, I have duplicated the above sensors to give me the last 5 notifications from the community and then displayed them using GitHub - thomasloven/lovelace-template-entity-row: 🔹 Display whatever you want in an entities card row.
type: entities
entities:
- type: 'custom:template-entity-row'
entity: sensor.community_notification_0_post
name: '{{ states(config.entity) }}'
secondary: >
{% set last_updated_seconds = as_timestamp(now()) -
as_timestamp(strptime(state_attr(config.entity, 'created_at'),
'%Y-%m-%dT%H:%M:%S')) %}
{% set days = (last_updated_seconds // (60 * 60 * 24)) | int %} {% set
hours = (last_updated_seconds // (60 * 60)) | int %} {% set minutes =
(last_updated_seconds // 60) | int %} {% set seconds =
(last_updated_seconds // (60 // 60)) | int %}
{% set seconds = seconds - (minutes * 60) - (days * 24 * 60 * 60) - (hours
* 60 * 60) %} {% set minutes = minutes - (days * 24 * 60) - (hours * 60)
%} {% set hours = hours - (days * 24) %} {% set weeks = (days // 7) | int
%}
{% set days = (days | int) - (weeks * 7) %}
{% macro phrase(value, name) %}
{%- set value = value | int %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
{%- endmacro %}
{% set text = [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours,
'hour'),
phrase(minutes, 'min'), phrase(seconds, 'sec') ] | select('!=','') | list | join(', ') %}
{% set last_comma = text.rfind(',') %} {% if last_comma > 0 %}
{% set text = text[:last_comma] + ',' + text[last_comma + 1:] %}
{%- endif %} {{ text }} ago
state: null
title: Home Assistant Community
I hope people find this helpful.