Saving Previous Numeric State of a Sensor

Hi all,

I would like to make some automations that will turn down my heat when the window is open and then turn it back up again once it is closed again (and maybe the opposite for aircon in the summer at some point). This worked fine when I have the temp always set to the schedule programmed into the thermostat, but now I’d like to take more control of the temperature and it could be any number of values when the automation fires, which means I have the new problem of getting the thermostat back to the temperature it was set to before the window was open.

So, I need to save the previous temperature somewhere so that after the window is closed again, I can set the temperature back to where I had it before. I’ve done something similar with my location to create vectors of which direction I was heading with an automation as follows which works fine using dropdowns:

- id: zone_to_dropdown
  alias: 'Zone to Dropdown'
  hide_entity: true
  initial_state: 'true'
  trigger:
    platform: state
    entity_id: device_tracker.seans_iphone_x
  action:
    service: input_select.select_option
    entity_id: input_select.sean_last_zone_dropdown
    data_template:
      option: '{{ states.device_tracker.seans_iphone_x.state }}'

But this isn’t a great option for a numeric state. I wanted to try to use the custom variable component as it appears to be perfect for this, so I tried this, but it never sets itself:

- id: living_room_last_temp
  alias: 'Living Room Last Temp'
  trigger:
    platform: state
    entity_id: climate.living_room
  action:
    service: variable.set_variable
    data:
      variable: living_room_last_temp_variable
      value_template: "{{ states('sensor.climate.living_room')}}"
      attributes_template: >
        {
          "temperature": "{{ state_attr('climate.living_room', 'temperature') }}",
        }   

I’ve tried a lot of different attempts with different syntax, but I just can’t get it to set… Does anyone have any bright ideas here?