Closed piwi91 closed 1 month ago
I don't have Tibber myself so I made some assumptions based on the other service calls for energy prices (like for Energy Zero). In those service calls datetime strings are used, so I assumed the same here.
I would expect that datetime objects are allowed as input for the service call, if not, it might be worthwhile to write up an issue about that.
To fix the last issue, a for loop is needed to convert all datetime objects to strings. I will adjust the docs on this
Can you try this
- trigger:
- platform: time_pattern
hours: "/1"
- platform: homeassistant
event: start
action:
- service: tibber.get_prices
data:
start: "{{ (today_at() - timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S') }}"
end: "{{ (today_at() + timedelta(days=2)).strftime('%Y-%m-%d %H:%M:%S') }}"
response_variable: prices
sensor:
- unique_id: 79c470d8-4ccd-4f44-b3a2-e3d59d5dda8a
name: Tibber prices
state: "{{ prices.prices.values() | first | selectattr('start_time', '<=', now()) | map(attribute='price') | list | last }}"
attributes:
prices: >
{% set ns = namespace(prices=[]) %}
{% for i in prices.prices.values() | first %}
{% set n = dict(start_time = i.start_time.isoformat(), price = i.price) %}
{% set ns.prices = ns.prices + [n] %}
{% endfor %}
{{ ns.prices }}
Thanks @TheFes , that fixed it! :)
There is an issue with timezone/non-timezone comparisons and string/datetime comparison. I can't exactly find what's causing the issue but I already changed some things.
Trace 1:
This was due to non-timezone/timezone comparison. I fixed this by using the template configuration below (note the strftime in data start and end):
The I got the next issue,
Error rendering state template for sensor.tibber_prices: TypeError: '<=' not supported between instances of 'datetime.datetime' and 'str'
, to fix this I removed thestrftime
from theselectattr
(already fixed in above configuration). Issue resides bceause the prices attribute has datetime objects instead of strings which cause more issues in the second step. Any idea how to fix?Output of the sensor: