Saving value of light atributte into input_number

I have Ikea lightbulb and Tuya sensor for doors/windows. My goal is for the brightness to decrease when the window is opened and when its closed again the brightness should be set to the value before the window was open. Right now I can setup the brightness when opening window.
I tried using input_number for storing the brightness, but it doesn’t seems to work.
Here is my code:

alias: Bugs Prevention
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/Dvere
condition:
  - condition: device
    type: is_on
    device_id: b6b40c6172817a483ee9d791c5bee191
    entity_id: be3733e14d0361d3aa2ade9ce69fb5f3
    domain: light
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.contact }}"
        sequence:
          - service: input_number.set_value
            data:
              value: "{{ state_attr('light.zarovicka', 'brightness') |int }}"
            target: 
              entity_id: input_number.previous_brightness
          - service: mqtt.publish
            data:
              retain: false
              qos: "1"
              payload_template: "{\"brightness\":{{ states('input_number.previous_brightness') | int }}}"
              topic: zigbee2mqtt/Zarovicka/set
      - conditions:
          - condition: template
            value_template: "{{ not trigger.payload_json.contact }}"
        sequence:
          - service: mqtt.publish
            data:
              retain: false
              qos: "1"
              payload: "{\"brightness\": 10}"
              topic: zigbee2mqtt/Zarovicka/set
mode: single

Just to be sure here is also the payload for opening the window:

MQTT publish: topic 'zigbee2mqtt/Dvere', payload '{"battery":100,"battery_low":false,"contact":false,"linkquality":94,"tamper":false,"voltage":3000}'
MQTT publish: topic 'zigbee2mqtt/Zarovicka', payload '{"brightness":10,"color":{"h":222,"hue":222,"s":0,"saturation":0,"x":0.3227,"y":0.329},"color_mode":"xy","color_options":{"execute_if_off":false},"color_temp":167,"color_temp_startup":370,"linkquality":94,"power_on_behavior":"previous","state":"ON","update":{"installed_version":587753009,"latest_version":587806257,"state":"available"},"update_available":null}'

And for closing the window:

MQTT publish: topic 'zigbee2mqtt/Dvere', payload '{"battery":100,"battery_low":false,"contact":true,"linkquality":97,"tamper":false,"voltage":3000}'

Hopefuly someone here can help