I read a lot the last day, but i am stuck in my mind. So i am asking for some help or at least a push in the right direction.
Here is my problem. I use the tankekoenig integration to get fuel prices from gas stations around my home. Each station give me a price entity and a status entity if the station is open.
With the auto entities card i build a sorted list that show the prices and everything is fine.
Noe my wife has made a request: Please diskplay the lowest price and the according station name on a mushroom template card (OK - she did not say something about mushroom cards, but the start is true)
Based on this request i tought about how to get the values i need to display. SHould be easy was my first tought: Get sensors, sort them reverse, get values from first element.
But …
It seems that there is no way to select the price sensors by using wildcards. The integration_entities give me the status sensors to and i did not find a way to exclude them to start sorting.
Be aware that there are more than 1000 integrations. Plenty of people on here are willing to help, but might not have access to the integration you are using. It would be helpful if you would post the details for at least one entity from the States tool.
The above achieves the basic goal, it might be improved by starting with the binary sensors so you only include price data from stations that are open…
{% set open = integration_entities('tankerkoenig')
|select('is_state', 'on')
| map('replace', 'binary_', '')
| map('replace', 'status', 'super_e10') | list %}
{% set lowest = open | select('has_value'))
| sort(attribute='state') | first %}
{{ lowest.name }}: {{ lowest.state }} €
After digging into the template code some questions come to my mind.
The expand expands the entities - Check
The reject filters the binary sensors out - Check
The select selects only entities with a value - Check
The sort does the sort - Check
And the first selects the first element - Check
The result is as follows
<template TemplateState(<state sensor.net_gas_station_nordoel_reuterstr_90_super_e10=1.779; state_class=measurement, brand=NORDOEL, fuel_type=e10, station_name=Eyes Tankstellen GmbH, street=Reuterstr., house_number=90, postcode=25436, city=Uetersen, latitude=53.6932879, longitude=9.6546212, unit_of_measurement=€, attribution=Data provided by https://www.tankerkoenig.de, icon=mdi:gas-station, friendly_name=NET - Gas Station - NORDOEL Reuterstr. 90 Super E10 @ 2023-04-21T20:02:49.182037+02:00>)>
Now i coud acces the state and the name but no other attribute. What did i miss?
state and name are properties of the state object so they can be accessed directly from lowest. To access attributes you would use lowest.attributes, for example lowest.attributes.postcode