Template sensor data and graph not persistent based on value template

Hello there,

I’m using a template sensor to compute the consumed electricity in my house. The data is provided by the P1 port of my smart reader via Toon (custom component by cyberjunkie). However, the P1 port only supplies the night-tariff and day-tariff separately. I would like to see the total consumption, that is why is resorted to a template sensor.

This is what the code for the template sensor looks like:

  - platform: template 
    sensors:
      toon_p1_power_use_total:
        unit_of_measurement: 'kWh'
        value_template: '{{ states.sensor.toon_p1_power_use_cnt_low.state|int + states.sensor.toon_p1_power_use_cnt_high.state|int }}'

This all seems to work fine. But there is a strange thing going on; when I restart Home-Assistant the value drops to zero/unknown. After the restart it works fine. However, this screws up the way the history graph is displayed:

The Toon does provide the total gas usage as displayed in the graph above the electricity usage. The data of provided by Toon seems to be persistent (e.g. is not reset to zero unknown each time Home Assistant restarts).

Any clue on how to fix the graph? Thanks in advance!

The state’s for your devices on startup are returning 0, so 0+0 = 0. Some sensors have the option to restore state on startup, you should look in that direction for whatever sensors you are using.