Energy meter configuration to avoid bad KWH consumption on HA Core restart

Hi all,

I tried to setup energy meter for France using pitinfo module and wemos D1.
Pitinfo data integration is working well, no problem with that.

In ESPHome config i have

sensors:
  - platform: teleinfo
    tag_name: "HCHC"
    name: "Index heures creuses"
    unit_of_measurement: "Wh"
    icon: mdi:gauge
    device_class: "ENERGY"
    state_class: "total_increasing"
    id: heures_creuses
    
  - platform: teleinfo
    tag_name: "HCHP"
    name: "Index heures pleines"
    unit_of_measurement: "Wh"
    icon: mdi:gauge
    device_class: "ENERGY"
    state_class: "total_increasing"
    id: heures_pleines

  - platform: teleinfo
    tag_name: "PTEC"
    name: "Tarif en cours"
    icon: mdi:clock-time-nine-outline

In ESPHome integration sensor.tarif_en_cours refers to PTEC, sensor.index_heures_creuses to HCHC and sensor.index_heures_pleines to HCHP.

In France we have 2 tariffs, on tariff change a signal (impulse) is sent to electricity network. (For HP and HC)
My wemos sends this value as sensor.tarif_en_cours. On HC the price must be 0.13/KWh, on HP it’s 0.1738 .

So i’ve done this in configuration.yaml

sensor:
  - platform: template
    sensors:
       tarif_elec:
            friendly_name: "Tarif Electricite"
            unit_of_measurement: "/kWh" 
            value_template: >-
                {%- if is_state('sensor.tarif_en_cours', 'HC..')
                -%}
                0.13
                {%- else -%}
                0.1738
                {%- endif -%}

The value of tariff is good at the good moment. No problem with that.

I m getting one index in Wh for HP Tariff and another for HC Tariff (sensor.index_heures_creuses and sensor.index_heures_pleines)
So i did another sensor template to convert High price consumption and Low price consumption to kwh

       total_elec_hc:
            friendly_name: "Electricite totale consommee en HC"
            unit_of_measurement: kWh
            value_template: "{{ states('sensor.index_heures_creuses')|float / 1000}}"
       
       total_elec_hp:
            friendly_name: "Electricite totale consommee en HP"
            unit_of_measurement: kWh
            value_template: "{{ states('sensor.index_heures_pleines')|float / 1000}}"

And declared the utility meter

utility_meter:
       compteur_elec_hc_journalier:
            source: sensor.total_elec_hc
            cycle: daily
       compteur_elec_hp_journalier:
            source: sensor.total_elec_hp
            cycle: daily

I then added it to the energy configuration, linked to the tariff provided by tarif_elec

It works

The problem i have is that each time i reboot the Home Assistant core, compteur_elec_hp_journalier and compteur_elec_hc_journalier are taking as value my FULL consumption from the setup of my electricity network.

For example today

I think i’m missing something but i don’t know what. Price is good, but daily consumption is then totaly messed up each time i reboot.

After searching a lot it seems to be an old issue.

On HA restart template sensors are unavailable or unknown state, and take by default a value of 0 ?
Then data comes to virtual sensor, and utility Meyer takes full consumption value.

Does anyone have a good workaround ?

Thanks for your help

Implement availability in your template sensors, based on whatever fits you.
If the availability is false, the template value will be ignored.

Thanks, i’m going to try this

Thanks, it works using availability.

Have a good day

Is there a chance to show how you have implemented this availability?

could you show how “implemnet availability”?