Empty response from the REST API on history endpoint when end_time is used

Hi,
I’m trying to fetch historical data from the HA REST API and I get an empty response when using the end_time parameter.
The API works when end_time is not passed, in:

$ curl -X GET --insecure  -H "Authorization: Bearer `cat .secret`" -H "Content-Type: application/json"
"https://192.168.1.xx:8123/api/history/period/2022-12-19T00:00:00+02:00?filter_entity_id=switch.a_switch"

But when I try to get e.g., the data for 3 days I get an empty response [ ] :

$ curl -X GET --insecure  -H "Authorization: Bearer `cat .secret`" -H "Content-Type: application/json"
"https://192.168.1.xx:8123/api/history/period/2022-12-19T00:00:00+02:00?filter_entity_id=switch.a_switch?end_time=2022-12-22T00%3A00%3A00%2B02%3A00" ; echo
[]

Any help?

I experience empty responses with the /history endpoint as well. However, it happens only when setting the start date to far back in the past (greater than 20 days).

Following request gives me a response, setting the timestamp and end_time one day earlier gives me an empty response.

'history/period/2022-12-12T00:00:00+01:00?minimal_response&end_time=2022-12-14T00%3A00%3A00%2B01%3A00&filter_entity_id=sensor.energy_consumption_tarif_1,sensor.energy_consumption_tarif_2'

Perhaps the empty response for >20 days in the past in your case is related to retention period configuration? see here

However, my problem is different: I get empty response when using the end_time, when retrieving data for different than 1 day period (the default period). When I want to get data for, e.g., a period of 3 days, or even half a day, setting the end_time, I get empty response. This happens for many entities I tried with and for dates that I checked that there are available data to retrieve.

Do you see any problem in the URL I use?
This happens on Home Assistant Core 2022.12.6.

Indeed, your are right @athanassis. Figured that one out myself after my post. I should have looked more carefully at the docs.

However, taking a look at your curl call I think I can spot your problem. You should use the ? symbol once to indicate there are query options to the call. In order to concatenate multiple query options, use the & symbol. Please try the following call;

$ curl -X GET --insecure  -H "Authorization: Bearer `cat .secret`" -H "Content-Type: application/json"
"https://192.168.1.xx:8123/api/history/period/2022-12-19T00:00:00+02:00?filter_entity_id=switch.a_switch&end_time=2022-12-22T00%3A00%3A00%2B02%3A00" 

Thakns @dremsol!
This was the problem indeed. Silly mistake but second pair of eyes is always hepful!
Basic URL query string formatting :slight_smile:

1 Like