How do i affect a value in a template sensor with conditions?

Hi I have the following template sensor that I am trying to implement but I can’t get the value of “assigned_value” (see below ) to be assigned to the sensor.

Anyone able to help with this ?

    energie_fantome:
      friendly_name: Energie Fantome
      unit_of_measurement: 'Watt'
       value_template: >-
        {% set cerbogxacoutwatt = states('sensor.cerbogxacoutwatt') | float(0) %}
        {% set cerbogxacinwatt = states('sensor.cerbogxacinwatt') | float(0) %}
        {% set cerbogxmppttotal = states('sensor.cerbogxmppttotal') | float(0) %}
        {% set cerbogxbatteriewatt = states('sensor.cerbogxbatteriewatt') | float(0) %}
          {% if cerbogxbatteriewatt < 0 %}
                 {% set In_Victron_Total = cerbogxacinwatt+cerbogxmppttotal+(-cerbogxbatteriewatt) %}
                 {% set assigned_value = In_Victron_Total - cerbogxacoutwatt %}
          {% else %}
                 {% set In_Victron_Total = cerbogxacinwatt+cerbogxmppttotal %}
                 {% set assigned_value = In_Victron_Total - cerbogxacoutwatt - cerbogxbatteriewatt %}
          {% endif %}

In_Victron_Total has no value.

My mistake, I changed the variables names before posting.

I fixed my mistake but still, I have no returned value

You don’t have any expressions… {{ }}

Expression are what “print” the output. You need a {{ assigned_value }} after the {% endif %}.

@Didgeridrew , thanks for helping me on this, after searching through the forum, I modified my .yaml to the modern template sensor . But even if I modified the code and add {{ assigned_value }} after the {% endif %}, when I look at the entity sensor, (sensor.energie_fantome) there are no value change (i tested the code in the development tools and i can see that the {{ assigned_value }} change and is shown as a return value

  • name: “energie_fantome”
    unit_of_measurement: ‘Watt’
    state: >
    {% set cerbogxacoutwatt = states(‘sensor.cerbogxacoutwatt’) | float(0) %}
    {% set cerbogxacinwatt = states(‘sensor.cerbogxacinwatt’) | float(0) %}
    {% set cerbogxmppttotal = states(‘sensor.cerbogxmppttotal’) | float(0) %}
    {% set cerbogxbatteriewatt = states(‘sensor.cerbogxbatteriewatt’) | float(0) %}
    {% if cerbogxbatteriewatt < 0 %}
    {% set In_Victron_Total = cerbogxacinwatt+cerbogxmppttotal+(-cerbogxbatteriewatt) %}
    {% set assigned_value = In_Victron_Total - cerbogxacoutwatt %}
    {% else %}
    {% set In_Victron_Total = cerbogxacinwatt+cerbogxmppttotal %}
    {% set assigned_value = In_Victron_Total - cerbogxacoutwatt - cerbogxbatteriewatt %}
    {% endif %}
    {{ (assigned_value | round(1)) }}

Finaly, I erased all my templates, re-created this one and for an unknown reason data started flowing to the sensor…

sorry for loosing your time and thanks again for your help