State Attributes returning null result

I’m trying to expose attributes from a RESTful sensor that gives me the following JSON:

{'CurrentlyConnected': 1, 'DownloadsInProgress': 0, 'UploadsInProgress': 0, 'WaitingDownloads': 0, 'ConnectionPeak': 1, 'ConnectionCounter': 3, 'DownloadCounter': 0, 'UploadCounter': 0, 'Since': '2022-07-05T09:02:28.195329012Z'}

my config for the rest sensor is as follows:

 - platform: rest
   name: Mobius
   resource: http://192.168.1.12:5603

This generates the sensor correctly and shows everything as expected in HA:

But when I attempt to access the ConnectionCounter attribute for example using state_attr I receive Null.

Any ideas what I’m doing wrong?

You configured the sensor to store the entire payload in its state property. In other words, your sensor is not configured to create entity attributes containing any part of the payload. That’s why your template returns nothing because there’s no attribute named ConnectionCounter.

For more information about an entity’s properties, refer to State Objects.

  1. An entity’s state property is limited to storing 255 characters. If your payload were to ever exceed that limit it will be truncated (thereby causing loss of data and corruption of its JSON structure).

  2. An entity’s state property always stores its value as a string. It doesn’t matter if it’s actually numeric, a list, dict, etc because it will always be handled as a string.

All this to say that you will need to use the REST Sensor integration’s json_attributes option to convert the received payload into actual attributes. In addition, I recommend you use the value_template option to pick the value of one of the received keys for the state property. For example, {{ value_json.Since }} will store the time in state and then you can use json_attributes to store all others in separate attributes.

After doing all of that, your test with the state_attr() function will work.

Let me know if you need more help with json_attributes.

1 Like

Many thanks for your reply, json_attributes was the missing piece of the puzzle, specifying them in the config pulled them through to be exposed via state_attr.

  - platform: rest
    name: Mobius
    resource: http://192.168.1.12:5603
    json_attributes:
      - CurrentlyConnected
      - DownloadsInProgress
      - UploadsInProgress
      - WaitingDownloads
      - ConnectionPeak
      - ConnectionCounter
      - DownloadCounter
      - UploadCounter
      - Since

Many thanks :slight_smile:

I still recommend you don’t store the entire payload in state (for the reason mentioned above).

- platform: rest
    name: Mobius
    resource: http://192.168.1.12:5603
    value_template: '{{ value_json.Since }}'
    json_attributes:
      - CurrentlyConnected
      - DownloadsInProgress
      - UploadsInProgress
      - WaitingDownloads
      - ConnectionPeak
      - ConnectionCounter
      - DownloadCounter
      - UploadCounter
      - Since

Will I need to specify a value_template for all the required json_attributes or is everything added to the value_json.since in the above instance?

No, there’s only one value_template option and it applies exclusively for getting the value for the state property. If you use the suggested template, the entity’s state will simply contain the value of the Since key (2022-07-05T09:02:28.195329012Z) which is less than the 255 character limit.

1 Like