(Template) Sensor of nested json_attributes (Solved)

Hey there,

as the HP iLO Integration doesn’t work with older hardware anymore (https://github.com/home-assistant/core/issues/87277, https://github.com/seveas/python-hpilo/issues/274) (iLO2 in my case), I need another approach to get that health data to my HA.

I can grab that iLO health data via another client, which has an older python than my HA and is able to retrieve that information as json data. Here’s my code in configurations.yaml:

  - platform: command_line
    name: hpdl380g6_health
    command: "ssh -i /data/.ssh/id_ed25519 [email protected] 'hpilo_cli -P raw -j -l user -p pass 192.168.1.2 get_embedded_health'" 
    command_timeout: 55
    scan_interval: 60
    value_template: "{{ now().timestamp() | int }}"
    json_attributes:
      - health_at_a_glance 
      - temperature
      - fans
      - power_supplies
      - drives_backplanes

As this command takes around 25 seconds I don’t want to split it into several requests.

As json data I get back the following data:

{
  "health_at_a_glance": {
    "power_supplies": {
      "status": "Ok",
      "redundancy": "Fully Redundant"
    },
    "fans": {
      "status": "Ok",
      "redundancy": "Fully Redundant"
    },
    "temperature": {
      "status": "Ok"
    },
    "vrm": {
      "status": "Ok"
    },
    "drive": {
      "status": "Ok"
    }
  },
  "temperature": {
    "Temp 4": {
      "status": "Ok",
      "currentreading": [
        38,
        "Celsius"
      ],
      "label": "Temp 4",
      "critical": [
        92,
        "Celsius"
      ],
      "caution": [
        87,
        "Celsius"
      ],
      "location": "Memory"
    },
    "Temp 5": {
      "status": "Ok",
      "currentreading": [
        41,
        "Celsius"
      ],
      "label": "Temp 5",
      "critical": [
        92,
        "Celsius"
      ],
      "caution": [
        87,
        "Celsius"
      ],
      "location": "Memory"
    },
    "Temp 6": {
      "status": "Ok",
      "currentreading": [
        43,
        "Celsius"
      ],
      "label": "Temp 6",
      "critical": [
        92,
        "Celsius"
      ],
      "caution": [
        87,
        "Celsius"
      ],
      "location": "Memory"
    },
... and much more information :)

Primarily I want to write that information into InfluxDB and visualize it via grafana, but as the states are very nested I didn’t find a way to feed that Sensor Attributes to grafana.

So I’ve tried to define a new template sensor

- platform: template
    sensors:
      hpdl380g6_health_power_supplies_status
        value_template: "{{ state_attr('sensor.hpdl380g6_health', 'health_at_a_glance.power_supplies.status') }}"

but didn’t get it work.

I’ve tried different syntaxes with the template editor to figure out how it could work, but i.e. with this {{state_attr('sensor.hpdl380g6_health', 'health_at_a_glance', 'power_supplies', 'status') }} it results in a TypeError: state_attr() takes 3 positional arguments but 5 were given.

Any idea about failures, wrong syntax or easier approaches? Thanks in advance!

Suggest you use the modern format for template sensors under template: not sensor:, although the template to pull the value from the dictionary in the attribute is the same:

template:
  - sensors:
      - name: hpdl380g6_health_power_supplies_status
        state: "{{ state_attr('sensor.hpdl380g6_health', 'health_at_a_glance')['power_supplies']['status'] }}"

Thanks for the suggestion @Troon, but the modern format didn’t bring me the data either. The state of the sensor is still “unknown”.

Paste in a screenshot from Developer Tools / States showing your hpdl380g6_health sensor’s state and attributes columns.

Also paste the result of my template when put into Developer Tools / Templates.

I’m sorry, but while copying your solution I put wrong brackets there, after I’ve checked again I found that error and now it works! Thanks a lot!

1 Like

Even if I don’t know/don’t understand why to put the “health_at_a_glance” to the sensor part of the state: "{{ state_attr(...)[...][...] }}" declaration and the “power_supplies” and “status” to the latter part. But yeah, thanks again for the solution!

The hpdl380g6_health sensor has an attribute called health_at_a_glance. You retrieve that with the state_attr call.

That returns this:

{
  "power_supplies": {
    "status": "Ok",
    "redundancy": "Fully Redundant"
  },
  "fans": {
    "status": "Ok",
    "redundancy": "Fully Redundant"
  },
  # and so on
}

To access the value you want, you first get ['power_supplies'] which gives you:

{
  "status": "Ok",
  "redundancy": "Fully Redundant"
}

then dig out ['status'] which returns "Ok".

1 Like

Thanks for that explanation, that’ll help me with other json data as well.

Unfortunately (but consequently) the sensor.hpdl380g6_temp_ambient sensors contains the value and “Celsius” now, as you can see in the screenshot.


Maybe you have an easy idea how to get rid of it, so that the sensory only contains the number in the end, if not I research/try later by myself.

Stick a [0] on the end of the template, to get just the first item in the sequence.

@Troon Thaaaank you so much!

1 Like