Hi all,
I am struggeling a bit with a loop in a template, where I want to select only entries in the future. I cannot get selectattr / rejectattr work on time and wanted to ask for help here. Background: Tibber has been my energy provider since 1st October. The energy price in my contract is hourly, therefore I want to have in Home Assistant a list of the future hours where energy is cheapest, just to konw the best time for the dish washer, washing maschine, loading EV, etc., at a glance - and the “future” is my problem … Also beforehand: I am no developer, just doing trial & error and total supprised, when it works at the end.
To start with: When I get the prices 1300ish from Tibber I store it in a sensor like this:
In the price_data (Object?) I always have 48 entries (before 1300 for yesterday & today, after 1300 for today and tomorrow). I want to setup a markdown card, where i.e. the cheapest 5 hours are shown, but only for the future. I use following tamplate:
{% for item in state_attr('sensor.tibber_strompreis', 'price_data') | sort(attribute='total') %}
{% if loop.index <=5 %}
{{ (item.total * 100) | round(2) }} Cent am {{ as_timestamp(item.startsAt) | timestamp_custom('%d.%m. (%a), %H:%M') }} Uhr
{% endif %}
{%- endfor %}
This leads to following result:
Acutally, I managed limiting the loop to the five cheapest entries - but they should all be in the future, not in the past like in this screenshot.
Unfortunately, I fail on the filter (selectattr) for startsAt > now()
, as I do not get the timestamp formatted correctly. When using | selectattr('startsAt', '>', now())
as additional filter, I get a TypeError: '>' not supported between instances of 'str' and 'datetime.datetime'
. Somehow I need to convert “startsAt” into a datetime object - and that’s where I am stuck …
Would really appreciate if someone can point me into the right direction.
Thanks!
Stephan