Adding two sensor values - template sensor

I’m setting up the energy dashboard in ha. Reading the momentary power from my electricity meter is working (see “- platform: sml” in the yaml file). Reading the generated solar power is also working (see “- platform: pulse meter”).

The momentary consumed power I want to calculate by adding the momentary power (value can be “+” when energy flows form the grid in my home or is “-” when I am feeding the grid) with the generated solar power (see “-platform: template”) But this does not work, please see the picture with the error message, and I don’t figure out, where the yaml is wrong. Can anyone help?

# Zählerstand Strom-Bezug  
sensor:
  - platform: sml
    name: "Zaehler_Bezug"
    sml_id: mysml
    obis_code: "1-0:1.8.0"
    unit_of_measurement: kWh
    accuracy_decimals: 1
    filters:
     - multiply: 0.0001

# Momentane Leistung ("+" bei Bezug, "-" bei Einspeisung)      
  - platform: sml
    name: "Momentanleistung"
    sml_id: mysml
    obis_code: "1-0:16.7.0"
    unit_of_measurement: W
    accuracy_decimals: 0
    device_class: power
    filters:
     - multiply: 0.1
     - heartbeat: 10 s
    id: momentanleistung

# Elektrische Energie (Integration der Momentanleistung); kann gelöscht werden, da sie auch negative Werte der Momentanleistung aufintegriert !?
  - platform: total_daily_energy
    name: "Verbrauch_Bezug"
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh
    # Die power_id muss bei diesem Sensor angegeben werden, damit er richtig integrieren kann.
    power_id: momentanleistung 
    

# Aktuell erzeugte PV-Leistung
  - platform: pulse_meter
    pin: D2
    unit_of_measurement: W
    name: "Einspeisung_Leistung"
    accuracy_decimals: 0
    device_class: power
    filters:
     - multiply: 6
     - heartbeat: 10s
    icon: 'mdi:flash'
    # Die id ist hier nicht zwingend erforderlich. Nur dann, wenn kein Name angegeben wurde.
    id: einspeisung_leistung

# Erzeugte PV-Energie (Integration der erzeugten PV-Leistung)    
  - platform: total_daily_energy
    name: "Einspeisung_Energie (Tag)"
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh
    power_id: einspeisung_leistung
    device_class: energy

# Aus dem Netz bezogene Leistung (nur "+")
  - platform: template
    name: bezug_leistung
    unit_of_measurement: W
    lambda: |-
     return "{{(states('sensor.momentanleistung')|float + (states('sensor.einspeisung_leistung')|float)}}";
    update_interval: 10 s
    accuracy_decimals: 0
    device_class: power
        #  filters:
         #   - multiply: 0.000001

# Aus dem Netz bezogene Energie
  - platform: total_daily_energy
    name: "Bezogene_Energie"
    filters:
     - multiply: 0.001
    unit_of_measurement: kWh
    power_id: bezug_leistung
    device_class: energy
  
# diese Zeile ist erforderlich, damit der Sensor total_daily_energy täglich zurückgesetzt wird    
time:
  - platform: homeassistant
    id: homeassistant_time   

You have no entity with id: bezug_leistung

I added the missing id and yes, the yaml-file is free of any syntax-errors now (thought, the name of the sensor makes the id obsolet).
Unfortunately there is another error-messag when compiling the file:
I don’t know how to fix the message “could not convert … from …”

Please post text, not screenshots.

Lambda’s must be written in C++, and that error line doesn’t have the C++ syntax.

So it should be something like

lambda: |-
  return (id(momentanleistung).state + id(einspeisung_leistung).state);

if that are the id’s of your sensors

@jsuanet, @nickrout, thank you both, it works! Great help!

Hay! I query my meters (PV yield, import and feed-in) via MQTT.
Do you think I can use your lines modified?