I’ve spend far too much time with this quite simply template sensor. The base code works in template editor so I guess the problem is with the self reference. I just can’t figure it out! Probably just minor syntax-like error. Thanks for help!
- sensor:
- name: Energy price night # sensor.energy_price_night
unique_id: 3901d2cd-950a-4510-836c-b65ae115915c
icon: mdi:lightning-bolt
device_class: monetary
unit_of_measurement: "c/kWh"
state: "{{ this.attributes.get('list')|average|round(2) }}"
attributes:
min: "{{ this.attributes.get('list')|min|round(2) }}"
max: "{{ this.attributes.get('list')|max|round(2) }}"
list: >-
{% set v_end_hour = 8 %}
{% if now().hour|int <= v_end_hour|int %}
{% set v_date = 'today' %}
{% set v_start_hour = now().hour %}
{% else %}
{% set v_date = 'tomorrow' %}
{% set v_start_hour = 0 %}
{% endif %}
{% if v_date == 'tomorrow' and is_state_attr('sensor.nordpool', 'tomorrow_valid', false) %}
{{ 0|float }}
{% else %}
{{ state_attr('sensor.nordpool', v_date)[v_start_hour:v_end_hour] }}
{% endif %}
Also please free to rewrite the whole code if there is a better way to get the avg, min and max prices for Nordpool sensor.
I couldn’t figure out why the self reference didn’t work so I ended up with the following massive code:
- sensor:
- name: Energy price night # sensor.energy_price_night
unique_id: 3901d2cd-950a-4510-836c-b65ae115915c
icon: mdi:lightning-bolt
device_class: monetary
unit_of_measurement: "c/kWh"
state: >-
{% set v_end_hour = 8 %}
{% if now().hour|int <= v_end_hour|int %}
{% set v_date = 'today' %}
{% set v_start_hour = now().hour %}
{% else %}
{% set v_date = 'tomorrow' %}
{% set v_start_hour = 0 %}
{% endif %}
{% if v_date == 'tomorrow' and is_state_attr('sensor.nordpool', 'tomorrow_valid', false) %}
{{ 0|float }}
{% else %}
{{ state_attr('sensor.nordpool', v_date)[v_start_hour:v_end_hour]|average|round(2) }}
{% endif %}
attributes:
min: >-
{% set v_end_hour = 8 %}
{% if now().hour|int <= v_end_hour|int %}
{% set v_date = 'today' %}
{% set v_start_hour = now().hour %}
{% else %}
{% set v_date = 'tomorrow' %}
{% set v_start_hour = 0 %}
{% endif %}
{% if v_date == 'tomorrow' and is_state_attr('sensor.nordpool', 'tomorrow_valid', false) %}
{{ 0|float }}
{% else %}
{{ state_attr('sensor.nordpool', v_date)[v_start_hour:v_end_hour]|min|round(2) }}
{% endif %}
max: >-
{% set v_end_hour = 8 %}
{% if now().hour|int <= v_end_hour|int %}
{% set v_date = 'today' %}
{% set v_start_hour = now().hour %}
{% else %}
{% set v_date = 'tomorrow' %}
{% set v_start_hour = 0 %}
{% endif %}
{% if v_date == 'tomorrow' and is_state_attr('sensor.nordpool', 'tomorrow_valid', false) %}
{{ 0|float }}
{% else %}
{{ state_attr('sensor.nordpool', v_date)[v_start_hour:v_end_hour]|max|round(2) }}
{% endif %}
list: >-
{% set v_end_hour = 8 %}
{% if now().hour|int <= v_end_hour|int %}
{% set v_date = 'today' %}
{% set v_start_hour = now().hour %}
{% else %}
{% set v_date = 'tomorrow' %}
{% set v_start_hour = 0 %}
{% endif %}
{% if v_date == 'tomorrow' and is_state_attr('sensor.nordpool', 'tomorrow_valid', false) %}
{{ 0|float }}
{% else %}
{{ state_attr('sensor.nordpool', v_date)[v_start_hour:v_end_hour] }}
{% endif %}
Help would be appreciated how to get the self reference to work.