Extracting data from JSON array from rest sensor that is longer than 255 characters

Hello, all I am trying to integrate my Minecraft server into home assistant using a plugin called “ServerTap” which hosts a rest API alongside the server. So far I have been able to successfully pull data using restful sensors for example, online players player names and server health info but I have gotten stuck for awhile on trying to pull more data. The API allows me to set a get request to “SERVER_ADDRESS/v1/players/all” and it sends back a json array of players that have ever joined the server in the format shown below.

[
  {
    "uuid": "string",
    "whitelisted": true,
    "banned": true,
    "op": true,
    "balance": 0,
    "displayName": "string"
  },
  {
    "uuid": "string",
    "whitelisted": true,
    "banned": true,
    "op": true,
    "balance": 0,
    "displayName": "string"
  }
]

I am using the template below to concatenate the values from a similar get request for the players online to form the state which I can use later.

{% for players in value_json -%}
  {{ players['displayName'] }}
  {%- if not loop.last %}, {% endif -%}
{%- endfor %}

The issue I am facing is that I have about 10ish players that have ever joined and when I concatenate all the names it passes the 255 character limit on the state. I figured I could put the values into the attributes but there doesn’t appear to be a way to accomplish this as the top level of the json response is an array.

looking at some other posts I have seen a few people with similar issues from a few years back that never ended up being solved or the solution won’t work for me like here: https://www.reddit.com/r/homeassistant/comments/k5chlv/handling_an_array_of_json_objects_in_a_rest_sensor/ and https://community.home-assistant.io/t/restful-sensor-parse-nested-json-to-sensor-attributes/96270.

The responses to those posts seem to infer that this is not possible. Is there any way I am missing to solve this like a way to extract attributes using templates or something similar?

The workaround would be to use NodeRed, use a HTTP IN node that you can call, and do all the processing in NodeRed and return only what you want to the HTTP Response node.

Thanks. I was able to make it work that way. I hadn’t even considered using NodeRed to do this before but I worked like a charm.

1 Like

Hi ,
Possible alternative solution.
I faced same issue by getting all google calendar of current day which come as a json array and store them in a sensor (by default only next callendar event is stored in the calendar attribute…)

I did fix it by using command line sensor to get the json array in a json with one direct attribute.
This way it is possible to have in json_attributes one attribute which contain all the array and can be used for later porcessing by any sensor or automation.
In value_template I store the number of entries in the array

Here how I did it (API call for calendar take a bit of time so tke some seconds to see the attributes populated the first time) :

- platform: command_line
  name: calendar_events
  scan_interval: 1500
  command: >
     echo "{\"events\":" $(
     curl 
     -s 
     -H 'Authorization: Bearer XXX'
     'http://192.168.0.50:8123/api/calendars/calendar.mycalendar_gmail_com?start={{ now().strftime('%Y-%m-%dT%H:%M:%SZ') }}&end={{ (now() + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ') }}'
     ) "}" 
  value_template: > 
     {{ value_json.events | length }}
  json_attributes:
     - events

hope this can help someone else…

7 Likes

That’s brilliant! Thanks for sharing!

Thank you. This worked beautifully for me in getting all of my “To-Do” items from Todoist.