Template errors on startup which I don't understand

I’ve been trying to sort out template errors on startup where a lot of sensors are unknown/unavailable for obvious reasons.

A majority seem to be lovelace (or Mushroom where you can template icon colours etc.)

Here is an example error :

Error while processing template: Template<template=({% if (states('sensor.gas_usage_hourly')|is_number and states('sensor.hot_water_hourly'))| is_number %} {{ (states('sensor.gas_usage_hourly') + states('sensor.hot_water_hourly'))| round(2) }} kWh {% else %} 0{% endif %) renders=2>

What’s really odd is I changed that line to say or instead of and but it continues to insist it’s the old version, but that aside - here’s the code. Am I doing something wrong? (There are loads more but this seems to be the same issue as the others)

{% if (states('sensor.gas_usage_hourly')|is_number or states('sensor.hot_water_hourly'))| is_number %}   
  {{ (states('sensor.gas_usage_hourly') + states('sensor.hot_water_hourly'))| round(2)|default(0)|default(0) }} kWh
{% else %}
  0
{% endif %}

Is there some bizarre cache somewhere?

That template is flawed in multiple ways: you must convert sensor states (which are always strings) to numbers before doing arithmetic on them.

{% if (states('sensor.gas_usage_hourly')|is_number or states('sensor.hot_water_hourly'))| is_number %}   
------^---------brackets don't match---------------------------------------------------^

Ignoring the syntactically-correct but functionally-wrong brackets, you’re checking if either sensor can be interpreted as a number, then trying to add them. If only one is a number, this will fail.

You want:

{{ (states('sensor.gas_usage_hourly')|float(0) + states('sensor.hot_water_hourly')|float(0)|round(2) }} kWh

The |float(0) converts the string state to a number, and uses 0 as a fallback if it fails.

Better to create a template sensor and display that rather than do manipulations in the dashboard.

I see that, and I can sort it, but I still do not understand why I am getting errors for code that doesn’t exist.

I have done a file search for the offending line - and this is the result :

That line doesn’t exist - so why is it an error?!

Your file search might not include all of the dashboard files. Try editing the dashboard, then three dots / raw configuration editor and search through your Lovelace YAML.

You may also need to look at any UI-created Helpers.

Trust me - it does search the lovelace files. If I look for something that does exist it comes up.

I’m searching .storage and it brings up anything in core - such as helpers.

I’ve solved the issue with the odd caching - it’s reading whatever is in the developer tools template box …now to sort the rest out!

1 Like

Right, thanks for the float(0) hint. I was using int(0) for most other errors. Fixed everything finally. It’s very annoying that local startup errors with anything in developer tools “template”. I had to go around all my devices and clear everything down :slight_smile:

Still have some “floaters” that don’t exist in my actual config. Anyone got any idea where HA looks for anything that is in the developer tools template editor? It is obviously stored somewhere but I can’t find it :frowning: