Right YAML Code REST Sensor

A service provider offers me a WEB interface in JSON format via HTTP POST.
Here is the available example listing for a query.

{
"name": “sonnengang”,
"abfragekoordinaten":
{
"rechtswert": 497884.6,
"hochwert": 407730.3,
"CRS": 31287
},
"output": "Horizont"
}

What is the correct YAML code for a REST sensor in Home Assistant?

Have you tried the RESTful sensor?

It would be something like this:

# Example configuration.yaml entry
sensor:
  - platform: rest
    resource: http://IP_ADDRESS/ENDPOINT  # change this accordingly to your case
    method: POST
    payload: '{ "device" : "heater" }'  # change this accordingly to your case. Or remove it if a payload is not needed.
    value_template: "{{ value_json.output }}"
    json_attributes:
      - name
      - abfragekoordinaten

By the way, are you aware of the Sun integration?
You probably already have that (look for an entity called sun.sun in your system) and it have the info about Sun position (above horizon or below horizon).

That would depend on what data you want as states or attributes.

Thanks for your answer.
So i try this code.
But i get no value.

sensor:

  - platform: rest
    resource: https://voibos.rechenraum.com/voibos/voibos
    method: POST
    name: Sonnengang
    value_template: '{{ value_json.result }}'
    scan_interval: 300
    headers:
      Content-Type: application/json
    payload: >
      {
        "name": "sonnengang",
        "abfragekoordinaten": {
          "rechtswert": 46339.35,
          "hochwert": 316211.87,
          "CRS": 31255
        },

Not sure how to help here. Any attempt I make to that URL gets an HTTP 502 Bad Gateway error.

As a starting point, your payload is either pasted incomplete, or incorrectly structured: you’re missing a close-bracket.

troon@pi:~$ curl -X POST -d '{"name": "sonnengang", "abfragekoordinaten": {"rechtswert": "46339.35","hochwert": "316211.87","CRS": "31255"}}' https://voibos.rechenraum.com/voibos/voibos
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

I only have this description. No idea how I can implement this correctly with the REST sensor.

With an abfrage.json of:

{
        "name": "sonnengang",
        "abfragekoordinaten": {
                "rechtswert": 46339.35,
                "hochwert": 316211.87,
                "CRS": 31255
        }
}

this command:

troon@pi:~$ curl -X POST -H "Content-Type: application/json" -H "User-Agent: curl" -d @abfrage.json https://voibos.rechenraum.com/voibos/voibos

with or without the -X POST, gives a huge HTML response with inline data images.

I’ve tried this, but I’m getting bad gateway also…

2023-03-02 14:34:37.690 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: <html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>
2023-03-02 14:34:37.690 WARNING (MainThread) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2023-03-02 14:34:37.691 DEBUG (MainThread) [homeassistant.components.rest.sensor] Erroneous JSON: <html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>
2023-03-02 14:34:37.691 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.abfragestatus }}'

If you try this site you will see a link for a request in a different way, but it is requesting more paramenters than you have shared with us.

That shows a result (HTML), but there are a link for a json result right above the table:


On the inspect you see all the parameters used. Maybe you should try providing all of those.

I’ve this this (with some of the parameters) but got the bad gateway response reported above:

sensor:
  - platform: rest
    resource: https://voibos.rechenraum.com/voibos/voibos
    method: POST
    name: Sonnengang
    value_template: '{{ value_json.abfragestatus }}'
    json_attributes:
      - abfragekoordinaten
      - abfragehoehe
      - datengrundlage
      - flugjahr
      - voibos
      - horizont
    scan_interval: 300
    headers:
      Content-Type: application/json
    params:
      crs: 31255
      koordinate: "46339.35,316211.87"
      name: sonnengang
      output: JSONDownload

When I click at that link then I get a json (too big to be here).

Try to play if those parameters.
Enable your logger for REST and you will have more detailed log to help you:

logger:
  logs:
    homeassistant.components.rest.sensor: debug

the “output” is an obligat parameter.
Ok, and how should the YAML code look like?

Yeah, I see that. This gives a huge JSON response:

troon@pi:~$ curl -H "Content-Type: application/json" -d '{"crs":"31255","koordinate":"46339.35,316211.87","name":"sonnengang","output":"JSONDownload"}' https://voibos.rechenraum.com/voibos/voibos

that looks like this, with azimuth 2–358° items removed from the middle:

{
        "abfragestatus": "erfolgreich",
        "abfragekoordinaten":
        {
                "rechtswert": 46339.4,
                "hochwert": 316211.9,
                "CRS": 31255
        },
        "abfragehoehe": "439.1 (+2.0)",
        "datengrundlage": "Laserscanning Höhenmodell 2022 - geoland.at",
        "flugjahr": "2014",
        "voibos": "v2021.06a-voibos1-build-Feb  3 2022-08:50:18",
        "horizont": [
                {
                        "azimuth": 0,
                        "hoehenwinkelDTM": 0.6,
                        "hoehenwinkelDSM": 57.2,
                        "entfernungDTM": 1263.6,
                        "entfernungDSM": 11.6,
                        "hoehenwinkelAbfragedatum": "n/a",
                        "UhrzeitSonnengangMEZ": "n/a",
                        "UhrzeitSonnengangMESZ": "n/a",
                        "hoehenwinkelSommersonnwende": "n/a",
                        "hoehenwinkelWintersonnwende": "n/a"
                },
                {
                        "azimuth": 1,
                        "hoehenwinkelDTM": 0.6,
                        "hoehenwinkelDSM": 57.7,
                        "entfernungDTM": 1261.6,
                        "entfernungDSM": 11.6,
                        "hoehenwinkelAbfragedatum": "n/a",
                        "UhrzeitSonnengangMEZ": "n/a",
                        "UhrzeitSonnengangMESZ": "n/a",
                        "hoehenwinkelSommersonnwende": "n/a",
                        "hoehenwinkelWintersonnwende": "n/a"
                },
                {
                        "azimuth": 359,
                        "hoehenwinkelDTM": 0.6,
                        "hoehenwinkelDSM": 56.6,
                        "entfernungDTM": 83863.2,
                        "entfernungDSM": 11.6,
                        "hoehenwinkelAbfragedatum": "n/a",
                        "UhrzeitSonnengangMEZ": "n/a",
                        "UhrzeitSonnengangMESZ": "n/a",
                        "hoehenwinkelSommersonnwende": "n/a",
                        "hoehenwinkelWintersonnwende": "n/a"
                }

        ]
}

What data do you want from it?

@almtaler, we are still wait for your answer to @Troon’s question, so we can choose what to go to the sensor status.
In the meanwhile I put all the result as attributes:

sensor:
  - platform: rest
    name: Sonnengang
    resource: https://voibos.rechenraum.com/voibos/voibos
    method: POST
    payload: '{"crs":"31255","koordinate":"46339.35,316211.87","name":"sonnengang","output":"JSONDownload"}'
    value_template: '{{ value_json.abfragestatus }}'
    json_attributes:
      - abfragestatus
      - abfragekoordinaten
      - abfragehoehe
      - datengrundlage
      - flugjahr
      - voibos
      - horizont
    scan_interval: 300
    headers:
      Content-Type: application/json

And it is working fine for me:

Thanks Troon!
I need only the data “horizont”.

But Horizont is the huge list.

If you look at tye sensor I’ve shared above, horizont is there as an attribute. It is too big to be on State as it is limited to 255 chars.

1 Like