I am writing a sensor template calculating the cheapest 3 consecutive hours based on Nordpool data for today and tomorrow. I will use this to decide when to start machines in my home. Once this is done I plan to calculate also the 2 consecutive hours and cheapest hour to use for machines not using 3 hours in total. Examples: dishwasher, washer etc.
I run into problem with my sensor template and I can not figure out what is wrong.
I get this error code:
Logger: homeassistant.config
Source: config.py:929
First occurred: 17:11:08 (1 occurrences)
Last logged: 17:11:08
Invalid config for [template]: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘[’) for dictionary value @ data[‘sensor’][0][‘state’]. Got '{% set nordpool_price_json = { “kr”: state_attr(‘sensor.nordpool_kwh_se3_sek_3_10_025’, ‘today’) + state_attr(‘sensor.nordpool_kwh_se3_sek_3_10_025’, ‘tomorrow’), “hr”: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47] } %} {% set nordpool_cons_price_json = { “kr”: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47],… (See /config/configuration.yaml, line 112).
If I remove the for loop after json definitions it works fine, but not when I add the for loops again. I have tried a very simple for loop and then it is ok. Any help is apprecaited.
- sensor:
- name: "Opt_pwr_hours"
state: >
{% set nordpool_price_json = {
"kr": state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'today') + state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'tomorrow'),
"hr": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47]
} %}
{% set nordpool_cons_price_json = {
"kr": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47],
"hr": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47]
} %}
{% for n in range (0,45) %}
{% if nordpool_price_json.kr[n+2] != none %}
{% set sum_of_three = nordpool_price_json.kr[n] + nordpool_price_json.kr[n+1] + nordpool_price_json.kr[n+2] %}
{% set nordpool_cons_price_json.kr[n] = sum_of_three %}
{% else %}
{% set nordpool_cons_price_json.kr[n] = 1000 %}
{% endif %}
{% endfor %}
{% set k = max(nordpool_cons_price_json.kr) %}
{% set current_hour = now().hour %}
{% for n in range (current_hour,45) %}
{% if nordpool_cons_price_json.kr[n] < k %}
{% set best_3_cons_start_hour = n %}
{% endif %}
{% endfor %}
{{best_3_cons_start_hour}}