knuthp / spartid-ais

Ship positions using AIS data in Norway
2 stars 6 forks source link

How to reuse data from rest_command in Home Assistant #10

Open mixedbreed opened 2 weeks ago

mixedbreed commented 2 weeks ago

Hi again! After much try and error I'm still not able to use the information provided by rest_command. Rest command does not return values, so I'm only able to see the result when manually running the rest_command as a service. When I try to reuse it in a sensor it fails again with following message: <!doctype html> 415 Unsupported Media Type

Unsupported Media Type

Did not attempt to load JSON data because the request Content-Type was not 'application/json'.

Could you please give some tips of how I'm going to reuse the reply from rest_command or other sensor to run a template alla this?: "{{ value_json.features[0].properties.mmsi == 257169200 }}"

Screenshot from restful command: image

Originally posted by @mixedbreed in https://github.com/knuthp/spartid-ais/issues/4#issuecomment-2308682524

mixedbreed commented 2 weeks ago

Found a solution and shares it here :)

- platform: rest
  name: AIS information
  resource: http://xx.xx.xx.xx9101/api/tracks/within_geojson
  method: 'POST'
  scan_interval: 30
  value_template: "{{ ('value_json', 'content').split(',')[0]}}"
#  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]]]}}]}'
  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 }}"