Need some help with a template

I’m already having a template sensor that is giving me half hourly values of PV power.

    - name: Pv power forecast list
      unique_id: 6bc0c511-9c6f-4778-b13e-a20e3646a7e5
      state: >
        {{ states("sensor.solcast_forecast_today")}}
      availability: >-
        {{ has_value("sensor.solcast_forecast_today") }}
      attributes:
        list: >
          {{  (state_attr('sensor.solcast_forecast_today', 'detailedForecast')
                |selectattr('period_start','gt',utcnow())
                |map(attribute='pv_estimate')
                |map('multiply',2000)
                |map('int')
                |list
            + state_attr('sensor.solcast_forecast_tomorrow', 'detailedForecast')
                |selectattr('period_start','gt',utcnow())
                |map(attribute='pv_estimate')
                |map('multiply',2000)
                |map('int')
                |list)[:36]
                }}

Which outputs a list [3463, 3307, 3726, 3896, 3744, 3640, 3536, 3372, 3036, 2840, 2671, 2374, 1547, 901, 415, 148, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
This list always changes, when sun is up, the list starts with values above 0, when sun is down,list starts with 0.

The output of my new template should have the postition in the list with the first value higher then AFTER all the zeros.
Example:
[4, 5, 6, 0, 0, 0, 0, 1, 2, 3] → wanted result 8
[0, 0, 0, 0, 1, 2, 3, 4, 5, 6] → wanted result 5
[1, 2, 3, 4, 5, 6, 0, 0, 0, 0] → wanted result full list length

I’ve tried different things but I cant get it right.

There’s probably a simpler way, but here’s a first shot…

{% if x[-1] == 0 %}
{{x | count}}
{% else %}
{%- for i in x %}
  {{ (loop.index + 1 if i == 0 and loop.nextitem != 0) }}
{% endfor %}{% endif %}

Maybe, but this does exactly what I want.

Thanks a lot.

Is there any case where the list both begins and ends with 0? If so you’ll need to account for that as well.

{% if x[-1] == 0 and x[0] != 0 %}
{{x | count}}
{% else %}
{%- for i in x %}
  {{ (loop.index + 1 if i == 0 and loop.nextitem != 0) }}
{% endfor %}{% endif %}

I didn’t thought about that, but indeed it can happen.
If we have days in the winter when there isn’t any sun.

What are you actually trying to get with this? The time that you actually have light? What’s the big picture here?

I’m using EMHASS to predict the optimal times to start my heat pump.
This is a shell command that is send to EMHASS

post_mpc_optim: >-
  curl -i -H 'Content-Type: application/json' -X POST -d '{
    "def_total_hours":[
      {{  states('sensor.wasmachien_uren')  }},
      {{  states('sensor.droogkast_uren')  }},
      {{  states('sensor.afwasmachien_uren')  }},
      {{  states('sensor.warmtepompboiler_uren')  }},
      {{  states('sensor.warmtepomp_uren')}}],
    "set_def_constant":[true, true, true, true, true],
    "pv_power_forecast": {{
        [states('sensor.huidige_opbrengst')|int(0)] +
        state_attr('sensor.pv_power_forecast_list', 'list')[:states('sensor.prediction_horizon') | int - 1]}},
    "prod_price_forecast": {{ 
        [state_attr('sensor.prod_price_forecast_list', 'extra') | float] +
        state_attr('sensor.prod_price_forecast_list', 'list')[:states('sensor.prediction_horizon') | int - 1]}},
    "load_cost_forecast": {{ 
        state_attr('sensor.cost_forecast_list', 'list')}},
    "prediction_horizon": {{ states('sensor.prediction_horizon') | int }},
    "alpha": 1,
    "beta": 0
    }' http://localhost:5001/action/naive-mpc-optim

Before was my sensor states('sensor.prediction_horizon') fixed to 36 half hours.
My heat pump (warm water) gets planned in.
But going further to the evening my state_attr('sensor.pv_power_forecast_list', 'list') shows the PV generation for the next day and my heat pump is planned the next morning.




So I wanted to shorten the prediction horizon, but keep it as long as possible

1 Like