Migration to new Template Sensor 2024.11

Dear Community,

I am trying to move a tempalte sensor to the new tempalte integration but i did not get it running.

The old template sensor ist the following:

 - platform: template
   sensors:
     hausverbrauch:
       friendly_name: 'Haus Verbrauch'
       unique_id: HausVerbrauch
       unit_of_measurement: 'W'
       state_class: measurement
       device_class: power
       value_template:  >
           {{ states('sensor.pv_live') | float -
              states('sensor.solaredge_modbus_m1_ac_power') | float }}

My current state of the template sensor ist the following:

 sensor:
   - name: "Haus Verbrauch"
     unit_of_measurement: "W"
     state_class: measurement
     device_class: power
     state: " {{ states.sensor.PV_Live.state - states.sensor.solaredge_modbus_m1_ac_power.state | float(0) }}"

Any hint what is wrong an need to be changed?

Kind regards

For one, there is a spare bracket here.

Thank you for point out the additional bracket.
Looks like one copy paste issue, but did not fix it.

All entity state values are handled as strings by default (even when they appear to be numeric).

Ensure both sensor values are first converted to either float or int prior to attempting to subtract them.

sensor:
   - name: "Haus Verbrauch"
     unit_of_measurement: "W"
     state_class: measurement
     device_class: power
     state: "{{ states('sensor.pv_live') | float(0) - states('sensor.solaredge_modbus_m1_ac_power') | float(0) }}"

I hope OP knows that the full syntax is

template:
  - sensor:
      - name: "Haus Verbrauch"

Thank you @123 for point out that all values are strings.
My old tempalte config took this into account. Changed the new one to do the same without success.

Is there any kind of Debug function to understand what might be wrong?

Thank you @Ildar_Gabdullin
This was just the sinplet of my acutal config which is a dedicated file for the named sensor.
The file is sourced by the folloing string:

template: !include_dir_merge_list cfg/template

Then you did something else wrong.

Post the exact configuration as it appears in whichever file contains it in the cfg/template directory.

I assume you’re executing Reload Template Entities after making changes?

Check the Log for errors.

I have checked the Logs and did not see any Errors regarding the sensor. I used the Template Checker in the Dev-Tools and my state was working.

So it seems to be related to loading the config.
Neither the Config Checker nor the Studio Code Server indicated an issue of a missing “-” in front of the sensor :frowning:
Adding the “-” fixed it. My Bad!
Learning: Never trust a working config check or an error free Stuido Code Server for getting a working config.

My configuration.yaml contains:
template: !include_dir_merge_list cfg/template

the file cfg/template/HausVerbrauch.yaml is

- sensor:
  - name: "Haus Verbrauch"
    unit_of_measurement: "W"
    state_class: measurement
    device_class: power
    icon: mdi:meter-electric-outline
    state: "{{ states('sensor.pv_live') | float(0) - states('sensor.solaredge_modbus_m1_ac_power') | float(0) }}"

Thank you for your tipps and support.