List of last state values

Hi,

I have a setup where the telephone number of incoming calls is distributed via MQTT and picked up by a sensor in my home assistant instance. The state of this sensor is either just empty or the telephone number if there currently is an incoming call.

Now I want to achieve the following, but do not really have an idea how to solve this: There should be a list in my lovelace interface showing me the last x incoming calls (telephone number + date/time).
Is there any way to generate such a list e.g. from the history? (only showing states where the state is not empty displayed in the form of a list)

Any help is greatly appreciated.
Regards

You could log them as persistent notifications:

- id: call_log
  alias: 'Call Log'
  trigger:
    platform: numeric_state
    entity_id: sensor.your_phone_number_sensor
    above: 0
  action:
  - service: persistent_notification.create
    data_template:
      message: "{{ now().strftime('%H:%M %A %d %B %Y') }} - The phone was called from {{ trigger.to_state.state }}"
      title: "Phone Call"

If your phone number sensor contains strings instead of a number (e.g. “555 888 555” - note the spaces) you might have to use this trigger instead:

  trigger:
    platform: template
    value_template: "{{ not is_state('sensor.your_phone_number_sensor', '') 

Then add a Lovelace custom card called the home feed card to display them:

Here is an automation I use that saves the states of the last 10 motion detection events into a history of a variable. You should be able to easily modify it for your use.

You’ll need to install a custom_component in order to use this but it works pretty well.

Here is the automation:

  - alias: 'Update Last Motion'
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.comp_rm_motion_template
        to: 'on'
      - platform: state
        entity_id:
          - sensor.computer_room_camera_motion
          - sensor.livingroom_camera_motion
          - sensor.diningroom_camera_motion
          - sensor.garage_camera_motion
          - sensor.kitchen_camera_motion
          - sensor.deck_camera_motion
        to: 'Detected'
    action:
      service: variable.set_variable
      data:
        variable: last_motion
        attributes_template: >
          {
            "history_1": "{{ variable.state }}",
            "history_2": "{{ variable.attributes.history_1 }}",
            "history_3": "{{ variable.attributes.history_2 }}",
            "history_4": "{{ variable.attributes.history_3 }}",
            "history_5": "{{ variable.attributes.history_4 }}",
            "history_6": "{{ variable.attributes.history_5 }}",
            "history_7": "{{ variable.attributes.history_6 }}",
            "history_8": "{{ variable.attributes.history_7 }}",
            "history_9": "{{ variable.attributes.history_8 }}",
            "history_10": "{{ variable.attributes.history_9 }}"
          }
      data_template:
        value: >
          {{ trigger.to_state.attributes.friendly_name }} : 
          {{ as_timestamp(trigger.to_state.last_changed)| timestamp_custom('%X') }}

and here is the custom component:

Thank you both for your ideas. I implemented the approach presented by @tom_l but came accross one difficulty:

The Home Feed Card allows me to specify an id_filter to only show persistent notifications matching that filter. However, I do not understand how to create appropriate notification IDs with the persistent_notification.create service. From the docs it seems as if I can only specify a “fixed” ID, but not a prefix (e.g. “call_log_”) to filter for the relevant notifications in the Home Feed Card. Do I miss something here? Can you give me an indication on how to create the notifications with IDs that can be filtered by the id_filter?

Thanks!

You could use this as the ID:

  - service: persistent_notification.create
    data_template:
      message: "{{ now().strftime('%H:%M %A %d %B %Y') }} - The phone was called from {{ trigger.to_state.state }}"
      title: "Phone Call"
      notification_id: "call_log_{{ as_timestamp(now()) }}"

It will have a prefix of call_log_ you can use to filter the home card. The timestamp as the rest of the ID will be unique so messages will not be overwritten.

EDIT: Hmm. Maybe not. From the docs:

The persistent_notification integration supports specifying templates for both the message and the title .

It does not say that the ID can’t be templated but not mentioning it there is ominous. Give it a go. If it does not work there is good cause for a PR to get this included.

EDIt2: Just had a look at the source for the persistent notification. The ID can not be templated:

        vol.Required(ATTR_MESSAGE): cv.template,
        vol.Optional(ATTR_TITLE): cv.template,
        vol.Optional(ATTR_NOTIFICATION_ID): cv.string,

So there goes that idea. The options I see are to put in a pull request to get the ID to support templates and hope someone with the time and skill can do it, or go with @finity’s idea.

Hi @finity, hi All,

I tried to use Home assistant variables component to save historical/last value of the backup, unfortunately I failed after few hours trial and error approach:

The goal is to save backup results communicated via mqtt message after subsequent HA reboot
(mqtt after reboot is not longer available).

configuration extract:

sensor:
  - platform: mqtt
    name: Google Backup Results
    state_topic: googlebackup/result
    value_template: '{{ as_timestamp(value_json.backupTimestamp)|timestamp_local }}'
    json_attributes_topic: googlebackup/result
#   payload:  # {"backupTimestamp": "2021-04-10T16:44:18.371463", "fromPattern": "/backup/*.tar", "backupDirID": "backupfolder", "fileCount": 7, "alreadyCount": 7, "backedUpCount": 0, "deletedCount": 0}

variable:
    lastbackup:
      value: '2021-01-01 00:00:00'
      attributes:
        fileCount: 0
        backedUpCount: 0
      restore: true

automation

  alias: gDriveBackupStataus
  description: Record state of last Google Drive Backup
  trigger:
  - platform: state
    entity_id: sensor.google_backup_results
  condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state not in ['Unknown'] }}
  action:
  - service: variable.set_variable
    data:
      variable: lastbackup
      attributes_template:
        fileCount: '{{ triger.to_state.attributes.fileCount }}'
        backedUpCount: '{{ triger.to_state.attributes.backedUpCount }}'
      value_template: '{{ trigger.to_state.state }}'
  mode: single

This automation does not update the lastbackup variable - do you have any idea how I shall correct it?

Many thanks in advance
P.

you have some spelling errors in your templates.

try these to see if it corrects the failure:

fileCount: '{{ trigger.to_state.attributes.fileCount }}'
backedUpCount: '{{ trigger.to_state.attributes.backedUpCount }}'

if not we can go on from there.

1 Like

LOL… Just LOL. :wink:

1 Like

Touche…touche…

:laughing:

I don’t know what you are talking about… I don’t see any spelling error. :smirk:

1 Like

:flushed: it is so embarrassing, corrected!

Summary
  alias: gDriveBackupStataus
  description: Record state of Google Drive Backup
  trigger:
  - platform: state
    entity_id: sensor.google_backup_results
  condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state not in ['Unknown'] }}
  action:
  - service: variable.set_variable
    data:
      variable: lastbackup
      attributes_template:
        fileCount: '{{ trigger.to_state.attributes.fileCount }}'
        backedUpCount: '{{  trigger.to_state.attributes.backedUpCount }}'
      value_template: '{{ trigger.to_state.state }}'
  mode: single

Unfortunately it did not solve my problem.
Can you support me further?
Thank you
P.

OK, I haven’t looked at the variable integration in a while.

First, that version of the custom integration (by rogro82) hasn’t been updated in over two yesrs now. So, first I recommend using the one by @Wibias found here instead. It was updated to account for changes in the way HA handled templates (I think that was why…).

After you change to that version and looking at my config for examples I think you need to try this:

alias: gDriveBackupStataus
  description: Record state of last Google Drive Backup
  trigger:
  - platform: state
    entity_id: sensor.google_backup_results
  condition:
  - condition: template
    value_template: >
      {{ trigger.to_state.state not in ['Unknown'] }}
  action:
  - service: variable.set_variable
    data:
      variable: lastbackup
      attributes:
        fileCount: '{{ trigger.to_state.attributes.fileCount }}'
        backedUpCount: '{{ trigger.to_state.attributes.backedUpCount }}'
      value: '{{ trigger.to_state.state }}'
  mode: single

SOLVED :+1:
Thank you for your support, now it works, without any problem. The old hacks repository apparently does not work with new HA version even with the right spelling :slightly_smiling_face:

1 Like