Template sensor difference on Outlet energy unavailable

Hi to all,
I’ve configured a sensor template to calculate the net consumption of the outlet line circuit, using the following code, but the sensors is “unavailable”

- platform: template
  sensors:
    outlet_net_energy:
      unique_id: f48b4cd2-bc87-460b-9f57-f464250fe194
      entity_id:
        - sensor.power_meter_em1_total_active_energy
        - sensor.lavatrice_energy
        - sensor.frigo_energy
        - sensor.forno_energy
      value_template: >-
        {% set outlet_total = states.sensor.power_meter_em1_total_active_energy.state|float %}
        {% set forno = states.sensor.lavatrice_energy.state|float %}
        {% set frigo = states.sensor.frigo_energy.state| float %}
        {% set lavatrice = states.sensor.forno_energy.state| float %}
        {{ (outlet_total - forno - frigo - lavatrice) }}

Can someone can help me to figure out what’s the problem?

That code belongs to last decade: legacy format and listing the entities used in the template (deprecated in v0.115! ref, see Templates section). See the up-to-date documentation. Where did you get this code syntax from?

Check that each of your entities is reporting a value. Your template will fail if any of them is unavailable. Mine (below) will default to 0 if a sensor is not available.

Use the UI to configure a template sensor helper with this state template:

{% set outlet_total = states('sensor.power_meter_em1_total_active_energy')|float(0) %}
{% set forno = states('sensor.lavatrice_energy')|float(0) %}
{% set frigo = states('sensor.frigo_energy')|float(0) %}
{% set lavatrice = states('sensor.forno_energy')|float(0) %}
{{ (outlet_total - forno - frigo - lavatrice) }}

Like this:

1 Like

Great
Works flawlessly
Thanks

1 Like