Insert rest sensor into home assistent to read json url

Dear all,
I am new but invested a lot of time and read several days website and the home assistant support site to start with home assistant.
Most things working fine, but I’m not able to include a values (json) from a url into a sensor.

Could you please be so kind and give me some hints ?

URL: https://ospa.iot-center.de/api/123/

I want to get the values “staus: open or closed”, tempature, chlor, ph, redox and time (published at).
I already tried several hours with different code inside the config file (/homeassistant/configuration.yaml).

My best trial was:

rest:
  - resource: http://ospa.iot-center.de/api/123/
    scan_interval: 10
    sensor:
      - name: "status"
        value_template: "{{ value_json.status }}"
        unit_of_measurement: "status"
      - name: "chlor"
        value_template: "{{ value_json.data.chlor }}"
        unit_of_measurement: "chlor"
      - name: "temp"
        value_template: "{{ value_json.data.temp }}"
        unit_of_measurement: "°C"

but the sensor is not showing any measurement value only “unkown”.
Does anyone have an idea what misstake I made ?

Thank you very much !
Best regards
Matthias

Could be that these keys/words are reserved, try [“data”][“temp”]

1 Like

Remove the unit_of_measurement from the status sensor, as that is only valid for numeric sensors.

Change the scan_interval to something longer as you risk being blocked ofr too many queries. Try 120 to start with.

With that done, and the sensors renamed just on my system so that they show up together:

No reserved keywords here, but always better to use bracket notation. Also the URL redirects to HTTPS:

rest:
  - resource: https://ospa.iot-center.de/api/sawoberbeerbach/
    scan_interval: 120
    sensor:
      - name: "status"
        value_template: "{{ value_json['status'] }}"
      - name: "chlor"
        value_template: "{{ value_json['data']['chlor'] }}"
        unit_of_measurement: "chlor"
      - name: "temp"
        value_template: "{{ value_json['data']['temp'] }}"
        unit_of_measurement: "°C"
1 Like

Are you saying that the whole setup crashes because one element was wrong?

1 Like

No, temp and chlor worked on my machine even before I noticed the unit_of_measurement error. I suspect OP has made too many queries and may have to wait a bit…

The unit error is visible in the logs, too:

ValueError: Sensor sensor.status has device class 'None',
state class 'None' unit 'status' and suggested precision 'None'
thus indicating it has a numeric value; however,
it has the non-numeric value: 'closed' (<class 'str'>)
2 Likes

Thank you and all others very much. Oh dear… I invested several hours yesterday evening with getting crazy.

I updated the code as you mentioned. I think I’m still blocked and will wait some minutes/hours. Hope that afterwards everything is working without issues.
Otherwise I’ll come back :wink:

Thank you very much !!

1 Like

I’m still struggling…

Conent of my /homeassistant/configuration.yaml


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes


api:

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


rest:
  - resource: http://ospa.iot-center.de/api/123/
    scan_interval: 600
    sensor:
      - name: "status"
        value_template: "{{ value_json['status'] }}"
      - name: "chlor"
        value_template: "{{ value_json['data']['chlor'] }}"

If i try to use https I will not find any sensor. With http I will be able to see for instance the sensor “sensor.status”. But status is still unkown
Any idea ? Maybe anything in config file is missing or I’ve not made a specific setting in home assistant.

Thanks

Can you access that resource URL from your web browser? Is that browser on the same LAN behind the same router as your HA installation? I wonder if your IP has been blocked.

There’s nothing wrong with your code, as per my working sensors in prior post. If I look at that URL from my HA shell (attached console via Portainer: HA is in a Docker container on my NAS):

NAS:/config# curl http://ospa.iot-center.de/api/sawoberbeerbach/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://ospa.iot-center.de/api/sawoberbeerbach/">here</a>.</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at ospa.iot-center.de Port 80</address>
</body></html>

That’s the redirect to the HTTPS. Trying that in turn:

NAS:/config# curl https://ospa.iot-center.de/api/sawoberbeerbach/
{"status":"opened","data":{"chlor":0.63,"ph":7.08,"redox":874,"temp":23.5},"published_at":"25.06.2024 11:29"}

Is there anything that looks relevant in your error logs?

1 Like

You are my hero !! Thank you very much !!! :heart_eyes:
I delete the raspberry with HO image in my router and restart router, because I had the feeling that anything inside the router was not ok or the website blocked my ip.
Not sure if a new IP or deleting settings of raspi in the router was the solution, but finally it works.

Thank you very much !! I’m so happy… thanks !

1 Like