FreeNAS Stat Monitor

I don’t know what i’m doing wrong. All I get is None, Unknown and Unavailable in the entities.
This is how I edited the above from @reven for example:

# General info
  - platform: rest
    name: tn_info
    resource: http://192.168.0.7/1-xxxxxxxxxxxxxxxxxxxxx/v2.0/system/info
    headers:
      Authorization: Bearer 1-xxxxxxxxxxxxxxxxxxxxxxx
      Content-Type: application/json
      User-Agent: Home Assistant
    scan_interval: 3600
    value_template: '{{ value_json.uptime }}'
    json_attributes:
      - uptime_seconds
      - version
      - model
      - cores

When i go to http://192.168.0.7/1-xxxxxxxxxxxxxxxxxxxxx/v2.0/system/info in my browser it takes me to the TrueNAS login page, if this helps.

Your api key should not be in the resource address. Try:
http://192.168.0.7/api/v2.0/system/info

Also, 1-xxxxxxxxxxx is a placeholder for your actual api key. @troy described a few posts up how you generate a key FreeNAS Stat Monitor - #98 by troy

Thank you. Correcting http://192.168.0.7/api/v2.0/system/info did the trick for some entities.

I’m getting None for temps for hard drives, model shows up correctly, cores is fine, uptime seems messed up big time.

Edit: Nope got it neverming. Had to rename my hard drives ids. Thank you @reven

Unfortunately the only thing left that doesn’t work is the storage. Percentage shows as unavailable and pool shows blank. Any ideas?

Here’s my code:

# Storage percentage
  - platform: rest
    name: TN pool
    resource: http://192.168.0.7/api/v2.0/pool/id/1
    headers:
      Authorization: xxxtoken
      Content-Type: application/json
      User-Agent: Home Assistant
    value_template: '{{ value_json["status"] }}'
    json_attributes_path: '$.topology.data[0].stats'
    json_attributes:
      - allocated
      - size
    scan_interval: 1800
  - platform: template
    sensors:
      tn_pool_pct_used:
        friendly_name: "% Used"
        value_template: ' {{ (state_attr("sensor.tn_pool", "allocated") / state_attr("sensor.tn_pool", "size") * 100) | round(1) }} '
        unit_of_measurement: "%"

I recommend using the curl command or similar to grab the response from the api to debug.

Id also recommend looking at the result of
/api/v2.0/pool/

1 Like

Alternatively you can change the log level to debug for the rest integration. I think this will post the response to your logs. I don’t prefer this method as it likely will spam your logs.

As @MatthewFlamm said, double check from a terminal if you are using the correct id for your pool and particular setup. You can use the command:

curl -X GET -H "Authorization: Bearer 1-xxxxxxxxxxxxxxx" "http://192.168.0.7/api/v2.0/pool"
1 Like

Did anyone figure out how to display CPU usage / memory usage from TrueNas into HA?

Look at this post for ideas

Well, I implemented that in my configuration successfully (thanks for sharing), but memory and CPU usage would be also nice.

VIa

curl -X GET "http://localhost/api/v2.0/reporting/graphs" -H "accept: */*" --user 'root:password' | jq 

Troy mentioned some months ago it´s possible to get some information like cpu, memory, interface, df, upsremainingbattery and so on.

Can someone help me out to get the data to provide them for HA?

Well, this is not so much about adding something new to the sensor but since the latest focus has been around using the Ver 2.0 API, I though this might be close enough.

I don’t think this is worth it a separate post but it might still be worth a mention

Anybody interested in taking zfs dataset snapshots from Home Assistant!?

You can add the above, with the following

rest_command:
  take_snapshot:
    url: http://truenas.local/api/v2.0/zfs/snapshot
    method: POST
    headers:
      Authorization: !secret tn_bearer_token
      Content-Type: application/json
      User-Agent: Home Assistant
    payload: '{"dataset":"{{ dataset }}","name":"{{ name }}"}'

script:
  take_snapshot:
    alias: "Create ZFS Snapshot"
    icon: "mdi:party-popper"
    mode: single
    sequence:
      - service: rest_command.take_snapshot
        data_template:
          dataset: "{{ dataset }}"
          name: "{{ name }}"
    description: 'Create a zfs snapshot of a zpool/dataset'
    fields:
      dataset:
        description: 'Take a snaphot of zpool/dataset'
        example: tank/config/homeassistant
      name:
        description: 'Name of the snapshot to create'
        example: todays_snapshot
1 Like

When I to add code for reading CPU temp I get an error on the row for:

json_attributes_path: “$.[0]”

The error I get says:

can not read a block mapping entry; a multiline key may not be an implicit key at line 96, column 25:
json_attributes_path: “$.[0]”
^

Does anyone knows how to solve this?

This is probably a yaml format error.

I suspect you have missing end-quote on a previous line, can you post the whole config for this?

Now it works… :grinning:

I misspelled json… :triumph: put jsson instead of json…

# TrueNAS CPU Temperatur
  - platform: rest
    name: TrueNAS_cpu_temp
    resource: http://192.168.1.10/api/v2.0/reporting/get_data
    headers:
      Authorization: !secret tn_bearer_token
      Content-Type: application/json
      User-Agent: Home Assistant
    device_class: temperature
    unit_of_measurement: '°C'
    scan_interval: 60
    method: POST
    payload: >-
      {
          "graphs":[{"name":"cpu"},{"name":"cputemp"}],
          "reporting_query":{"unit":"HOUR","page":0,"aggregate":true}
      }
    json_attributes_path: "$.[0]"
    json_attributes:
      - aggregations
    value_template: >-
      {% set tn = namespace(temp=0, cores=4) %}
      {% for core in range(0, tn.cores) %}
      {% set tn.temp = tn.temp + value_json[1].data[358][core] %}
      {% endfor %}
      {{ "%.1f"% (tn.temp / tn.cores) }}

I also have some issues with getting the monitoring of storage to work…maybe you have some idea?

This is how it looks like in my configuration.yaml:


# TrueNAS Utrymme
  - platform: rest
    name: TrueNAS_pool
    resource: http://192.168.1.10/api/v2.0/pool/1/1
    headers:
      Authorization: !secret tn_bearer_token
      Content-type: application/json
      User-Agent: Home Assistant
    scan_interval: 1800
    value_template: '{{ value_json["status"] }}'
    json_attributes_path: '$.topology.data[0].stats'
    json_attributes:
      - allocated
      - size

 # Utrymme Använt
      nas_pool_used:
        friendly_name: "Allokerat"
        value_template: ' {{ state_attr("sensor.truenas_pool", "allocated") }} '
 # Utrymme Totalt
      nas_pool_total:
        friendly_name: "Totalt"
        value_template: ' {{ state_attr("sensor.truenas_pool", "size") }} '
 # Utrymme Använt %
      nas_pool_pct_used:
        friendly_name: "% Använt"
        value_template: ' {{ state_attr("sensor.truenas_pool", "allocated") / state_attr("sensor.truenas_pool", "size") * 100 | round(1) }} '
        unit_of_measurement: "%"

But I get “Unknown” on the sensors…

I ran the Curl command:

curl -X GET -H "Authorization: Bearer 1-xxxxxxxxxxxxxxx" "http://192.168.1.10/api/v2.0/pool"

The id in my case is just “1”.
There’s also a row called guid with a 20 digit number, tried both of them but still get “Unknown” on the sensors for Storage…Do you have any idea what could be the problem?

Try

resource: http://192.168.1.10/api/v2.0/pool/1

Close… I just checked mine, I think you’re looking for

resource: http://192.168.1.10/api/v2.0/pool/id/1

PS I made a basic blueprint for creating an automation to snapshot zfs datasets. I still think it could be improved on. If anybody want to take a look, give it a try, or make suggestion – You can find it here

1 Like

Thanks for the correction, you are right.

Thanks for the blueprint! I was working on something similar for the alerts for notification, but I won’t be able to work on it for few weeks. I will take a look at what you have so far.

Ahh…so stupid of me…

I never thought about that /1 in the example was the id number…I thought I should replace “id” with my id…

I searched in the API documentation under Pools, thought it was strange that I couldn’t find anything about the /1…but now I get it haha :laughing:

Big thanks! :grinning: