A sensor that shows the latest notification and hold the history of notifications

A sensor that shows the latest notification and hold the history of notifications.
Of course, all notifications that were delivered for all devices (browser, mobiles, tablets etc.).
The idea is to have a card with all notifications delivered by Home assistant.

I think a general notification log would be great, the Android app offers notification history but that’s only good for the notifications it receives

1 Like

You can st this up this yourself. I keep a log of all TTS messages that are played. Basically so I can repeat the last message if I missed it. You can do the same thing for notifications.

Set up the file notify platform:

notify:
  - platform: file
    name: "TTS History"
    filename: /config/www/tts_history.txt
    value_template: "{{ value_json }}"

And a sensor to read the notification file:

  - platform: file
    name: "TTS Last Message"
    file_path: /config/www/tts_history.txt last line of that text file.

Then when you send a notification, also “notify” the file to write whatever data you want to log to the file.

    - service: notify.tts_history
      data:
        message: "{{ (now() ~ ' ' ~ message)[0:250]|replace('\n',' ') }}"

And here is where I read the last line and replay the message. The text file retains all of the historical messages until you delete them, but the file sensor contains only the last line.

    - service: script.tts_play
      data:
        message: >
          {% set message = states('sensor.tts_last_message')[33:255] %}
          {% set time = as_datetime(states('sensor.tts_last_message')[0:32]).strftime('%-I:%M %p') %}
          {{ message }} at {{ time }}
        tts_service: tts.cloud_say
        media_player: media_player.kiosk_internal_player
        quiet_play: true
        night_play: true
        ignore_away: true

It would be cool to have it built in for notifications though, and my solution would only show the last one so ya got my vote.

I do this with the python_script integration a template sensor and the custom card logbook-card. then you get this.

image

step 1: add the python_script integration

step 2: create the following python file and name it “set_state.py” and add to the folder python_scripts

if 'entity_id' not in data:
    logger.warning("===== entity_id is required if you want to set something.")
else:
    data = data.copy()
    inputEntity = data.pop('entity_id')
    inputStateObject = hass.states.get(inputEntity)
    if inputStateObject:
        inputState = inputStateObject.state
        inputAttributesObject = inputStateObject.attributes.copy()
    else:
        inputState = 'unknown'
        inputAttributesObject = {}
    if 'state' in data:
        inputState = data.pop('state')
    logger.debug("===== new attrs: {}".format(data))
    inputAttributesObject.update(data)

    hass.states.set(inputEntity, inputState, inputAttributesObject)

step 3: Add a template sensor

- sensor:
  - name: notifications
    unique_id: "ed5aed3f-46af-4991-a195-0457a3d116f2"
    state:  "Homeassistant is opnieuw opgestart"

Step 4: add the logbook_card component and choose the template sensor you have just created.
step 5: add the following service call to the automations where you want the notifications from

service: python_script.set_state
data:
  entity_id: sensor.notifications
  state: Het alarm is ingeschakeld.
1 Like