Updating Templates with the new default values in 2021.10.x

This first line makes a list of entity_ids that range from 1 to 24 with the name sensor.mgp_pun_ore_{n} where {n} is the number. I did this so you don’t have to list them all out manually.

{% set entities = range(1, 25) | map('string') | map('reverse') | map('slice', 1, 'sensor.mgp_pun_ore_') | map('list') | map(attribute=0) | map('reverse') | map('join') | list %}

The full template then converts the entity_id’s to states, compares the list of entity_id’s to the list of numerical states. If the length of them does not match, it uses the previous state. The numerical state list and the entity_id list will only match when all sensors are available.

{% set entities = range(1, 25) | map('string') | map('reverse') | map('slice', 1, 'sensor.mgp_pun_ore_') | map('list') | map(attribute=0) | map('reverse') | map('join') | list %}
{% set values = entities | map('states') | select('is_number') | list %}
{% if entities | length == values | length %}
  {{ values | map('float') | average | round(3) }}
{% else %}
  {{ this.state if this is defined and this.state is defined else 0 }}
{% endif %}
1 Like

thank you. I’m sorry was missing the answer.
I’ll try asap and let you know

Thanks
Claudio

thank you very much @petro , it seems working !
At this point let me ask you another thing :slight_smile:

I have this template to calculate daily consumption.

  home_energy_today:
    friendly_name: 'Home Energy Today'
    unique_id: sensor.home_energy_today
    device_class: energy
    value_template: "{{ ((states('sensor.produzione_giornaliera')|float) 
                       + (states('sensor.solax_today_s_import_energy')|float)
                       + (states('sensor.solax_today_s_yield')|float)
                       - (states('sensor.solax_e_charge_today')|float)
                       - (states('sensor.solax_today_s_export_energy')|float))|round(3)}}"
    unit_of_measurement: "kWh"

all counters should reset to 0 at midnight (came from inverter), every day. Instead there are some that are resetting 40 seconds before midnight, some 20 sec before and one at 00:00 exactly.

the result is that I have a spike in the graph that is disturbing other “utility meter” helpers referring to this for montly and yearly calculation.

immagine

Any advice on how to manage this?

Thank again
Claudio

Who/what are you replying to?