Unable to filter json based on mmsi

Hello,
I have a docker running which returns ship data.
I have been using rest_command to query ships within a specific geofence.
I have however been unable to use that data, only run it manually as a service.
Have tried to run same query through as a rest_sensor or a rest_binary_sensor but fails.

Here is the rest_command that works:

ais_information:
  url: http://10.10.10.100:9101/api/tracks/within_geojson
  method: 'POST'
  payload: '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[10.582742552132572,59.68073946853113],[10.582742552132572,59.642878218853696],[10.648754933525879,59.68073946853113],[10.582742552132572,59.68073946853113]]]}}]}'
  content_type: 'application/json; charset=utf-8'

When trying something similar in rest_sensor it fails:

- platform: rest
  name: AIS information Test X
  resource: http://10.10.10.100:9101/api/tracks/within_geojson
  method: 'POST'
  scan_interval: 30
  payload: '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[10.582742552132572,59.68073946853113],[10.582742552132572,59.642878218853696],[10.648754933525879,59.68073946853113],[10.582742552132572,59.68073946853113]]]}}]}'
  headers:
    User-Agent: Home Assistant
    Content-Type: application/json
- platform: template
  sensors:
    ais_information_ship_test_x:
      friendly_name: AIS information ship
      value_template: "{{ state_attr('sensor.ais_tracker', 'features')[0].properties.mmsi == '2581960000'}}"

Could please someone help? I’m almost giving up, would be nice to get this landed so our family could get a notification when specific ships enter our fiord

Small steps, first make sure that the rest sensor works, add a value_template as per examples and possibly also json_attributes (with path)
RESTful Sensor - Home Assistant (home-assistant.io)

One of the problems is that it returns more than the allowed limit of 255 characters.

Examle of output from rest_command you can see in first post.
Have tried to find a solution to split the response.
But unable to work.
Any pointers would be much appreciated.

- platform: rest
  name: AIS information
  resource: http://10.10.10.100:9101/api/tracks/within_geojson
  method: 'POST'
  scan_interval: 30
  payload: '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[10.582742552132572,59.68073946853113],[10.582742552132572,59.642878218853696],[10.648754933525879,59.68073946853113],[10.582742552132572,59.68073946853113]]]}}]}'
  headers:
    User-Agent: Home Assistant
    Content-Type: application/json
- platform: template
  sensors:
    ais_information_drobak_0:
      friendly_name: AIS information ship Drobak 0
      value_template: "{{ value_json.split(',')[0].split(':')[1] }}"

Maybe not clear enough but stop with the template before you have the REST working, it might be cluttering results.
Add value template to the REST sensor and possibly some attribs… above link shows ideas on how-to

You were clear enought. The latest template is an effort to split the result so it doesn’t exceed 255 characters.

OK…you do not clearly describe what you want ot get out of the response and into a sensor (more?) … as a part-time clarivoyant I now guess that you want to put things in the state-value(s?) and not the attributes?
Another option is that you use a command_line and curl and put all in the attribute, from there on you can create other sensors, an example of mine below

command_line
  - sensor:
        name: flights_from_to
        unique_id: whatever
        command: >
             echo "{\"events\":" $(
             curl 
             -s 
             'https://api.fingrid.fi/v1/variable/336/events/json?start_time={{ (now()-timedelta(hours=4)).strftime('%Y-%m-%dT%H:%M:%SZ') }}&end_time={{ (now()+timedelta(hours=12)).strftime('%Y-%m-%dT%H:%M:%SZ') }}'
             ) "}" 
        value_template: > 
            {{ value_json.events | length }}
        json_attributes:
            - events

Another option is to use curl and JQ and tear it apart into flat ordered data and then put it in attribs / state

Hi again,

Thanks for the reply.
Thought it was clear by the code and inital post that I want to get a a status if a ship (mmsi) is within geofence. Running rest command I’m able to get a response with ships within fence. Trying to do the same with rest_sensor fails due to response exceeds 255 characters. Last attempt is to split the result to be within 255 characters.

I have no clue why it would fail if (!) you have a proper path to that key/value.
Else, again…use the curl setup

EDIT: you do NEED a value in the rest sensor of course, else it will try to take all as state-value

As I have several times mentioned it fails due to exceeding 255 characters. So do you have a solution to split the respond ti be within 255 characters or in other way find a solution besides rest_sensor that can send and receive result. A possible solution is to use curl command.

WITH the value_template in the rest sensor, yes

- platform: rest
  name: AIS information
  resource: http://10.10.10.100:9101/api/tracks/within_geojson
  method: 'POST'
  scan_interval: 30
  value_template: "{{ value_json.split(',')[0].split(':')[1] }}"
  payload: '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[10.582742552132572,59.68073946853113],[10.582742552132572,59.642878218853696],[10.648754933525879,59.68073946853113],[10.582742552132572,59.68073946853113]]]}}]}'
  headers:
    User-Agent: Home Assistant
    Content-Type: application/json

Thanks, it was what I was trying previous in this thread.
Finally got it working.
Sharing the result should any other be interested.

- platform: rest
  name: AIS information
  resource: http://10.10.10.100:9101/api/tracks/within_geojson
  method: 'POST'
  scan_interval: 30
  value_template: "{{ ('value_json', 'content').split(',')[0]}}"
  payload: '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[10.582742552132572,59.68073946853113],[10.582742552132572,59.642878218853696],[10.648754933525879,59.68073946853113],[10.582742552132572,59.68073946853113]]]}}]}'
  json_attributes:
    - "features"
  headers:
    User-Agent: Home Assistant
    Content-Type: application/json
- platform: template
  sensors:
    ais_information_drobak_0:
      friendly_name: AIS information ship Drobak 0
      value_template: "{{ state_attr('sensor.ais_information', 'features')[0].properties.mmsi }}"