I can’t, for the life of me, figure out what i’m doing wrong here, but i’m sure i’m missing something obvious…
When i try this test Jinja template in the Template tab:
{% set foo = states.geo_location %}
{{ foo | map(attribute='name') | list }}
i get back the list of geo_location domain states as expected:
['M 3.4 - 1km SSW of Kasina, Croatia', 'M 4.6 - 4km WSW of Kasina, Croatia', 'M 5.4 - 3km SW of Kasina, Croatia']
But if i use it in an actual template sensor, i get an empty list:
[]
This happens even if i use the entity_id: sensor.time
trick to ensure it’s getting updated.
Any ideas?
PS: Code above is just an example i’m using for debugging purposes… What i’m actually trying to do is something like this:
{% set time = states.geo_location | map(attribute='attributes.time') | max %}
{% set sensor = (sensors | selectattr('attributes.time','eq', time) | list)[0] %}
{{state_attr(sensor.entity_id, "magnitude")}}
… i.e. get the latest earthquake from usgs_earthquakes_feed
and use the magnitude as the template sensor value.
PPS: Nothing useful in the logs, no errors.
There is no sensors
defined in your template. If you replace this with states.geo_location
you should get a result.
I just tested the following successfully, and had to add a filter on geolocation source because I am using multiple geolocation integrations:
{% set time = states.geo_location | selectattr('attributes.source', 'eq', 'usgs_earthquakes_feed') | map(attribute='attributes.time') | max %}
{% set sensor = (states.geo_location | selectattr('attributes.time','eq', time) | list)[0] %}
{{state_attr(sensor.entity_id, "magnitude")}}
Sorry, i dropped a line when copy pasting:
{{ set sensors = states.geo_location }}
It doesn’t make a difference.
I’ve only provided that snippet for context, check out the simplified debug snippet at the top of the post - it’s not missing it, and in fact, the whole problem is that states.geo_location generator returns an empty list when running within a sensor (vs. template tester).
OK, understood.
Just for comparison, I’ve just done another test and used a different geolocation integration - GeonetNZ Quakes - and got the expected result:
sensor:
- platform: template
sensors:
latest_earthquake:
friendly_name: "Latest Earthquake Magnitude"
value_template: >-
{% set time = states.geo_location | selectattr('attributes.source', 'eq', 'geonetnz_quakes') | map(attribute='attributes.time') | max %}
{% set sensor = (states.geo_location | selectattr('attributes.time','eq', time) | list)[0] %}
{{state_attr(sensor.entity_id, "magnitude")}}
Unfortunately the same template does not seem to work when applied to geolocation entities generated by the USGS integration. When I just change the source to usgs_earthquakes_feed
I get an error message “ValueError: max() arg is an empty sequence”.
I can’t spot the difference yet…
1 Like
Yeah, that is the result i am getting as well.
The cause of the ValueError: max() arg is an empty sequence
error is actually a bit earlier in the code - even if you do just:
{% set sensors = states.geo_location %}
{{ sensors | map(attribute='name') | list }}
You’ll get an empty list.
Max fails because map produces an empty list, and map produces an empty list because states.geolocation is empty for some reason.
Something’s funky with the USGS integration, and i can’t figure out what.