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) }}
@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:
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?
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);
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.
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.
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.
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.
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.