Template sensor state remains "unknown"

I’m using a template sensor to calculate total energy consumption and production in my house, as well as net energy usage.

Here’s my setup:

sensor:
  - platform: dsmr
    dsmr_version: 5
    port: /dev/ttyUSB0

  - platform: template
    sensors:
      energy_production_total:
        value_template: >
          {{ (states('sensor.energy_production_tarif_1') | float + states('sensor.energy_production_tarif_2') | float) | round(1) }}
        unit_of_measurement: 'kWh'

      energy_consumption_total:
        value_template: >
          {{ (states('sensor.energy_consumption_tarif_1') | float + states('sensor.energy_consumption_tarif_2') | float) | round(1) }}
        unit_of_measurement: 'kWh'

      net_energy:
        value_template: >
          {{ (states('sensor.energy_consumption_total') | float - states('sensor.energy_production_total') | float) | round(1) }}
        unit_of_measurement: 'kWh'

The problem is that energy_consumption_total remains unknown, it also doesn’t show any unit_measurement. The template developer tools can parse the template just fine and calculates it as it should.

As a result, any derivative sensor using the utility_meter component also fails. I can’t find anything in the logs that indicates a failure in the template. What could be going on here?

if it’s not obvious to HA which entities to monitor, it just won’t.
List the entities that need to be monitored:

Are there any related warning or error messages in the log?

I don’t think the problem is that it can’t figure out which entities to watch. In this case it’s pretty straightforward (and it’s clearly working for sensor.energy_production_total which is almost identically configured, and I don’t see any typos.)

EDIT: Or…

I think the problem may simply be timing related.

I.e., a template sensor does not update itself when it is initialized. It will only update when one of its “input” entities changes (and the template can be successfully rendered.) So in this case what is probably happening is, when sensor.energy_consumption_total is being initialized, its “input” entities (i.e., sensor.energy_consumption_tarif_1 and sensor.energy_consumption_tarif_2) have probably already updated, and then are probably not updating again for a while. During this period sensor.energy_consumption_total will not have updated yet and hence will display as unknown. (The fact that it does not show a unit_of_measurement during this time further corroborates this theory.)

If you look on the History page, do you see that sensor.energy_consumption_total finally does update once one of its input entities updates?

To solve this problem, you could add an automation that triggers some amount of time after the system is initialized, and then calls homeassistant.update_entity to update sensor.energy_consumption_total. Something like:

- trigger:
  - platform: homeassistant
    event: start
  action:
  - delay: 5
  - service: homeassistant.update_entity
    entity_id: sensor.energy_consumption_total
1 Like

@lolouk44 Your suggestion seemed promising, but didn’t fix it.

@pnbruckner Last hour sensor.energy_consumption_tarif_2 has increased in value (I had to wait for the sun to go down because of solar panels), but sensor.energy_consumption_total remains unknown. I also tried to manually call the homeassistant.update_entity service but that also did not update the entity at all. Note that sensor.energy_consumption_tarif_1 did not update as both tarifs are mutually exclusive in update times.

I agree that it is interesting that the energy_production part of it works as it should. Your assumption that it might be timing related somehow could still be true though, as the template itself works perfectly fine in the template editor.

Another theory that I have is that it might be some “restoring from old state” issue. The friendly name of the sensor is different from sensor.energy_production_total, especially the braces around the word “total”. It’s not something that home assistant can infer from the entity_id.

After removing all sensors (except for the DSMR platform) the production total sensor disappears, but the consumption total sensor remains. Only after removing the DSMR platform as well the consumption sensors is gone. I have a feeling that somewhere in the platform this sensor is trying to be restored, but I can’t verify it without diving into the source code, which is what I’ll be doing next.

To confirm my last theory: After renaming the total sensors to something different both work immediately. sensor.energy_consumption_total is still there as well, not doing anything :sweat_smile:.

Hmm. Is something else (still???) trying to create sensor.energy_consumption_total???

You could try removing it using Configuration -> Entities. Do you see this icon on the right of it?

image

If so, if you hover the mouse over it, it will say “Restored”. Click on it, and in the window that pops up, click DELETE and then OK.

My guess is somehow the entity_id sensor.energy_consumption_total got reserved (maybe you created it differently before???), and when you tried to create a new sensor with the same entity_id, it wouldn’t create the new one. (Still, I would have thought you would have seen some warning or error in the log!!)