Numeric data from rest sensor... I feel lost

For my NAS I created script that scans periodically all shares and provide me with information about storage consumption. Result is send to json file. Then on HA side I read this file and populate set of sensors with shares’ names and sizes. So far so good… The issue is that value for share size seems to be text, not number, so I can’t use this in cards like ApexChart, that I wanted to use to show storage consumption split.
Here is how the sensor code looks like (one for name and one for size of share):

sensor:
  - platform: rest
    resource: http://192.168.52.30/storage.json
    name: share_name_1
    value_template: '{{ value_json.data[1].share_name }}'
  - platform: rest
    resource: http://192.168.52.30/storage.json
    name: share_size_1
    value_template: '{{ value_json.data[1].share_size }}'

This returns proper values of share size, but share size it is treated as text (so ApexChart shows zero value). I tried several variations:

value_template: '{{ value_json.data[1].share_size | float }}'

or

value_template: >-
  {{ value_json.data[1].share_size }}

or

value_template: >-
  {{ value_json.data[1].share_size | float }}

or

value_template: >
  {{ value_json.data[1].share_size }}

I even tried to create separate template sensor to convert this text to number:

  - platform: template
    sensors:
      share_size_numeric_1:
        friendly_name: 'Share Size Numeric 1'
        value_template: >
          {{ (states('sensor.share_size_1') | float) }}
        unit_of_measurement: 'MB'

But nothing works… ApexChart shows all values as zero. The same with template editor, I can’t perform any mathematical operation on these sensors without adding float filter - they are treated as text…

So I’m scratching my head and cannot find some obvious error I’m doing here… please help!

THe problem is that all states are strings, and never anything else.

You might be able to set the value you want to track as an attribute of a sensor. And since attributes can be anything and aren’t limited to just being strings you could use that attribute in the card as a number.

Hm… surprise… does this relate to rest sensors only? I have plenty of sensors and use them for charting of data based on their state, without any prior processing. Some are custom (snmp or template sensors), some are coming from integrations.

All sensor states are strings.

Give your sensors a unit_of_measurement for core charts to work. Not sure about the Apex charts.

sensor:
  - platform: rest
    resource: http://192.168.52.30/storage.json
    name: share_name_1
    value_template: '{{ value_json.data[1].share_name }}'
  - platform: rest
    resource: http://192.168.52.30/storage.json
    name: share_size_1
    value_template: '{{ value_json.data[1].share_size }}'
    unit_of_measurement: 'MB' # or '%' or whatever the unit is.
2 Likes

HA! I got the mistake I made! I falsely assumed, that if I want to present static data (only current state of storage consumption, not the history) I do not need to include sensors in recorder… seems I was wrong. Thanks for hint with standard core charts! This led me to include sensors in history and voila! Now I got proper data in Apex too!