Closed redlefloh closed 8 months ago
@edwardtfn would you mind taking a look, thank you!
My apologies, I've missed your previous message. I will take a look and come back to you. It's nice to look at that code anyways.., if i was writting that today it probably would be a bit different, 😂
Anyways, I was looking my sensor now (which is working fine) and I saw it is slightly different than yours (maybe the version on GitHub was outdates, I've just pushed the latest).
Could you please try this and let me know if you still having the same issue?
template:
- sensor:
- name: Electricity price
unique_id: d4c04abf-c258-4f0f-a61d-cc0170225081
unit_of_measurement: "SEK/kWh"
icon: mdi:currency-usd
device_class: monetary
availability: "{{ state_attr('sensor.tibber_electricity_price', 'today') | default('unknown') not in ['unknown','unavailable', None] }}"
state: "{{ this.attributes.current.total | float(0) if this.attributes is defined and this.attributes.current is defined and this.attributes.current.total is defined and is_number(this.attributes.current.total) else None }}"
attributes:
all_prices: "{{ state_attr('sensor.tibber_electricity_price', 'today') | default([]) + state_attr('sensor.tibber_electricity_price', 'tomorrow') | default([]) }}"
min_price: "{{ this.attributes.all_prices | map(attribute='total') | list | min if this.attributes.all_prices is defined and this.attributes.all_prices | count > 0 else None }}"
max_price: "{{ this.attributes.all_prices | map(attribute='total') | list | max if this.attributes.all_prices is defined and this.attributes.all_prices | count > 0 else None }}"
avg_price: "{{ this.attributes.all_prices | map(attribute='total') | list | average(0) if this.attributes.all_prices is defined and this.attributes.all_prices | count > 1 else this.attributes.max_price | default(None) }}"
future: "{{ (this.attributes.all_prices | default([])) | selectattr('startsAt', 'gt', (now() + timedelta(hours=-1)).isoformat()) | list if this.attributes.all_prices is defined else [] }}"
future_min_price: "{{ this.attributes.future | map(attribute='total') | list | min if this.attributes.future is defined and this.attributes.future | count > 0 else None }}"
future_max_price: "{{ this.attributes.future | map(attribute='total') | list | max if this.attributes.future is defined and this.attributes.future | count > 0 else None }}"
future_avg_price: "{{ this.attributes.future | map(attribute='total') | list | average(0) if this.attributes.future is defined and this.attributes.future | count > 1 else this.attributes.future_max_price | default(None) }}"
current: "{{ this.attributes.future | first if this.attributes.future | count > 0 else None }}"
price_level: "{{ this.attributes.current.level | default('unknown') | replace('_', ' ') | capitalize if this.attributes.current is defined and this.attributes.current.level is defined else None }}"
price_level_1d: >-
{% set price_cur = this.state | default(0) | float(0) %}
{% set price_avg = this.attributes.avg_price | default(0) | float(0) %}
{% if price_cur == 0 or price_avg == 0 %}
unknown
{% else %}
{% set price_ratio = (price_cur / price_avg) %}
{% if price_ratio >= 1.4 %}
Very expensive
{% elif price_ratio >= 1.15 %}
Expensive
{% elif price_ratio <= 0.6 %}
Very cheap
{% elif price_ratio <= 0.9 %}
Cheap
{% else %}
Normal
{% endif %}
{% endif %}
price_level_combined: >-
{% set level1 = this.attributes.price_level_1d | default('unknown') %}
{% set level3 = this.attributes.price_level | default('unknown') %}
{% if level1 == level3 %}
{{ level1 }}
{% elif level1 in ['unknown','unavailable','none'] or level3 in ['unknown','unavailable','none'] %}
unknown
{% elif level1 == "Very cheap" %}
{{ level3 }}
{% elif level3 == "Very cheap" %}
{{ level1 }}
{% elif level1 == "Cheap" %}
{{ level3 }}
{% elif level3 == "Cheap" %}
{{ level1 }}
{% elif level1 == "Normal" %}
{{ level3 }}
{% elif level3 == "Normal" %}
{{ level1 }}
{% elif level1 == "Expensive" %}
{{ level3 }}
{% else %}
{{ level1 }}
{% endif %}
is_below_average: "{{ (this.state | float(0) < this.attributes.avg_price | float(0)) if is_number(this.state) and is_number(this.attributes.avg_price) else None }}"
is_above_average: "{{ (this.state | float(0) > this.attributes.avg_price | float(0)) if is_number(this.state) and is_number(this.attributes.avg_price) else None }}"
is_at_min: "{{ (this.state | float(0) <= this.attributes.min_price | float(0)) if is_number(this.state) and is_number(this.attributes.min_price) else None }}"
is_at_max: "{{ (this.state | float(0) >= this.attributes.max_price | float(0)) if is_number(this.state) and is_number(this.attributes.max_price) else None }}"
is_close_to_min: "{{ (this.state | float(0) <= (1.15 * this.attributes.min_price | float(0))) if is_number(this.state) and is_number(this.attributes.min_price) else None }}"
future_price_level: >-
{% set price_cur = this.state | default(0) | float(0) %}
{% set price_avg = this.attributes.future_avg_price | default(0) | float(0) %}
{% if price_cur == 0 or price_avg == 0 %}
unknown
{% else %}
{% set price_ratio = (price_cur / price_avg) %}
{% if price_ratio >= 1.4 %}
Very expensive
{% elif price_ratio >= 1.15 %}
Expensive
{% elif price_ratio <= 0.6 %}
Very cheap
{% elif price_ratio <= 0.9 %}
Cheap
{% else %}
Normal
{% endif %}
{% endif %}
next_16h: "{{ (this.attributes.future | default([])) | selectattr('startsAt', 'lt', (now() + timedelta(hours=16)).isoformat()) | list if this.attributes.future is defined else [] }}"
next_16h_min_price: "{{ this.attributes.next_16h | map(attribute='total') | list | min if this.attributes.next_16h is defined and this.attributes.next_16h | count > 0 else None }}"
next_16h_is_at_min: "{{ (this.state | float(0) <= this.attributes.next_16h_min_price | float(0)) if is_number(this.state) and is_number(this.attributes.next_16h_min_price) else None }}"
And is the sensor sensor.tibber_electricity_price
working properly for you? It is required for this one.
Hi @edwardtfn, sorry for the holiday delay... hope you enjoyed the holidays! The new version works just fine, thanks so much!
Hi @edwardtfn, is the "Electricy price" template sensor as you have it specified here still supposed to work? I copied it as is but unfortunately the sensor is "Unavailable" for me:
even though the "Tibber Electricity Price" REST sensor works just fine?
Is there anything I am missing or doing wrong? The YAML files are below, if you could maybe take a quick look? Thanks so much!
The "Tibber Electricity Price" sensor looks like this:
The "Electricity Price" sensor looks like this:
The error I am getting in the log is: