FreeNAS Stat Monitor

HI all, this look great. Im new to HA and just trying to get this working but my values are all coming up as “Unknown” and the error log says “REST result could not be parsed as JSON”.

Would anyone be able to let me know what I’ve doing wrong?

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

sensor:
# General info
- platform: rest
  name: tn_info
  resource: http://192.168.1.245/api/v2.0/system/info
  headers:
    Authorization: Fo15XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXIbOKASKgeYZ
    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.245/api/v2.0/reporting/get_data
  headers:
    Authorization: Fo15XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXYkPRIbOKASKgeYZ
    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) }}

Authorization: Fo15XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXIbOKASKgeYZ

Do you have the Bearer 1-, or whatever number you have, in this line?

@reven Thanks again for putting this together and sharing. Wondering if I may ask a quick question regarding the text below. I don’t understand the templating code very well, but it seems that this number should be 604800, the number of seconds in a week. Am I mis-understanding?

Yes, you are totally right. The code was adapted from a sensor that stored the uptime in minutes. You can either divide the ‘uptime’ variable by 60, or you can change the TIME_MAP block of code to:

  {%- set TIME_MAP = {
          'w': (uptime / 604800) % 604800,
          'd': (uptime / 86400) % 7,
          'h': (uptime / 3600) % 24,
          'm': (uptime / 60) % 60,
          's': (uptime % 60)
          }
        -%}

To add to that, the text that was generated before was too verbose for me anyway; the code I’m using now is:

    tn_uptime:
      unique_id: 13acb2d2-df90-467e-9ef8-7dd807804f8b
      friendly_name: "Uptime"
      value_template: >-
        {%- set uptime  = states.sensor.tn_uptime_seconds.state | round -%}
        {%- set sep     = ', ' -%}
        {%- set TIME_MAP = {
          'w': (uptime / 604800) % 604800,
          'd': (uptime / 86400) % 7,
          'h': (uptime / 3600) % 24,
          'm': (uptime / 60) % 60,
          's': (uptime % 60)
          }
        -%}

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

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

        {%- endfor -%}

        {%- if uptime < 1 -%}
          just now
        {%- endif -%}

It also skips the plurals as with the initials it’s not necessary.

Sorry for the late reply, was busy with other things.

2 Likes

Hi,
im having problems here on the pool size, i get on the gui of the truenas and it says:

ONLINE | 39.1 TiB (96%) Used | 1.72 TiB Free

but when i run the command:
curl -X GET -H “Authorization: Bearer 1-H50v3UO1yRLWN4VE8yu6169q8IOlwkDJnzSwe9gWEgJuz18zAJPanlHfTCvjeAa2” "http://192.168.178.254/api/v2.0/pool/id/1"

and i get:
“size”: 31885837205504,
“allocated”: 27610535960576,

Different size and utilization, any idea why am i getting this numbers?

thanks

and does anyone know how to get the memory in use, i looked at the api docs and could not find it.

thanks!

While you might be concerned in monitoring memory usage for FN/TN, this is also completely irrelevant due to the followings:

  • there are multiple components of the memory in use - see below printscreen of top (similar to processor usage, in regard of which 1,5,15m loads are more relevant indicators); stricto senso, you could compute memory in use as total installed memory less free memory (which gets us to the second point that is actually the most important);

image

  • the OS uses, by design, almost the entire physical memory (actually large free memory values would be an inefficiency indicator). As such, except for some very obscure cases, largest performance increase would be obtained by installing as much RAM as the system supports (preferably ECC, but the majority of FN/TN systems in the wild do real good without it).

I guess that you could, instead, monitor the free memory case it goes higher than a certain threshold but it is unlikely that the system would leave large RAM values unused.

and how do i get that to a sensor?? :smiley:

Haven’t looked in detail at the API v2 but I think that it is possible to get virtually any value from FN/TN with command line and custom SNMP OIDs sensors.

it is not in the api , i have cheked, and no idea how to create an oid sensor. have any example?

Check my topic posted here Advanced SNMP monitoring, part two: FreeNAS

1 Like

Hi!

My CPU temp suddenly stopped working. i cant figure out why. any ideas?

Now it only reports unknown.

- platform: rest
  name: TrueNAS_local_backup_cpu_temp
  resource: http://XXX.XXX.X.XX/api/v2.0/reporting/get_data
  headers:
    Authorization: !secret tn2_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) }}

Is there someone that have this currently working?

i also get this in the logs

2021-11-03 20:39:04 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: dict object has no element 1 when rendering '{% 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) }}'

and my version of Truenas is: Version: “TrueNAS-12.0-U6”

This works well in U5; my config is exactly the same as yours. In the change logs for U6 there seem to have been some changes to the graphs for temperature. My guess is that the rest format has changed. Try looking at the return of the REST call on a browser. The template might have to be modified.

EDIT: I just tested the update to U6 and reporting seems broken on my system. There are no CPU temperature graphs on the TrueNAS UI and trying the REST calls yields errors. I guess it’s a TrueNAS bug, unless the calls have changed, but then they would’ve updated the UI as well I guess.

Ok, then we know why. I have tried to search for any related changes and i cant find any. Maybe it gets fixed in the next release. Or maybe raise a issue ticket to Truenas

It was reported a little while ago: https://jira.ixsystems.com/browse/NAS-112690. There is a possible fix in the bug report if you feel adventurous. Or you can revert to U5 for the time being unless U6 brings a feature or bugfix that you were waiting for. Just go to System > Boot and make the U5.1 boot environment the default one. Disclaimer: I’ve done this and it works fine, but do at your own risk.

1 Like

Thank you!

I tried to tinker with the template. But i cant get it to work. Any one knows how to fix this? :slight_smile:

It would definitely help to know what you’ve changed or what you’re trying to do. :smile:

haha, sorry.

I tried to mess with the code i posted eralier to the “fix” they posted in the jira ticket. i changed 0 to 1 on different areas but no change. but this can also be my lack of understanding code.

BR

Looks like this should be fixed in the latest TrueNAS update

TrueNAS CORE 12.0-U6.1 has been released! This is a hotpatch release that includes Samba security releases, a fix for CPU temperature reporting, an adjustment for the default dataset checksum, and a small fix for the Dashboard.