Templates no longer loads in Home Assisant

Hi,

All my custom sensor have disappeared.
It’s not that they show an error, they straight up does not load. Any sensors from helpers, using the customer templated sensors, just show ‘unavailable’.

Until recently they all worked just fine, and have been for months. Then they didn’t. I have not made any changes in the config.

Running supervised HA in a virtual machine, latest updates (updating and restarting have not helped).

My template setup:

#### TEMPLATES ####
template:
  - sensor:
      #POWER CALCULATIONS:
      ##Outputs the power user by the wall connector in W
      - name: "Tesla Wall Connector Power"
        state_class: "measurement"
        unit_of_measurement: "W"
        icon: mdi:battery-charging
        state: "{{ (((states.sensor.tesla_wall_connector_phase_a_voltage.state | float) * states.sensor.tesla_wall_connector_phase_a_current.state | float) + ((states.sensor.tesla_wall_connector_phase_b_voltage.state | float) * states.sensor.tesla_wall_connector_phase_b_current.state | float) + ((states.sensor.tesla_wall_connector_phase_c_voltage.state | float) * states.sensor.tesla_wall_connector_phase_c_current.state | float)) | round(0) }}"
      ##Outputs the power user by the wall connector in kW
      - name: "Tesla Wall Connector Power kW"
        state_class: "measurement"
        unit_of_measurement: "kW"
        icon: mdi:battery-charging
        state: "{{ ((((states.sensor.tesla_wall_connector_phase_a_voltage.state | float) * states.sensor.tesla_wall_connector_phase_a_current.state | float) + ((states.sensor.tesla_wall_connector_phase_b_voltage.state | float) * states.sensor.tesla_wall_connector_phase_b_current.state | float) + ((states.sensor.tesla_wall_connector_phase_c_voltage.state | float) * states.sensor.tesla_wall_connector_phase_c_current.state | float)) / 1000) | round(0) }}"
      ##Total number of kWh used by the Wall Connector to date
      - name: "Tesla Wall Connector Energy kWh"
        state_class: "measurement"
        unit_of_measurement: "kWh"
        icon: mdi:ev-station
        state: "{{ states('sensor.tesla_wall_connector_energy') | float / 1000 | round(2) }}"
      #COST CALCULATIONS:
      #Outputs the NET DKK spent by the wall connector in DKK
      - name: "Tesla Wall Connector Money Spent DKK brutto"
        state_class: "measurement"
        unit_of_measurement: "DKK"
        icon: mdi:cash-100
        state: "{{ (states('sensor.tesla_wall_connector_monthly_energy_kwh') | float * (state_attr('sensor.energi_data_service', 'current_price') | float))  | round(2) }}"
      #Outputs the DKK spent by the wall connector in DKK after deduction of the TAX Cash Back
      - name: "Tesla Wall Connector Money Spent DKK netto"
        state_class: "measurement"
        unit_of_measurement: "DKK"
        icon: mdi:cash-100
        state: "{{ (states('sensor.tesla_wall_connector_monthly_energy_kwh') | float * (state_attr('sensor.energi_data_service', 'current_price')-0.86 | float))  | round(2) }}"
      #Outputs the Tax Cash Back amount (0.86 DKK/kWh spent sharging the vehicle)
      - name: "Tesla EV Charging Cash Back DKK"
        state_class: "measurement"
        unit_of_measurement: "DKK"
        icon: mdi:hand-coin
        state: "{{ (states('sensor.tesla_wall_connector_monthly_energy_kwh') | float * 0.86 | float)  | round(2) }}"

      #Outputs the met.no outzide temperature
      - name: "Outside Temperature"
        state_class: "measurement"
        unit_of_measurement: "°C"
        icon: mdi:temperature-celsius
        state: "{{ state_attr('weather.home', 'temperature') | float }}"

      # Outputs the Net Operator tariff #
      - name: "Net Operator Tariff"
        state_class: "measurement"
        unit_of_mesurement: "DKK/kWh"
        icon: mdi:cash
        state: "{{ state_attr('sensor.net_operator_tariff', 'current_tariff') | float}}"

      #########################
      ### LOW BATTERY LIST ####
      #########################
      ##### Low Battery list ##################################
      - name: "Low Battery Devices"
        unique_id: ba292875-ae67-480d-9867-29cdb20afaea
        icon: >
          mdi:battery-low
        state: >
          {% set threshold = states('input_number.battery_threshold') | int %}
          {%- set ns = namespace(sensors=[]) -%}
          {%- for state in states.sensor 
            | selectattr('attributes.device_class', 'defined') 
            | selectattr('attributes.state_class', 'defined') 
            | selectattr('attributes.device_class', '==', 'battery') 
            | selectattr('attributes.state_class', '==', 'measurement') 
            | selectattr('state', 'is_number') -%}
            {%- if state.state | int <= threshold -%}
              {% set ns.sensors = ns.sensors + [dict(name = state.name | replace(' battery', '') | replace(' Battery', '') | replace(' level', '') | replace(' Level', ''), state = state.state | int)] %}
            {%- endif -%}
          {%- endfor -%}
          {%- set batt = ns.sensors | sort(attribute='state') %}
          {%- set ns = namespace(batt='') -%}
          {%- for state in batt -%}
              {% set ns.batt= ns.batt + (state.name  ~ ' (' ~ state.state ~'%)' ~ "\n") %}
          {%- endfor -%}

          {% if ns.batt | count > 0 %}
            {{ ns.batt | truncate(255, true, '...') }}
          {% else %}
            {{ 'unavailable' }}
          {% endif %}

      #################################
      ### DEAD Z-Wave Device Sensor ###
      #################################
      - name: "Offline ZWave Devices"
        unique_id: offline_zwave_devices
        icon: mdi:z-wave
        unit_of_measurement: Devices
        state: >
          {% set devices = state_attr(this.entity_id,'entity_id') %}
          {% if devices != none %} {{ devices|count }}
          {% else %} {{ devices }}
          {% endif %}
        attributes:
          entity_id: >
            {{ expand(integration_entities('Z-Wave JS') )
                |selectattr('entity_id', 'search', 'node_status')
                |selectattr('state','in','dead, unavailable, unknown')
                |map(attribute="object_id")
                |map('regex_replace', find='(.*)_node_status', replace='button.\\1_ping', ignorecase=False)|list }}

Please help…

You’ll have to post the errors in your logs.

Hi @petro

Thank you for getting back to me.

I read thru the log and only found this warning regarding the [homeassistant.config].
The rest was regarding other stuff like HomeKit, ESPhome and I think not relevant - but let me know if it can bring context.

Did a complete reboot of the system, HA and all components.
From the log:

2023-05-03 10:31:40.002 ERROR (MainThread) [homeassistant.config] Invalid config for [template]: [unit_of_mesurement] is an invalid option for [template]. Check: template->sensor->7->unit_of_mesurement. (See /config/configuration.yaml, line 40).

Line 40 in configuration.yaml is coincidentally the line “template:”

Can you figure out what the error is.
It seems like it just skips all templates…

When I run one of the sensor templates in the template testing tool - everything works just fine…

Sensor

##Total number of kWh used by the Wall Connector to date
      - name: "Tesla Wall Connector Energy kWh"
        state_class: "measurement"
        unit_of_measurement: "kWh"
        icon: mdi:ev-station
        state: "{{ states('sensor.tesla_wall_connector_energy') | float / 1000 | round(2) }}"

Result:

##Total number of kWh used by the Wall Connector to date
      - name: "Tesla Wall Connector Energy kWh"
        state_class: "measurement"
        unit_of_measurement: "kWh"
        icon: mdi:ev-station
        state: "6573.194"

Fixed the typo in the sensor template ( mesurement → measurement ), btw. But it did not fix the missing templates.

All your templates are missing defaults for floats, are you sure you don’t have other errors in your logs on startup? This is most likly the reason they are not working. As a sidenote, this change was enforced many versions ago.

1 Like

Yes, there are no other errors in the log, apart from some things relating to HomeKit, and ZHA not having started soon enough, etc, but not really related.

The sensors all worked 2-3 days ago, but are now not even present in the entity list as broken.

I’m afraid I don’t really follow the guide you have linked (I’m not a developer) - can you maybe make an example from one of my existing templates?

I appreciate your help.

you have | float, a default of 0 would be | float(0). An example pulled directly from that link, all you have to do is read it.

You can also just add availability templates instead

availability: "{{ states('sensor.tesla_wall_connector_monthly_energy_kwh') | is_number and state_attr('sensor.energi_data_service', 'current_price') | is_number }}"

You’d have to make one for each sensor using the appropriate sensors.

Hi @petro

I must’ve missed it when I read it through.

I have now changed all float → float(0) and it actually seems to have solved the problem.

It’s strange though, I have sensors not including any float values, and they did not work either, i.e. the “Low Battery Devices” sensor - but they’re also working now…

Thank you for your help - you saved the day :tada:

I’m not sure how you made it this long without fixing that. It was made a warning in 2021, then made an error in 2022.9. So you’re 6 months into this being an issue essentially

yeah, he fixed that a few posts back

Hi @Troon

Yes, this was fixed as soon as I found the log.

Still strange that there’s no logs pointing to anything being wrong, and when I fixed the default value on the float → float(0) that also fixed sensors that did not have any floats in them… :thinking:

Oops, missed that. Post removed for brevity.