Hi all,
I’ve been trying to solve this for several days and search the faq but I can’t locate the issue.
This code is using the nordpool integration to search the cheapest four hours between today 14 and tomorrow 24. It should save the starting time to sensor.nordpool_cheapest_hours and the automation should also update it to the input_datetime helper. I used the developer tools and template editor to verify that the sensor code works (so it seems, but…) I also checked from developer tools / states that the sensor state is correct (see the screenshot below).
However, when I use template editor and call "{{ state_attr('sensor.nordpool_cheapest_hours', 'state') }}"
the result is null.
This is probably why saving the value to the helper also fails and the automation is terminated with an error: Error rendering data template: TypeError: ‘NoneType’ object is not subscriptable
I’ve tried to parse the output using different ways but it didn’t seem to have an impact. My HA is installed from an image directly on a rasp4. Version information below:
Home Assistant 2023.5.4
Supervisor 2023.04.1
Operating System 10.2
Frontend 20230503.3 - latest
What am I missing? Can someone help me.
PS. ChatGPT was quite useless in the troubleshooting. It just kept apologizing for errors it made
Here’s the code:
input_datetime:
nordpool_cheapest_hours_start:
name: nordpool cheapest hours start
has_date: true
has_time: true
sensor:
- platform: template
sensors:
nordpool_cheapest_hours:
friendly_name: "Nordpool cheapest hours"
#device_class: timestamp
value_template: >
{% set output = namespace(value=[]) %}
{% set data = state_attr('sensor.nordpool_kwh_fi_eur_3_10_024', 'today')[14:24] + state_attr('sensor.nordpool_kwh_fi_eur_3_10_024', 'tomorrow') %}
{% set hours = 4 %}
{%- for inval in data[:min(-hours+1, -1)] -%}
{% set temp = namespace(value=[]) %}
{%- set j = loop.index -1 -%}
{%- for i in range(hours) %}
{%- set temp.value = temp.value + [data[j+i]] -%}
{%- endfor -%}
{%- set output.value = output.value + [temp.value | average] -%}
{%- endfor -%}
{% set data2 = state_attr("sensor.nordpool_kwh_fi_eur_3_10_024", "raw_today")[14:24] + state_attr("sensor.nordpool_kwh_fi_eur_3_10_024", "raw_tomorrow") %}
{{ data2[output.value.index(output.value|min)]["start"].isoformat()}}
automation:
- id: 'nordpool_update_cheapest_hours'
alias: Nordpool update cheapest hours
trigger:
- platform: time_pattern
minutes: '/15'
condition:
- condition: template
value_template: "{{ state_attr('sensor.nordpool_kwh_fi_eur_3_10_024', 'tomorrow_valid') and now().hour >= 14 }}"
action:
- service: homeassistant.update_entity
entity_id: sensor.nordpool_cheapest_hours
- service: notify.persistent_notification
data_template:
title: Nordpool Cheapest Hours
message: "Nordpool cheapest hours state: {{ state_attr('sensor.nordpool_cheapest_hours', 'state') }}"
- service: input_datetime.set_datetime
data:
datetime: >-
{% set timestamp = state_attr('sensor.nordpool_cheapest_hours', 'state')[:19] | replace('T', ' ') %}
{% set timestamp_string = timestamp | timestamp_custom('%Y-%m-%d %H:%M:%S') %}
{{ timestamp_string }}
target:
entity_id: input_datetime.nordpool_cheapest_hours_start
mode: single