Display state_attr history in Lovelace Card

Tags: #<Tag:0x00007f7398084248> #<Tag:0x00007f739808bf98> #<Tag:0x00007f739808bdb8>

Hi guys,

I have Sonoff Door sensors (The kind that only gives “Open” state) hooked to a Sonoff RF bridge.
I have 4 doors connected to this RF bridge.
This is all working well with the AlexIT Sonoff repo and I have a card displaying the last state received.

What I would like to do is to have a card in Lovelace displaying a history of all the doors triggered with the date and time stamp.

I had a look at the state_attr “name:” which gives me the name of the door that was triggered but I am not sure how to go about displaying a history of let’s say the last 20 triggers with the date and time stamps.

Is anyone able to assist with something like this?

Currently, this is my config:

configuration.yaml

sonoff:
  username: [email protected]
  password: ************
  force_update: [temperature, power]
  scan_interval: '00:05:00'  # (optional) default 5 minutes
  sensors: [temperature, humidity, power, current, voltage]
  sonoff:
    rfbridge:
      Door sensor:
      name: Door Sensor with one state  # optional, you can change sensor name
      device_class: door
      timeout: 5

Under Developer Tools I have the sonoff.remote with Attributes:

type manufacturer: Sonoff
model: RF_Bridge
sw_version: PSF-BRA-GL v3.4.1
command: 1
ts: 2020-07-17T14:33:50.000Z
name: Front Door
friendly_name: Door Sensor
supported_features: 1

So I would imagine that I would need the “name:” and “ts:” attribute to get this to work but have no idea from here.

Appreciate the help guys!

I don’t know if there is an easy way (or even possible) to do it using built-in stuff but I do the exact thing you want to do using a custom component.

the custom component can be found here: https://github.com/rogro82/hass-variables

And here is the code I use to save the history of motion detection events (you can just change the trigger entities to the door sensors you want):

# first create a variable
variable:
  last_motion:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:map-marker
      name: 'Last Motion'

# then create an automation to update the variable
automation:
  - alias: 'AS Update Last Motion'
    initial_state: 'on'
    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
          - binary_sensor.bosch_sensor_1_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') }}"

then after the variable is created then you can use the variable in a lovelace card to display the history. It also uses a custom lovelace card - entity-attributes-card:

  - type: entities
    title: Motion Sensor History
    show_header_toggle: false
    state_color: true
    entities:
      - type: custom:entity-attributes-card
        entity: media_player.bedroom
        filter:
          include:
            - variable.last_motion.history_1
            - variable.last_motion.history_2
            - variable.last_motion.history_3
            - variable.last_motion.history_4
            - variable.last_motion.history_5
            - variable.last_motion.history_6
            - variable.last_motion.history_7
            - variable.last_motion.history_8
            - variable.last_motion.history_9
            - variable.last_motion.history_10

here is the resulting card:

ex

1 Like

Thanks so much, I will give this solution a go.

@finity - Thank you for the solution above, but I am not winning with this one. I am getting lost at the part of the trigger as well as the automation section. I do not have the door sensors as entities (if that makes sense). I only have one entity called “remote.sonoff_10007c5697” and then it is within it’s state_attr that it mentions which door was opened. Please have a look at the image below of the remote.sonoff entity:
Screenshot 2020-07-20 at 15.00.31
Other than this I have built an automation that broadcasts to my Google mini device when a door opens and the automation for that looks like the below:

- id: '1592847168444'
  alias: Announce Door open status
  description: ''
  trigger:
  - event_data: {}
    event_type: sonoff.remote
    platform: event
  condition:
  - after: '20:00'
    before: 06:00
    condition: time
  action:
  - data_template:
      command: The {{ trigger.event.data.name }} has just been opened
    service: rest_command.assistant_broadcast

{{ trigger.event.data.name }} basically just says the name of the door for example “Garage Door”.

I hope the above automation code is enough to give you an idea as to how the system knows which door was opened and how I can use my current code to store the {{ trigger.event.data.name }} with the time stamp which is “ts:” in the attributes as shown in the image above in the variable. I have fixed the date and time stamp to show correctly in my local time and not UTC by creating the below value template:

{{ as_timestamp(state_attr(‘remote.sonoff_10007c5697’,‘ts’))|timestamp_custom(’%Y-%m-%d-%H:%M’) }}

So I have the Name of the door as:
{{state_attr(‘remote.sonoff_10007c5697’,‘name’)}}
and I have the date / time stamp as :
{{ as_timestamp(state_attr(‘remote.sonoff_10007c5697’,‘ts’))|timestamp_custom(’%Y-%m-%d-%H:%M’) }}

Maybe with the info above you will be able to assist me on how I can adapt your code to make it work for me. Apologies but I am a bit stupid with these things, but I would really like to learn.

Thanks again.

Ok I have put some more time into this and I believe that I am very close to solving this but just need a bit of guidance to get it over the line:

configuration.yaml

# variable
variable:
  last_triggered:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:map-marker
      name: 'last Triggered'

Automation:

- id: '1595254249342'
  alias: Door trigger history
  description: ''
  trigger:
  - event_data: {}
    event_type: sonoff.remote
    platform: event
  condition: []
  action:
    service: variable.set_variable
    data:
      variable: last_triggered
      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.event.data.name }} : {{ as_timestamp(state_attr('remote.sonoff_10007c5697','ts'))|timestamp_custom('%Y-%m-%d-%H:%M') }}"

Lovelace entity-attributes-card:

type: entities
title: Motion Sensor History
show_header_toggle: false
state_color: true
entities:
  - type: 'custom:entity-attributes-card'
    entity: remote.sonoff_10007c5697
    filter:
      include:
        - variable.last_triggered.history_1
        - variable.last_triggered.history_2
        - variable.last_triggered.history_3
        - variable.last_triggered.history_4
        - variable.last_triggered.history_5
        - variable.last_triggered.history_6
        - variable.last_triggered.history_7
        - variable.last_triggered.history_8
        - variable.last_triggered.history_9
        - variable.last_triggered.history_10

With all of the above, it is not showing the info in the Lovelace card.

Is it not because the Lovelace card has to have the “variable” defined before it can display info from it?

Thanks

It is defined.

The “variable.last_triggered” is the entity that you want to show the data from and “history_1, history_2, etc” are the attributes you want to display.

One thing to check is if there is an entity called “variable.last_triggered” in your states view under developer tools that has a current valid state (which should be the name:timestamp of the last triggered event) and then make sure it has all of the attributes for history included in it under the “attributes” tab.

Hi and thank you for your assistance.
There is the variable called variable.last_triggered under the states but it only has the state listed as Not set as per image below.

Not sure where I went wrong but I will go through it all once more to see where I could have messed something up.

Once again thank you for your help.

for some reason the set_variable service isn’t running correctly from the automation.

Are you sure the trigger is working correctly? Does it contain the data in the “value” template?

Since there are no attributes set I believe the trigger is possibly the cause of the malfunction. If it was triggering you should probably have a bunch of attributes that all say “Not set” since the first thing it does is sets each attribute in a cascade from the previous state/history.

Thank you so much finity for all of your help. You were correct that the variable was never set. You pointed me in the right direction to have a look at the variable.last_triggered 's attributes to see that it shows the history attributes. Once I knew where to look, I redid the automation code and confirmed that the attributes were now showing. Once that was done, I rebuilt the Lovelace card and all was well. I am now able to see the state history of all my door open events.

Thank you so much, you are a legend!

Should anyone else be interested in doing the same with their Sonoff Door sensors hooked to the Sonoff Bridge, below is my code that I used:

in configuration.yaml:

# variable
variable:
  last_triggered:
    value: 'Not set'
    restore: true
    attributes:
      icon: mdi:map-marker
      name: 'last Triggered'

In automations.yaml:

- id: '1595326044618'
  alias: Door Trigger History
  description: ''
  trigger:
  - event_data: {}
    event_type: sonoff.remote
    platform: event
  condition: []
  action:
  data:
  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 }}"
     }
  variable: last_triggered
data_template:
  value: >-
    {{state_attr('remote.sonoff_10007c5697', 'name')}} : {{
    as_timestamp(state_attr('remote.sonoff_10007c5697','ts'))|timestamp_custom('%Y-%m-%d-%H:%M')
    }}
service: variable.set_variable

and lastly, the Lovelace card using the Lovelace card - entity-attributes-card:

type: entities
title: Door Open History
show_header_toggle: false
state_color: true
entities:
  - type: 'custom:entity-attributes-card'
    entity: variable.last_triggered
    filter:
      include:
        - variable.last_triggered.history_1
        - variable.last_triggered.history_2
        - variable.last_triggered.history_3
        - variable.last_triggered.history_4
        - variable.last_triggered.history_5
        - variable.last_triggered.history_6
        - variable.last_triggered.history_7
        - variable.last_triggered.history_8
        - variable.last_triggered.history_9
        - variable.last_triggered.history_10

Hope this helps someone else achieve the same results. All Kudos goes to @finity

2 Likes