iantrich / list-card

📰 Display sensor list data in a table
Apache License 2.0
123 stars 34 forks source link

Error when card is displayed before the sensor gets updated for the first time #46

Open viktak opened 2 years ago

viktak commented 2 years ago

I created custom mqtt sensor which gets its data on a regular basis, i.e. every minute. If I visit the page with the list-card before the first update of the sensor, I get the following error in the HA logs:

Logger: frontend.js.latest.202203012
Source: components/system_log/__init__.py:190
First occurred: 10:28:10 AM (1 occurrences)
Last logged: 10:28:10 AM

http://192.168.1.111:8123/hacsfiles/list-card/list-card.js:92:71 TypeError: can't convert undefined to object

Once the sensor is updated this error doesn't happen.

This is my sensor:

- platform: mqtt
  name: hostnames
  state_topic: "viktak/network/dhcp"
  value_template: >
    {{ value_json.hosts | length }}
  json_attributes_topic: "viktak/network/dhcp"
  json_attributes_template: >-
      {{
        {
        'hosts' : value_json.hosts
        } | tojson        
      }}

This is my card:

  - type: custom:list-card
    entity: sensor.hostnames
    title: Hostnames
    feed_attribute: hosts
    columns:
      - title: Hostname
        field: field4
      - title: IP Address
        field: field3
      - title: MAC Address
        field: field2

This is a sample JSON arriving:

{"hosts":[
  {
    "field1": "1648590673",
    "field2": "00:24:8c:66:7f:54",
    "field3": "192.168.1.222",
    "field4": "test",
    "field5": "01:00:24:8c:66:7f:54"
  },
  {
    "field1": "1648586977",
    "field2": "5c:cf:7f:18:d6:94",
    "field3": "192.168.1.147",
    "field4": "officehall-18D694",
    "field5": "*"
  },
  {
    "field1": "1648585868",
    "field2": "5c:cf:7f:2b:b3:78",
    "field3": "192.168.1.105",
    "field4": "bedroomhall-2BB378",
    "field5": "*"
  }
]}

I think, the correct behavior should be no error message, just the card with the header, but no rows.