Conditional value for sensor

I’m trying to define the value of a sensor based on a condition. Most of the articles I’ve read say I should be using a value_template but when I try that it says value_template not allowed and my sensor is missing a state value, not sure what I’m missing

template:
  - sensor:
      - name: inverter_exporting
        unit_of_measurement: W
        device_class: energy
        state_class: measurement
        state: 
          {{% if is_state_attr('sensor.inverter_energy_today', 'export_power_indicator', 0) %} state_attr('sensor.inverter_energy_today', 'export_power') | abs
          {% else %} 0
          {% endif % }}
template:
  - sensor:
      - name: inverter_exporting
        unit_of_measurement: W
        device_class: energy
        state_class: measurement
        state: >
          {% if is_state_attr('sensor.inverter_energy_today', 'export_power_indicator', 0) %} 
            {{ state_attr('sensor.inverter_energy_today', 'export_power') |float(0)| abs }}
          {% else %} 
            0
          {% endif %}
1 Like

that’s for the legacy style template sensor (which are still valid but the syntax is different) but the new style template sensors use “state:” instead.

look in the docs for the differences.