EMHASS: An Energy Management for Home Assistant

thanks. I will look into that. Not sure when this happend but now I get som weird results?

Even I have chosen no battery to grid export?

Hi All,

Need some help with generating a list with 30 min/15 min intervals. Itā€™s about dynamic tariffs which are updated by the hourā€¦ I use this code to get the hourly list, however I want a 30 min and even a 15 min list. That would mean that within an hour I need 2 times the same value (30 min) and 4 times the same value (15 min). So like this:
[1, 1, 2, 2, 3, 3 ā€¦ etc] 30 min interval
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3ā€¦etc] 15 min interval

I tried a lot of things but I canā€™t get it to work, maybe one of you guys know how to achieve this?

I use this code to extract the dynamic pricing and feed to EMHASS

- platform: template
  sensors:
      electricity_consumption_price_24h_forecast:
        friendly_name: "Electricity consumption price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
          {% endfor %} {{ (values.all)[:24] }}
      electricity_production_price_24h_forecast:
        friendly_name: "Electricity production price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %} 
          {% endfor %} {{ (values.all)[:24] }}

To generate the 30-minute and 15-minute interval lists from the hourly list you have, you can use a combination of for loops and the repeat filter in Jinja2. Hereā€™s how you can modify your code:

30-Minute Interval

- platform: template
  sensors:
    electricity_consumption_price_30min_forecast:
      friendly_name: "Electricity consumption price 30min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_30min = values.all | map('repeat', 2) | list %}
        {{ values_30min[:48] }}
    electricity_production_price_30min_forecast:
      friendly_name: "Electricity production price 30min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_30min = values.all | map('repeat', 2) | list %}
        {{ values_30min[:48] }}

15-Minute Interval

- platform: template
  sensors:
    electricity_consumption_price_15min_forecast:
      friendly_name: "Electricity consumption price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = values.all | map('repeat', 4) | list %}
        {{ values_15min[:96] }}
    electricity_production_price_15min_forecast:
      friendly_name: "Electricity production price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = values.all | map('repeat', 4) | list %}
        {{ values_15min[:96] }}

These templates should generate the 30-minute and 15-minute interval lists from your hourly data by repeating each hourly value 2 times and 4 times, respectively. Adjust the slices to ensure they match your required forecast length.

Thank you!

I tried the code in the Developers tool and get this error:

TemplateRuntimeError: No filter named 'repeat'.

Donā€™t know why, I run latest HA version

Try this:


- platform: template
  sensors:
    electricity_consumption_price_30min_forecast:
      friendly_name: "Electricity consumption price 30min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_30min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_30min.all = values_30min.all + [v, v] %}
        {% endfor %}
        {{ values_30min.all[:48] }}
    electricity_production_price_30min_forecast:
      friendly_name: "Electricity production price 30min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_30min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_30min.all = values_30min.all + [v, v] %}
        {% endfor %}
        {{ values_30min.all[:48] }}

2 Likes

That gives:

[-0.0091, -0.0091, -0.0093, -0.0093, -0.0091, -0.0091, -0.0077, -0.0077, 0.0032, 0.0032, 0.0413, 0.0413, 0.0597, 0.0597, 0.0769, 0.0769, 0.084, 0.084, 0.0661, 0.0661, 0.0708, 0.0708, 0.0645, 0.0645]

For 15 min interval:

{% set values_15min.all = values_15min.all + [v, v, v, v] %}

[-0.0091, -0.0091, -0.0091, -0.0091, -0.0093, -0.0093, -0.0093, -0.0093, -0.0091, -0.0091, -0.0091, -0.0091, -0.0077, -0.0077, -0.0077, -0.0077, 0.0032, 0.0032, 0.0032, 0.0032, 0.0413, 0.0413, 0.0413, 0.0413, 0.0597, 0.0597, 0.0597, 0.0597, 0.0769, 0.0769, 0.0769, 0.0769, 0.084, 0.084, 0.084, 0.084, 0.0661, 0.0661, 0.0661, 0.0661, 0.0708, 0.0708, 0.0708, 0.0708, 0.0645, 0.0645, 0.0645, 0.0645]

Thank you for this! :slight_smile:

This is my code that works in the Developer Tools:

- platform: template
  sensors:
    electricity_consumption_price_15min_forecast:
      friendly_name: "Electricity consumption price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_15min.all = values_15min.all + [v, v, v, v] %}
        {% endfor %}
        {{ values_15min.all[:96] }}
    electricity_production_price_15min_forecast:
      friendly_name: "Electricity production price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_15min.all = values_15min.all + [v, v, v, v] %}
        {% endfor %}
        {{ values_15min.all[:96] }}

I added it to the configuration.yaml, sensor is created but remains ā€™ unknown ā€™ for the values. I waited for an hour for the hourly sensor to refresh, but still ā€™ unknown ', is there a maximum size maybe?

Hi!

I see an (to me) unexpected prognosis in EMHASS 0.10.1 for negative production prices when the cost function is set ā€œprofitā€ and the parameter ā€œset_total_pv_sellā€ is set to ā€œfalseā€. This is set so because we have a direct self consumption and we have to export surplus energy, regardless of price.

The reason Iā€™m asking is since the prognosis looks like below, with P_grid_neg = 0 for P_PV > 0 and unit_prod_price < 0. (In my instance, unit_prod_price = unit_load_cost, it is not true but I think this is not the problem)

  • For P_PV > 0 regardless of unit_prod_price or unit_load_cost, I had expected P_grid_neg < 0.
  • Also, I would expect deferrable loads to be active when the cost would be as low as possible given that solar energy is exported. This would mean taking into account the export with unit_prod_price < 0

Perhaps Iā€™m misunderstanding something or have set up something wrong, but the behavior P_grid_neg < 0 seems OK as long as unit_prod_price > 0.

Best regards and thanks,
Bjƶrn

I didnā€™t do anything. Just copied your entire posts to chatgpt and then returned the response from chatgpt to you verbatim.

See the conversation here:

1 Like

I see, donā€™t understand why the sensor shows ā€˜unknownā€™. In the Developers Tool all is ok.

You need to put

- platform: template
  sensors:

above each sensor.

Also, rather than creating a sensor for this perhaps you should be using this code to create a REST command. Then it will only be executed when you call the REST command. Sensors would be constantly being executed.

Hereā€™s an example:

rest_command:
  naive_mpc_optim:
    url: http://localhost:5000/action/naive-mpc-optim
    method: POST
    content_type: 'application/json'
    payload: >-
      {
        "prod_price_forecast": {{
          ([states('sensor.cecil_st_feed_in_price')|float(0)] +
          (state_attr('sensor.cecil_st_feed_in_forecast', 'forecasts')|map(attribute='per_kwh')|list))
          | tojson 
        }},
        "load_cost_forecast": {{
          ([states('sensor.cecil_st_general_price')|float(0)] + 
          state_attr('sensor.cecil_st_general_forecast', 'forecasts') |map(attribute='per_kwh')|list) 
          | tojson 
        }},
        "pv_power_forecast": {{
          ([states('sensor.sonnenbatterie_84324_production_w')|int(0)] +
          state_attr('sensor.solcast_pv_forecast_forecast_today', 'detailedForecast')|selectattr('period_start','gt',utcnow()) | map(attribute='pv_estimate')|map('multiply',1000)|map('int')|list +
          state_attr('sensor.solcast_pv_forecast_forecast_tomorrow', 'detailedForecast')|selectattr('period_start','gt',utcnow()) | map(attribute='pv_estimate')|map('multiply',1000)|map('int')|list
          )| tojson
        }},
        "prediction_horizon": {{
          min(48, (state_attr('sensor.cecil_st_feed_in_forecast', 'forecasts')|map(attribute='per_kwh')|list|length)+1)
        }},
        "num_def_loads": 2,
        "def_total_hours": [2,2],
        "P_deferrable_nom": [1300, 7360],
        "treat_def_as_semi_cont": [1, 0],
        "set_def_constant": [0, 0],
        "soc_init": {{(states('sensor.sonnenbatterie_84324_state_charge_user')|int(0))/100 }},
        "soc_final": 0.1
      }
  publish_data:
    url: http://localhost:5000/action/publish-data
    method: POST
    content_type: 'application/json'
    payload: '{}'

Also your sensors in configuration.yaml should appear under ā€œsensor:ā€ like this:

sensor:

  - platform: template
    sensors:
      electricity_consumption_price_30min_forecast:
        friendly_name: "Electricity consumption price 30min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_30min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_30min.all = values_30min.all + [v, v] %}
          {% endfor %}
          {{ values_30min.all[:48] }}

  - platform: template
    sensors:
      electricity_production_price_30min_forecast:
        friendly_name: "Electricity production price 30min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_30min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_30min.all = values_30min.all + [v, v] %}
          {% endfor %}
          {{ values_30min.all[:48] }}

However I wouldnā€™t create sensors for these arrays when they are to be passed in a REST command. Better to use the jinja2 code in the REST command itself as in the example in the last post.

Also I do this in Node-Red using a Render Template Node. Let me know if youā€™re a Node-Red user.

No, I donā€™t use Node-Red.

This is my code at the moment, I have all the 3 options: 1h, 30min and 15min resolution. In the end I will use only one of the 3.

sensor:

- platform: template
  sensors:
      electricity_consumption_price_24h_forecast:
        friendly_name: "Electricity consumption price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
          {% endfor %} {{ (values.all)[:24] }}
      electricity_production_price_24h_forecast:
        friendly_name: "Electricity production price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %} 
          {% endfor %} {{ (values.all)[:24] }}

- platform: template
  sensors:
    electricity_consumption_price_15min_forecast:
      friendly_name: "Electricity consumption price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_15min.all = values_15min.all + [v, v, v, v] %}
        {% endfor %}
        {{ values_15min.all[:96] }}
    electricity_production_price_15min_forecast:
      friendly_name: "Electricity production price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_15min.all = values_15min.all + [v, v, v, v] %}
        {% endfor %}
        {{ values_15min.all[:96] }}

- platform: template
  sensors:
    electricity_consumption_price_30min_forecast:
      friendly_name: "Electricity consumption price 30min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_30min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_30min.all = values_30min.all + [v, v] %}
        {% endfor %}
        {{ values_30min.all[:48] }}
    electricity_production_price_30min_forecast:
      friendly_name: "Electricity production price 30min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_30min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_30min.all = values_30min.all + [v, v] %}
        {% endfor %}
        {{ values_30min.all[:48] }}

Results in this:

Doesnā€™t look logical at all. I donā€™t know if there are limitations coding like this.

Oh wait, I see capital letters in the friendly_name of the code, which is not reflected in the result. The sensors which start with a capital ā€˜Eā€™ show results.
The presented result comes from above code.

YAML very sensitive about indentation. You need two spaces as below. I canā€™t test this as I donā€™t have these variables in my system so there may still be typos. But make sure you have two spaces and indentation correct.

So something like this:

sensor:

  - platform: template
    sensors:
      electricity_consumption_price_24h_forecast:
        friendly_name: "Electricity consumption price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %} {{ (values.all)[:24] }}

  - platform: template
    sensors:
      electricity_production_price_24h_forecast:
        friendly_name: "Electricity production price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %} 
          {% endfor %} {{ (values.all)[:24] }}

  - platform: template
    sensors:
      electricity_consumption_price_15min_forecast:
        friendly_name: "Electricity consumption price 15min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_15min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_15min.all = values_15min.all + [v, v, v, v] %}
          {% endfor %}
          {{ values_15min.all[:96] }}

    electricity_production_price_15min_forecast:
      friendly_name: "Electricity production price 15min forecast"
      value_template: >-
        {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
        {% set values = namespace(all=[]) %}
        {% for i in range(now().hour+1,data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
        {% for i in range(data | length) %}
          {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
          {% set values.all = values.all + [ v ] %}
        {% endfor %}
        {% set values_15min = namespace(all=[]) %}
        {% for v in values.all %}
          {% set values_15min.all = values_15min.all + [v, v, v, v] %}
        {% endfor %}
        {{ values_15min.all[:96] }}

  - platform: template
    sensors:
      electricity_consumption_price_30min_forecast:
        friendly_name: "Electricity consumption price 30min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_30min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_30min.all = values_30min.all + [v, v] %}
          {% endfor %}
          {{ values_30min.all[:48] }}

  - platform: template
    sensors:
      electricity_production_price_30min_forecast:
        friendly_name: "Electricity production price 30min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_30min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_30min.all = values_30min.all + [v, v] %}
          {% endfor %}
          {{ values_30min.all[:48] }}

If you use Visual Studio Code editor from Microsoft youā€™ll get guide lines to help get indentation correct:

Bu as I say if you are using electricity_consumption_price_24h_forecast as a data dictionary to feed into an MPC calculation in EMHASS you are better off putting the JINJA code in the rest_command not in a sensor.

Yes, I am transferring the code to rest_command right now

Can you share the finished result later?

1 Like

Sure I will. Just thinking about prediction_horizon. Thatā€™s on optimisation step basis if I read it correctly, if my optimisation step is 15 min, so the prediction_horizon step is also 15 min?

sensor:

  - platform: template
    sensors:
      electricity_consumption_price_24h_forecast:
        friendly_name: "Electricity consumption price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %} {{ (values.all)[:24] }}

  - platform: template
    sensors:
      electricity_production_price_24h_forecast:
        friendly_name: "Electricity production price 24h forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %} 
          {% endfor %} {{ (values.all)[:24] }}

  - platform: template
    sensors:
      electricity_consumption_price_15min_forecast:
        friendly_name: "Electricity consumption price 15min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_15min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_15min.all = values_15min.all + [v, v, v, v] %}
          {% endfor %}
          {{ values_15min.all[:96] }}

  - platform: template
    sensors:
      electricity_production_price_15min_forecast:
        friendly_name: "Electricity production price 15min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_15min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_15min.all = values_15min.all + [v, v, v, v] %}
          {% endfor %}
          {{ values_15min.all[:96] }}

  - platform: template
    sensors:
      electricity_consumption_price_30min_forecast:
        friendly_name: "Electricity consumption price 30min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((0.011 + (0.001*(data[i] | float(0))*10)*1.06 + 0.01582*1.06 + 0.0538613 + 0.0020417 + 0.0503288) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_30min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_30min.all = values_30min.all + [v, v] %}
          {% endfor %}
          {{ values_30min.all[:48] }}

  - platform: template
    sensors:
      electricity_production_price_30min_forecast:
        friendly_name: "Electricity production price 30min forecast"
        value_template: >-
          {% set data = state_attr('sensor.nordpool', 'raw_today') | map(attribute='value') | list %}
          {% set values = namespace(all=[]) %}
          {% for i in range(now().hour+1,data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set data = state_attr('sensor.nordpool', 'raw_tomorrow') | map(attribute='value') | list %}
          {% for i in range(data | length) %}
            {% set v = ((-0.00905 + (0.001*(data[i] | float(0))*10)) | round(4)) %}
            {% set values.all = values.all + [ v ] %}
          {% endfor %}
          {% set values_30min = namespace(all=[]) %}
          {% for v in values.all %}
            {% set values_30min.all = values_30min.all + [v, v] %}
          {% endfor %}
          {{ values_30min.all[:48] }}