Filter values in a template

Hello, I am a very beginner HA user. I managed to establish a connection via Modbus with the photovoltaic inverter. But I have a problem with several registers that send false values when the inverter is started and turned off.

I’m currently using the code:

    - name: "fronius_voltage_DC_string1"
      unit_of_measurement: "V"
      device_class: "voltage"
      state: "{{ states('sensor.fronius_voltage_DC_string1_RAW') |int * 10 ** states('sensor.fronius_voltage_DC_SF') | int }}"

The “fronius_voltage_DC_string1_RAW” entity sends valid data alternating with the false value 65535.

I would like HA to ignore the value 65535 and take only the previous correct value instead.

Is something like this possible to do?
Please help.

P.S. My English is poor…

i’m presuming this is defining a sensor, so i’m using this.state
also is this a string like the name implies? i put a string ‘65535’ below instead of an int

- name: "fronius_voltage_DC_string1"
      unit_of_measurement: "V"
      device_class: "voltage"
      state: "{{ (states('sensor.fronius_voltage_DC_string1_RAW') |int * 10 ** states('sensor.fronius_voltage_DC_SF') | int) if (states('sensor.fronius_voltage_DC_string1_RAW') != '65535' else this.state }}"

or make it trigger based

template:
  - trigger:
      - platform: state
        entity_id: sensor.fronius_voltage_DC_string1_RAW
        not_to:
          - unavailable
          - unknown
          - "65535"
    sensor:
      - unique_id: "add something unique here, maybe an UUID"
        name: "fronius_voltage_DC_string1"
        unit_of_measurement: "V"
        device_class: "voltage"
        state: >
          {% set raw = states('sensor.fronius_voltage_DC_string1_RAW') | int %}
          {% set power = states('sensor.fronius_voltage_DC_SF') %}
          {{ raw * 10 ** power | int if power | is_number else this.state }}