Template sensor with attributes - How can I (re)use a variable?

Sorry if it’s a newbie question, none of this is my native language and I’ve been been googeling all day without luck.
How can I set a variable and use it for output as an attribute? For some reason, currentPrice won’t keep its value past the first print in this example:

 #Template:
 #  sensor:
     - name: "test"
       state: >-
         {% set currentPrice = 1234 %}
         {{ currentPrice }}
       attributes:
         some_attribute: >-
           {{ currentPrice }}

Last line doesn’t work - the output is just an empty string:
some_attribute: ''

Not exactly. But you can use self-referencing.

Thank you kindly for your reply!
I realize my example above is a little misleading. State and attribute(s) will not be the same.

#Template:
#  sensor:
    - name: "test"
      state: >-
        {% set morningPrice = 1234 %}
        {% set currentPrice = 5678 %}
        {% set lossYield = currentPrice - morningPrice %}
        {{ lossYield }}
      attributes:
        some_attribute: >-
          {{ currentPrice }}
        other_attribute: >-
          {{ morningPrice }}

did you find a solution, I have same problem, here is my code

- trigger:
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_00
      to: "off"
      id: Input_00K
      from: "on"
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_00
      to: "on"
      id: Input_00L
      for:
        hours: 0
        minutes: 0
        seconds: 1
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_08
      to: "off"
      id: Input_08K
      from: "on"
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_08
      to: "on"
      id: Input_08L
      for:
        hours: 0
        minutes: 0
        seconds: 1
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_16
      to: "off"
      id: Input_16K
      from: "on"
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_16
      to: "on"
      id: Input_16L
      for:
        hours: 0
        minutes: 0
        seconds: 1
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_24
      to: "off"
      id: Input_24K
      from: "on"
    - trigger: state
      entity_id:
        - binary_sensor.ihc1_input_24
      to: "on"
      id: Input_24L
      for:
        hours: 0
        minutes: 0
        seconds: 1
  condition:
    - condition: or
      conditions:
        - condition: and
          conditions:
            - condition: template
              value_template: >-
                {{ trigger.to_state.last_updated -
                trigger.from_state.last_updated  < as_timedelta("00:00:01")
                }}
            - condition: template
              value_template: "{{ trigger.to_state.state == 'off'}}"
        - condition: and
          conditions:
            - condition: template
              value_template: >-
                {{ trigger.to_state.last_updated -
                trigger.from_state.last_updated  >= as_timedelta("00:00:01")
                    }}
            - condition: template
              value_template: "{{ trigger.to_state.state == 'on'}}"
  sensor:
    - name: LK-Pushbutton-Pressed
      state: "{{ trigger.id }},{{now().strftime('%H:%M:%S')}}"
      attributes:
        trigger-id: "{{ trigger.id }}"
        lk-key-map: >
          {% set mapper =
            {
              'Input_00K':['switch.toggle','switch.ihc1_output_00'],
              'Input_00L':['switch.turn_off','switch.ihc1_output_01'],
              'Input_08K':['switch.turn_on','switch.ihc1_output_08'],
              'Input_08L':['switch.turn_off','switch.ihc1_output_08'],
              'Input_16K':['switch.turn_on','switch.highnrswitch'],
              'Input_16L':['switch.turn_off','switch.highnrswitch'],
              'Input_24K':['switch.toggle','switch.ihc1_output_24'],
              'Input_24L':['switch.turn_off','switch.ihc1_output_24']
            }
          %} 
          {% set map = mapper[trigger.id] if trigger.id in mapper %}
          {{ map[0],map[1] }}

Can you clarify what “problem” you are having?

Unlike OP, you are creating a trigger-based Template sensor. That difference allows you to set widely available variables at multiple places… but I don’t see anywhere that would make a big difference in your example configuration.

I have 80 push buttons in an old LK IHC low voltage lighting control system that I have connected to Home Assistant via an esp32
I would like to be able to distinguish between long and short presses
I would like to define in an easy way what should happen when a button is pressed.
As you can see in my code it returns an attr with 2 values, I need more, I would like to have these values ​​returned in each attr.
I know I can set up 160 lines for each attribute, but I don’t think that’s smart. And it will be difficult to maintain.

you can see my project on my github page: GitHub - sekt1953/IHC-Replacement: I am making a replacmenr for my IHC system

my idea is that folders & map can be stored in a variable, which i could then use to define the contents of attributes.
but i can’t figure out the correct syntax

I am not sure if the following information will help with your project but a Trigger-based Template Sensor can have a variables section.

Any variables you define in that section can be referenced in the Sensor’s state and its attributes.

Example

The following (contrived) example defines four variables in the variables section. The variables are then referenced in state and in the color, and level attributes.

- trigger:
    - trigger: state
      entity_id: sensor.demo
  variables:
    my_map:
      10: blue
      20: green
      30: yellow
      40: orange
      50: red
      60: purple
    nmbr: "{{ trigger.to_state.state | int(0) }}"
    rounded_nmbr: "{{ (nmbr / 10) | round(0) * 10 }}"
    yes_or_no: "{{ iif(nmbr > 0, 'Yes', 'No') }}"
  sensor:
    - name: Example
      state: "{{ yes_or_no }}"
      attributes:
        color: "{{ my_map.get(rounded_nmbr, 'unknown') }}"
        level: "{{ nmbr }}"

In addition to Taras’ response above, you can also set variables that are specific to each trigger if you prefer to organize it that way:

- trigger:
    - id: Input_00K
      trigger: state
      entity_id:
        - binary_sensor.ihc1_input_00
      to: "off"
      from: "on"
      variables:
        action_id: switch.toggle
        target_ent: switch.ihc1_output_00
    - id: Input_00L
      trigger: state
      entity_id:
        - binary_sensor.ihc1_input_00
      to: "on"
      from: "off"
      variables:
        action_id: switch.turn_off
        target_ent: switch.ihc1_output_01
...

  sensor:
    - name: LK-Pushbutton-Pressed
      state: "{{ trigger.id }},{{now().strftime('%H:%M:%S')}}"
      attributes:
        trigger-id: "{{ trigger.id }}"
        lk-key-map: "{{ [action_id, target_ent] }}"

This looks exactly like what I need, I’ll try it out later today when I get home.

I will think about this solution.