I’m trying to read some JSON values from Meteoalarm rss feed. Basically it works like intended, though drops lots of errors into log file if no alarm found for searched area. Here is problematic rest sensor:
- platform: rest
resource: https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-rss-poland
name: meteoalarm_index
value_template: >-
{{ -1 }}
{% for x in range(value_json.rss.channel.item | length) %}
{% if value_json.rss.channel.item[x].title == 'Mazowieckie Province Wołomiński County' %}
{{ x }}
{% endif %}
{% endfor %}
By default I’m returning -1 (if no entry for my region found in the feed) and index of entry in the feed (if entry found). This part works as supposed to. Problem is, that each time when sensor is refreshed and no entry is for my area is found it logs error to log file (means every 30 seconds ):
2022-08-21 10:44:21.003 WARNING (MainThread) [homeassistant.components.rest.sensor] JSON result was not a dictionary or list with 0th element a dictionary
Is there any way to build the sensor in the way that this error is avaided? Obviously I can filter out this in logger settings, but this will prevent any future real errors to be shown for other sensors too… Any idea?
I tried to look at the url-output but this seems not to be a json and this is (for me) not a known area.
EDIT: what I seem to read from your script is that it would output below when finding Warsaw area in 2nd row …is that correct?
-11
Then it would kick back only -1 if it is not found
And if no connection/data then you will get an error
@browetd - you’re right… I changed search for something that is present in the feed and error is still there. Apparently I was confused, since I’ve not seen this error previously and meteoalarm sensor is the only rest one I recently added to my configuration… The question though is if there is ny method to find more about this error and which of other rest sensors throuws this error… @vingerha - you are righ, output of this rss feed is pure xml, buut json parser in HA does automatic conversion from xml to json format when reading such file, so no additional steps are required. And you’re right… I also noticed that my logic was slightly wrong and that {{ x }} statement not creates output from template, but add to it… So insted of single index value I receive ‘-1 1’ from this template. Have to make small change :-). I tried to correct this, but run into other problem. Here is how the sensor looks now:
- platform: rest
resource: https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-rss-poland
name: meteoalarm_index
value_template: >-
{% set ind = -1 %}
{% for x in range(value_json.rss.channel.item | length) %}
{% if value_json.rss.channel.item[x].title == 'Wielkopolskie Province Wolsztyński County' %}
{% set ind = x %}
{% endif %}
{% endfor %}
{{ ind }}
So as you can see first I set value of ind to -1 and then, in the loop, correct it to actual index value, if matching data found. But the problem is that this template always return -1, so looks like statement {% set ind = x %} is not executed…
This works, I believe your example is crashing on the ń so you need to see how to format it (no direct clue myself as never had this before)
- platform: rest
resource: https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-rss-poland
name: meteoalarm_index
value_template: >-
{% set ind = -1 %}
{% set y = (value_json.rss.channel.item | length) %}
{% for x in range(0,y) %}
{% if value_json.rss.channel.item[x].title == 'Opolskie Province Kluczborski County' %}
{{x}}
{% endif %}
{% endfor %}
My guess is the characterset/coding…a bit different test, below returns “Opolskie Province Kluczborski County” as state value but if you set x==3 it returns not even a sensor.
- platform: rest
resource: https://feeds.meteoalarm.org/feeds/meteoalarm-legacy-rss-poland
name: meteoalarm_index
value_template: >-
{% set y = (value_json.rss.channel.item | length) %}
{% for x in range(0,y) %}
{% if x == 2 %}
{{ value_json.rss.channel.item[x].title }}
{% endif %}
{% endfor %}
Hence I believe the whol script fails when non ascii(does it need ascii) characters are returned
Correction,… it just updated with a sensor and the non-ascii characters…very odd
The x==2 worked immediately, x==3 took a minute or so…before that it showed no sensor.
… back to thinking
And check this… set variables within loop does not work as you expect
Scoping Behavior
Please keep in mind that it is not possible to set variables inside a block and have them show up outside of it. This also applies to loops. The only exception to that rule are if statements which do not introduce a scope. As a result the following template is not going to do what you might expect