[homeassistant.helpers.template] Template variable warning: dict object has no element 0 when rendering '{{ value_json[0] }}'

I just noticed that I’m getting hundreds of these errors in my log. I’m not sure when it started.

2021-04-25 22:11:52 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 1 when rendering '{{ value_json[1] }}'
2021-04-25 22:11:52 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 0 when rendering '{{ value_json[0] }}'

It is related to the below rest sensors:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: Horizon C Drive Used
        value_template: '{{ value_json[0] }}'
        json_attributes_path: "$.fs.0"
        json_attributes:
          - percent
        unit_of_measurement: "%"
      - name: Horizon E Drive Used
        value_template: '{{ value_json[1] }}'
        json_attributes_path: "$.fs.1"
        json_attributes:
          - percent
        unit_of_measurement: "%"

I am able to see the respective state attribute of each sensor, for example:
Screenshot 04-27-2021 at 08.51.00 PM

However, when I look at the entity in my Home Assistant card the value is zero:

What am I missing? This exact configuration used to display the value for sensor.horizon_c_drive_used perfectly in my Home Assistant cards. It’s possible updating Home Assistant recently might have broken this. I’m just not sure how to fix it.

I think it has something to do with setting up templates for these sensors. However, I have no clue what I need to do for it to work again;

Thanks in advance!

the rest platform for a sensor you are showing is nothing like what I see in the docs…

sensor:
  - platform: rest
    resource: http://192.168.1.2:61208/api/3/all
    name: Horizon C Drive Used
      value_template: '{{ value_json[0] }}'
      json_attributes_path: "$.fs.0"
      json_attributes:
        - percent
      unit_of_measurement: "%"
  - platform: rest
    resource: http://192.168.1.2:61208/api/3/all
    name: Horizon E Drive Used
      value_template: '{{ value_json[1] }}'
      json_attributes_path: "$.fs.1"
      json_attributes:
        - percent
      unit_of_measurement: "%"

At least that is how mine are configured…

Yeah there’s a new rest platform:

Do you have a sample of the returned data?

If not, try it with just this to get the data structure:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: Horizon C Drive Used
        value_template: '{{ value_json }}'

That’s pretty cool! I might migrate to that as I have multiple sensors on same endpoint/resource…

It’s based off the the suggested REST config by @ bdraco

Here:

I haven’t changed my configuration since he helped me to get it working. Pretty much all my REST sensors are configured that way; and work great. It’s only these two sensors that stopped working. Unfortunately, since that Github issue is closed, I have no way to ask him why it stopped working

I’ve been thinking about doing it but I only have a few sensors wit the same resource.

This is the relevant returned data from the API endpoint:

  "fs": [
    {
      "device_name": "C:\\",
      "fs_type": "NTFS",
      "mnt_point": "C:\\",
      "size": 255057563648,
      "used": 130684645376,
      "free": 124372918272,
      "percent": 51.2,
      "key": "mnt_point"
    },
    {
      "device_name": "E:\\",
      "fs_type": "NTFS",
      "mnt_point": "E:\\",
      "size": 8001542946816,
      "used": 2653830512640,
      "free": 5347712434176,
      "percent": 33.2,
      "key": "mnt_point"
    }
  ],

The only thing different between these two REST sensors and all the other similar ones that work is… all the other ones have respective templates to do calculations.
For example, this works fine:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: diskio_0
        value_template: '{{ value_json[0] }}'
        json_attributes_path: "$.diskio.0"
        json_attributes:
          - time_since_update
          - disk_name
          - read_bytes
          - write_bytes
      - name: diskio_1
        value_template: '{{ value_json[1] }}'
        json_attributes_path: "$.diskio.1"
        json_attributes:
          - time_since_update
          - disk_name
          - read_bytes
          - write_bytes

sensor:
  - platform: template
    sensors:
      horizon_c_drive_read_bytes:
        value_template: >-
                {% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
                {% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
                {% if (read_byte != None ) and (time_since != None ) %}
                  {{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_0") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_c_drive_write_bytes:
        value_template: >-
                {% set write_byte = state_attr('sensor.diskio_0', 'write_bytes') %}
                {% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
                {% if (write_byte != None ) and (time_since != None ) %}
                  {{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_0") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'

That doesn’t match with the sample data you’ve posted. Try:

sensor:
  - platform: rest
    resource: http://192.168.1.2:61208/api/3/all
    name: Horizon C Drive Used
    value_template: "{{ value_json.fs.0.percent }}"
    unit_of_measurement: "%"
2 Likes

Thank you. That fixed it!

@tom_l (or anyone here). I’m still getting the log errors below continuously; although, I did fix a completely different issue. :slight_smile:

2021-04-28 20:04:25 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 0 when rendering '{{ value_json[0] }}'
2021-04-28 20:04:36 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: dict object has no element 0 when rendering '{{ value_json[0] }}'

I only have two other REST sensors that use '{{ value_json[0] }}' & '{{ value_json[1] }}'… they are below. What could be causing these continuous errors? All my sensors seem to display correctly in Lovelace cards, at least.

      - name: diskio_0
        value_template: '{{ value_json[0] }}'
        json_attributes_path: "$.diskio.0"
        json_attributes:
          - time_since_update
          - disk_name
          - read_bytes
          - write_bytes
      - name: diskio_1
        value_template: '{{ value_json[1] }}'
        json_attributes_path: "$.diskio.1"
        json_attributes:
          - time_since_update
          - disk_name
          - read_bytes
          - write_bytes

The respective returned data is:

  "diskio": [
    {
      "time_since_update": 11.011141300201416,
      "disk_name": "PhysicalDrive0",
      "read_count": 37,
      "write_count": 119,
      "read_bytes": 227840,
      "write_bytes": 36214784,
      "key": "disk_name"
    },
    {
      "time_since_update": 11.011141300201416,
      "disk_name": "PhysicalDrive1",
      "read_count": 238,
      "write_count": 0,
      "read_bytes": 28854272,
      "write_bytes": 0,
      "key": "disk_name"
    }
  ],

PS: In case it’s relevant, below, are the respective templates for these two sensors:

sensor:
  - platform: template
    sensors:
      horizon_c_drive_read_bytes:
        value_template: >-
                {% set read_byte = state_attr('sensor.diskio_0', 'read_bytes') %}
                {% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
                {% if (read_byte != None ) and (time_since != None ) %}
                  {{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_0") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_c_drive_write_bytes:
        value_template: >-
                {% set write_byte = state_attr('sensor.diskio_0', 'write_bytes') %}
                {% set time_since = state_attr('sensor.diskio_0', 'time_since_update') %}
                {% if (write_byte != None ) and (time_since != None ) %}
                  {{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_0") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_e_drive_read_bytes:
        value_template: >-
                {% set read_byte = state_attr('sensor.diskio_1', 'read_bytes') %}
                {% set time_since = state_attr('sensor.diskio_1', 'time_since_update') %}
                {% if (read_byte != None ) and (time_since != None ) %}
                  {{ ((read_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_1") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_e_drive_write_bytes:
        value_template: >-
                {% set write_byte = state_attr('sensor.diskio_1', 'write_bytes') %}
                {% set time_since = state_attr('sensor.diskio_1', 'time_since_update') %}
                {% if (write_byte != None ) and (time_since != None ) %}
                  {{ ((write_byte | int) / (time_since | int) / 1000000) | round (2) }}
                {% endif %}
        availability_template: >-
                {{ states("sensor.diskio_1") not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: 'MB/s'

When I see these kinds of errors I use dev-tools to troubleshoot them.
Set the returned json to a variable and then a template to extract data - they you aren’t continually restarting,

Instead of ‘{{ value_json[0] }}’ have you tried ‘{{ value_json.0 }}’ ?

It didn’t make any difference changing ‘{{ value_json[0] }}’ to ‘{{ value_json.0 }}’ . Unfortunately, I’m not sure what specifically to do in dev-tools. The only thing I knew how to do was paste sensor.diskio_0 templates in the Template Editor; which works perfectly.

I’m not sure what else to do. I’m hoping you or someone else would please tell me what to do based on the configuration and returned data I posted.

The odd thing is all my REST sensors and respective entities are populating fine in Lovelace cards. However, I’m getting so many of these errors in my log that it’s starting to slow down Home Assistant performance.

If the sensor is getting a valid state from the json then it’s likely something else generating the error.

Actually, none of the sensors here are displaying a state in Dev Tools. I vaguely remember that someone told me that I’m hitting a character limit… the respective REST JSON response is relatively huge. However, I’m not sure how to suppress these errors in my log.

Screenshot 04-28-2021 at 09.24.05 PM

Screenshot 04-28-2021 at 09.27.22 PM

Screenshot 04-28-2021 at 09.28.23 PM

See here:

Scroll past the first three codes blocks and there’s an example to extract all the data in a single call and get around the character limit.

I think I figured out what was wrong. There was a breaking change in Home Assistant Core 2021.4 where it will generate these errors if you have a RESTful config such as:

rest:
  - resource: http://192.168.1.2:61208/api/3/all
    sensor:
      - name: diskio_0
        value_template: '{{ value_json[0] }}'
        json_attributes_path: "$.diskio.0"
        json_attributes:
          - time_since_update
          - disk_name
          - read_bytes
          - write_bytes
      - name: diskio_1
        value_template: '{{ value_json[1] }}'
        json_attributes_path: "$.diskio.1"
        json_attributes:
          - time_since_update
          - disk_name
          - read_bytes
          - write_bytes

I had to change lines like this:

value_template: '{{ value_json[0] }}'
value_template: '{{ value_json[1] }}'

to this:

value_template: '{{ value_json[0] | default }}'
value_template: '{{ value_json[1] | default }}'

It seems like every time I upgrade Home Assistant Core, something usually breaks.

3 Likes