Sensors for Exportation and Real Energy consumption

Hello. I’m trying to set a sensor that controls PV production, home consumption, real consumption (when you are not producing enough) and exportation (when you produce more than you consumed.

I created this sensors:

- platform: template



        friendly_name: "Consumo de Red Diario"

        unit_of_measurement: "kWh"

        value_template: "{{ (states('sensor.consumo_de_red_diario')|float + (states('sensor.house_consumption_energy_daily')|float - states('sensor.solar_yield_daily')|float)) if states('sensor.house_consumption_energy_daily')|float > states('sensor.solar_yield_daily')|float else states('sensor.consumo_de_red_diario') }}"


        friendly_name: "Exportación Diaria"

        unit_of_measurement: "kWh"

        value_template: "{{ (states('sensor.exportacion_diaria')|float + (states('sensor.solar_yield_daily')|float - states('sensor.house_consumption_energy_daily')|float)) if states('sensor.solar_yield_daily')|float > states('sensor.house_consumption_energy_daily')|float else states('sensor.exportacion_diaria') }}"

It should only sum the differences and only when I export more than consume or vice versa, but the values are always “not available”

Can you please help me?