JSON Array

Hi All,

I am struggling to figure out how I can display this JSON array in lovelace

I receive this array from an MQTT sensor:

  - platform: mqtt
    name: "Network_Clients_Status"
    state_topic: "homeassistant/domotz"  

Example of data in Network_Clients_Status:

{"display_name":"Windows Server","status":"DOWN","last_status_change":"2021-04-27T20:02:00+00:00","IP":["172.24.1.221"]}

There could be multiple entries returned, in my example above, there is just one entry “Windows Server”

The data is a list of network devices that are offline.

What I’d like to do is display these entries within lovelace - i.e. a widget that lists devices that are offline.

What’s the best way to take this JSON array and display it in lovelace?

Thanks!

It’s easy to do but what does the received JSON look like when there’s more than just one network device that’s offline? You said:

However, what you posted isn’t a list (or an array; it’s a dictionary).

Thanks for the reply!

This is what > 1 entry looks like

[
  {
    "display_name": "synology",
    "status": "DOWN",
    "last_status_change": "2021-01-28T22:16:38+00:00",
    "IP": [
      "172.24.1.24"
    ]
  },
  {
    "display_name": "Windows Server",
    "status": "DOWN",
    "last_status_change": "2021-04-27T20:02:00+00:00",
    "IP": [
      "172.24.1.11"
    ]
  }
]

As additional background, this is being generated by a jsonata query and pushed to mqtt via nodered:


message[status='DOWN'][importance = 'VITAL'].{
   "display_name": **.display_name,
   "status": **.status,
   "last_status_change": **.last_status_change,
   "IP": **.ip_addresses
}

Create a Template Sensor using the following template:

{{ value_json | map(attribute='display_name') | join (', ') }}

For example:

template:
  - sensor:
      - name: "Network Clients Status"
        state: "{{ value_json | map(attribute='display_name') | join (', ') }}"

After you add the information to your configuration.yaml file, execute Configuration > Server Controls > Check Configuration and ensure no errors are reported. Then execute Configuration > Server Controls > Reload Template Entities. A new sensor will be created: sensor.network_client_status
The sensor’s state value will contain a comma-delimited list of server names such as “synology, Windows Server”. If will also automatically appear in the Lovelace UI.

NOTE
An entity’s state value is limited to 255 characters. If you believe that’s insufficient for your application, we will have to change the Template Sensor’s configuration so that it stores the server names in an attribute (which doesn’t have a 255-character limit).

Thank you for the detailed reply.

I’ve got the data in an existing mqtt sensor and I’m trying to reference this in the template. Although no errors, I see this when looking at the sensor created:

Is my syntax incorrect?

  - platform: mqtt
    name: "Network_Clients_Status"
    state_topic: "homeassistant/domotz"
    
template:
  - sensor:
      - name: "Network Offline Clients"
        state: "{{ 'states.sensor.Network_Clients_Status' | map(attribute='display_name') | join (', ') }}"

I apologize for misleading you. You are using an MQTT Sensor and I’ve been dealing with Template Sensors all day and gave you a screwed up example of one. :man_facepalming:

See if this works for you:

  - platform: mqtt
    name: "Network_Clients_Status"
    state_topic: "homeassistant/domotz"
    value_template: "{{ value_json | map(attribute='display_name') | join (', ') }}"

After confirming there are no errors, execute Configuration > Server Controls > Reload Manually Configured MQTT Entities.

1 Like

Thanks this works perfectly! Appreciate the pointers!

1 Like

You’re welcome and I’m glad to hear it resolved the issue.

Please consider marking my post above with the Solution tag. Only you, the author of this topic, can mark one post. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

1 Like