Template sensor: using "this" variable

This is incorrect.

The post you are responding to is using the contemporary format which uses state not value_template like the legacy format . The contemporary format is required for the trigger-based sensor they were trying to set up.

In HA, states are always strings. You could store a serialised JSON string though if you stay within the 256 character limit. Attributes can store objects.

Based on last example, is this possibile?

- sensor:
    - name: "Name of sensor"
      state: >-
        {% if ........ %}
             {{ "A" }}
        {% else %}
             {{ "B" }}
        {% endif %}
      attributes:
        is_A: "{{ (this.state == 'A') }}"
        is_B: "{{ (this.state == 'B') }}"

or

- sensor:
    - name: "Name of sensor"
      variables:
        Z: "{{ many IFs and checks of OTHER entities. The final value could be 'A' or 'B' }}"
      state: "{{ Z }}"
      attributes:
        is_A: "{{ (Z == 'A') }}"
        is_B: "{{ (Z == 'B') }}"

it seems not in both cases but I don’t want to have made any mistake trying those…