Energy Template down after update (Core Update 2023.2.0)

I have a template for my energy consumption. Everything worked fine, but when I do the Home Assistant Core Update 2023.2.0 I don’t get any values anymore.

# Strom
  - platform: template
    sensors:
      template_verbrauch:
        unique_id: "aktueller Verbrauch"
        unit_of_measurement: 'W'
        value_template: "{{(states('sensor.power_ac_fronius_inverter_1_http_192_168_0_189')|float(0.0)) - (states('sensor.aktueller_uberschuss')|float(0.0)) + (states('sensor.aktueller_bezug')|float(0.0))|round(0) }}"

  - platform: template
    sensors:
      template_ueberschuss_bezug:
        unique_id: "Überschuss/Bezug"
        unit_of_measurement: "W"
        value_template: "{{ (states('sensor.aktueller_uberschuss') | float) - (states('sensor.aktueller_bezug') | float) | round(0) }}"

Maybe someone can help me. I read through similar topics but I can’t find any solution.

Thank you in advance

What are the states you are trying to calculate with currently? The second template is missing default values fro float (and maybe a pair of parentheses).

Have you had a look in the logs?

I did already a backup to restore former settings. One state was unknown and one 0 as far as I can remember. In the logs it said it‘s a problem with float. I could do an update again this evening to get all the details again.

I already had a similar problem several month ago and could fix it by myself- just had to change the „format“. I guess it will be the same this time but couldn‘t find the solution so far.

Thank you already for your help!

That’s the way it looked before the update
Bildschirmfoto 2023-02-02 um 20.48.37

and this ist after
Bildschirmfoto 2023-02-02 um 21.04.32

LOGs:

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:364 
First occurred: 21:03:41 (1 occurrences) 
Last logged: 21:03:41

TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('sensor.aktueller_uberschuss') | float) - (states('sensor.aktueller_bezug') | float) | round(0) }}' but no default was specified') while processing template 'Template("{{ (states('sensor.aktueller_uberschuss') | float) - (states('sensor.aktueller_bezug') | float) | round(0) }}")' for attribute '_attr_native_value' in entity 'sensor.template_ueberschuss_bezug'
Logger: homeassistant.helpers.event
Source: helpers/template.py:459 
First occurred: 21:03:41 (1 occurrences) 
Last logged: 21:03:41

Error while processing template: Template("{{ (states('sensor.aktueller_uberschuss') | float) - (states('sensor.aktueller_bezug') | float) | round(0) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1745, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 457, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1984, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1748, in forgiving_float_filter
    raise_no_default("float", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1446, in raise_no_default
    raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('sensor.aktueller_uberschuss') | float) - (states('sensor.aktueller_bezug') | float) | round(0) }}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 575, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 459, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (states('sensor.aktueller_uberschuss') | float) - (states('sensor.aktueller_bezug') | float) | round(0) }}' but no default was specified

You could do as the error message tells you and define a default value. Like in your first template. Eg 0: | float(0) instead of | float.

Ok, I’ll try that out and report you again in the evening.

Sorry - I have nothing to do with computer in my life and everything is self educated. So the logs are absolutely spanish for me. I appreciate your help!

I suggest rewriting your code and try again.

I got the exact same error log:

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:364
First occurred: 12:38:44 PM (1 occurrences)
Last logged: 12:38:44 PM

TemplateError('ValueError: Template error: as_timestamp got invalid input 'unknown' when rendering template '{% if as_timestamp(now()) - as_timestamp(states('sensor.power_meter_last_seen')) > 120 %} on {% else %} off {% endif %}' but no default was specified') while processing template 'Template("{% if as_timestamp(now()) - as_timestamp(states('sensor.power_meter_last_seen')) > 120 %} on {% else %} off {% endif %}")' for attribute '_state' in entity 'binary_sensor.apagon_luz'

What did you do?

I‘ll try something out this weekend. I‘ll tell you if I get it working. Like complex1 frank suggested I will rewrite the code.

Try the code below, but note that I haven’t tested it:

# Strom
template:
  - sensor:
      - name: "Aktueller Verbrauch"
        unique_id: "aktueller_verbrauch"
        unit_of_measurement: "W"
        state: >
          {{ (states('sensor.power_ac_fronius_inverter_1_http_192_168_0_189') | float(0) - 
              states('sensor.aktueller_uberschuss') | float(0) + 
              states('sensor.aktueller_bezug') | float(0)) | round(0) }}

  - sensor:
      - name: "Überschuss/Bezug"
        unique_id: "uberschuss_bezug"
        unit_of_measurement: "W"
        state: >
          {{ (states('sensor.aktueller_uberschuss') | float(0) - 
              states('sensor.aktueller_bezug') | float(0)) | round(0) }}"

Back again! I found the solution - the main problem was, that I got some values from MQTT. The Mqtt Integration had to be reconfigurated with the new Update so the names for the code where wrong.

I also tried a new format for the template configuration which has a bit more clarity for me:

# Strom 
template:
  - sensor:
      - name: "aktueller Verbrauch"
        unit_of_measurement: "W"
        unique_id: aktueller_Verbrauch
        state: >
          {% set Fronius = states('sensor.power_ac_fronius_inverter_1_http_192_168_0_189') | float %}
          {% set Ueberschuss = states('sensor.2_7_0') | float %}
          {% set Bezug = states('sensor.1_7_0') | float %}

          {{ (Fronius - Ueberschuss + Bezug)  | round(0, default=0) }}

In the first 3 lines you define the values and in the last one you do the calculation.

Thank you for your support!

@ChileHA - hopefully this helps you as well

reconfigure in what way?

I did open the mqtt integration in configuration - integrations and there was the option configure mqtt new. As you can see in the picture.

Afterwards you can find the MQTT sensors/entities in the integration

Bildschirmfoto 2023-02-14 um 14.57.37

Before this I have added the senors with the file editor and therefore the IDs for the entities have changed which I used in the template.