madrose
(Madrose)
December 29, 2018, 5:23pm
1
Hi,
I have a sensor configure to pull JSON from another server. sensor works fine when there is value returned but gives an error when there is a null value. how can avoid the error when there is empty JSON value.
VDRainer
(🍻)
December 29, 2018, 6:08pm
2
Maybe try it with:
value_template: >
{% if value_json is defined %}
{{ value_json['objects'][0].level }}
{% else %}
{{ states('sensor.fn_alert_level') }}
{% endif %}
Not tested!
madrose
(Madrose)
December 31, 2018, 3:43pm
3
The concept worked, but I had to use “None” to indicate an empty return. Thanks
value_template: >
{% if value_json is defined %}
{{ value_json['objects'][0].level }}
{% else %}
{{ "None" }}
{% endif %}
I ran into this too - This should really be in the restful sensor documentation. I ran into it too. It’s especially confusing because the error message is:
“Error fetching data: failed with Server disconnected without sending a response.”
Which isn’t correct; the server returned a 200 response, so saying “no response” is quite false.
I opened a ticket against HA core, we’ll see if its addressed:
opened 07:00PM - 02 Mar 22 UTC
integration: rest
### The problem
When a RESTful sensor pings an endpoint and the endpoint replie… s with a status code 200 and an empty body, the following error is logged:
> "Error fetching data: <url> failed with Server disconnected without sending a response."
This is incorrect, and worse, misleading, because the server in fact returned a 200 (HTTP OK) response.
Perhaps in this case, HA should return a Python `None` object and not log an error? Or perhaps the logged event should be of type Warning?
If the expected behaviour for an empty json response is `null` (which is valid according to the JSON parsers I've used), the error could be amended with:
"If the endpoint intended to return a null value, it should return the string `null` without quotes"
As a followup, it would be nice if the documentation reflected the behaviour, regardless of if this issue is fixed.
### What version of Home Assistant Core has the issue?
core-2022.2.9
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant OS
### Integration causing the issue
rest
### Link to integration documentation on our website
https://www.home-assistant.io/integrations/sensor.rest/
### Diagnostics information
n/a
### Example YAML snippet
```yaml
Not relevant to the issue, but here is an example:
- platform: rest
name: NAS Volume 1 Free Space
resource: http://10.1.1.20/stats/volume1-free
unit_of_measurement: "GB"
value_template: "{{ (value | trim | float / 1024 / 1024) | round(1) }}"
```
```
### Anything in the logs that might be useful for us?
_No response_
### Additional information
Other users have run into this and thankfully there are people who have discussed a possible fix:
https://community.home-assistant.io/t/help-needed-with-rest-when-empty-null-value-return/87735/3
I did a little more digging and it looks like an empty string isn't valid JSON, so maybe such endpoints should return the string `null` (no quotes) - in any event, the message is misleading since the server did send a response along with a 200. Saying it "disconnected without sending a response" is confusing.
One thing to note is how a JSON parser handles an empty string. In my elixir console, I did some testing and it appears that the string null
(without quotes) is valid json, where a zero character string is not (which is what is currently being returned by my endpoints, at least)