FreeNAS Stat Monitor

This is what I have to get used allocation in a pool.

- platform: rest
  name: Nas Storage
  resource: http://192.168.1.180/api/v2.0/pool/id/1
  headers:
    Authorization: mytoken
    User-Agent: Home Assistant
    Content-Type: application/json
  value_template: '{{ value_json["status"] }}'
  json_attributes_path: '$.topology.data[0].stats'
  json_attributes:
    - allocated
    - size
  scan_interval: 1800
- platform: template
  sensors:
    nas_storage_usage:
      friendly_name: "Nas Storage Usage"
      value_template: ' {{ (state_attr("sensor.nas_st\
orage", "allocated") / state_attr("sensor.nas_storage",\
 "size") * 100) | round(1) }} '
      unit_of_measurement: "%"
3 Likes

Thanks @MatthewFlamm!

So with all the bits and pieces that everyone has posted here and elsewhere, these are my TrueNAS sensors that all work with the 2.0 API and don’t rely on scripts, and this is my monitor page:

Hope it’s useful to someone and feel free to suggest improvements.

# TrueNAS sensors

# All of the sensors have been ported to use the v2.0 API.

# Current sensors:
# rest:
#   tn_info (uptime, version, model, cores)
#   tn_cpu_temp
#   tn_alert_level *
#   tn_pool
#   tn_disk_temp
# template:
#   tn_uptime_seconds
#   tn_uptime
#   tn_pool_pct_used
#   tn_version
#   tn_cpu_model
#   tn_cpu_cores
#   tn_alert_message
#   tn_ada0_temperature
#   tn_ada1_temperature
#   tn_ada2_temperature

# General info
- platform: rest
  name: tn_info
  resource: http://192.168.1.4/api/v2.0/system/info
  headers:
    Authorization: !secret tn_api_key
    Content-Type: application/json
    User-Agent: Home Assistant
  scan_interval: 3600
  value_template: '{{ value_json.uptime }}'
  json_attributes:
    - uptime_seconds
    - version
    - model
    - cores

## template sensors that extract state_attr
- platform: template
  sensors:
    ## NAS Uptime seconds
    tn_uptime_seconds:
      friendly_name: "Uptime seconds"
      value_template: "{{ state_attr('sensor.tn_info', 'uptime_seconds') }}"
    ## NAS Uptime seconds nice
    tn_uptime:
      friendly_name: "Uptime"
      value_template: >-
        {%- set uptime  = states.sensor.tn_uptime_seconds.state | round -%}
        {%- set sep     = ', ' -%}
        {%- set TIME_MAP = {
            'week': (uptime / 10080) % 10080,
             'day': (uptime / 1440) % 7,
            'hour': (uptime / 60) % 24,
          'minute': (uptime % 60)
        }
        -%}

        {%- for unit, duration in TIME_MAP.items() if duration >= 1 -%}
          {%- if not loop.first -%}
            {{ sep }}
          {%- endif -%}

          {{ (duration | string).split('.')[0] }} {{ unit }}

          {%- if duration >= 2 -%}
            s
          {%- endif -%}
        {%- endfor -%}

        {%- if uptime < 1 -%}
          just now
        {%- endif -%}
    ## NAS Version
    tn_version:
      friendly_name: "Version"
      value_template: "{{ state_attr('sensor.tn_info', 'version') }}"
    ## CPU model
    tn_cpu_model:
      friendly_name: "Model"
      value_template: "{{ state_attr('sensor.tn_info', 'model') }}"
    ## CPU num of cores
    tn_cpu_cores:
      friendly_name: "Cores"
      value_template: "{{ state_attr('sensor.tn_info', 'cores') }}"

## cpu temp version 2
## remember - set `cores=$num` in the value_template
- platform: rest
  name: tn_cpu_temp
  resource: http://192.168.1.4/api/v2.0/reporting/get_data
  headers:
    Authorization: !secret tn_api_key
    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) }}

## Alerts. Error if response is empty.
- platform: rest
  name: tn_alert_level
  resource: http://192.168.1.4/api/v2.0/alert/list
  headers:
    Authorization: !secret tn_api_key
    Content-Type: application/json
    User-Agent: Home Assistant
  scan_interval: 300
  json_attributes:
    - level
    - formatted
  value_template: >
    {% if value_json is defined %}
      {{ value_json[0].level }}
    {% else %}
      {{ "None" }}
    {% endif %}
- platform: template
  sensors:
    ## Alert message
    tn_alert_message:
      friendly_name: "Alert Message"
      value_template: "{{ state_attr('sensor.tn_alert_level', 'formatted') }}"

# Disk temps
- platform: rest
  name: tn_disk_temp
  method: POST
  resource: http://192.168.1.4/api/v2.0/disk/temperatures
  headers:
    Authorization: !secret tn_api_key
    Content-Type: application/json
    User-Agent: Home Assistant
  scan_interval: 300
  payload: '{"names":["ada0","ada1","ada2"]}'
  json_attributes:
    - ada0
    - ada1
    - ada2
- platform: template
  sensors:
    tn_ada0_temperature:
      unit_of_measurement: '°C'
      value_template: '{{ state_attr("sensor.tn_disk_temp", "ada0") }}'
    tn_ada1_temperature:
      unit_of_measurement: '°C'
      value_template: '{{ state_attr("sensor.tn_disk_temp", "ada1") }}'
    tn_ada2_temperature:
      unit_of_measurement: '°C'
      value_template: '{{ state_attr("sensor.tn_disk_temp", "ada2") }}'

# Storage percentage
- platform: rest
  name: TN pool
  resource: http://192.168.1.4/api/v2.0/pool/id/1
  headers:
    Authorization: !secret tn_api_key
    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: "%"
14 Likes

The CPU temperature request and handling is quite cumbersome. I’m glad you figured it out and shared it!

I’ve tried looking at the online API documentation to find an easier to use endpoint for this information, but it loads soooo slowly and often crashes my browser.

I definitely can’t take credit for any of it. :smile: The cpu temperature part was posted by @troy.

hello reven! A true newbie here. First of all, Thanks you all very much for making things easier, and I apologize in advance for not being on your level. I’d like to know if you could help me get some information to make it work. The information I don’t understand is where do you get the api key for authorization? do you have to configure something in freenas? I tried to follow the steps but English is not my native language, so I got lost.

Welcome @nani_silverstone

TrueNAS 12.0 and later has the ability to use API keys instead of login credentials. Unfortunately, there is no way to use the API key on FreeNAS 11.3.

Here’s the link for the TrueNAS docs

Basically

To create an API key, open the [ TrueNAS ] web interface :gear: (Settings) menu and click API KEY .

Click Add , enter a new key name, and click Add again to confirm. Immediately after the API key is created, you can use the key locally by clicking Copy to Clipboard . The API key can only be copied immediately after creation. When the initial creation window is closed, the API key cannot be copied again.


EDIT: For those still using FreeNAS 11.3, I have modified the # General info sensor from @reven great example, to show how you can use login credentials instead of an API Key.

# General info
- platform: rest
  name: tn_info
  resource: http://192.168.1.4/api/v2.0/system/info
  authentication: basic
  username: !secret tn_user
  password: !secret tn_passwd
  headers:
    Content-Type: application/json
    User-Agent: Home Assistant
  scan_interval: 3600
  value_template: '{{ value_json.uptime }}'
  json_attributes:
    - uptime_seconds
    - version
    - model
    - cores
3 Likes

71/5000

Thank you very much for the quick answer!!! I will try these days :slight_smile:

Thanks @reven, brilliant stuff.
Mostly working here though I was unable to get the alert message to show content for some reason.

Oh, and for others confused with using API keys, you need to make sure it’s precursered with “Bearer”.
So for myself I set my tn_api_key variable to something like:

tn_api_key: Bearer 1-SomerandomAPIkeystringthing

EDIT: okay so found the reason for the missing alert message, my current warning message of pools eligible for upgrade was more than the 255 character limit allowed for a sensor state. Something to keep in mind.

3 Likes

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