Turning thermostat on and off while honoring the last known mode

I have a honeywell wifi thermostat (total connect comfort). I want an automation to turn off the thermostat when a door/window is opened for more than 2 minutes and turn it back on when the door/window is closed for more than 2 minutes.

The only part I am struggling with is when i use service climate.turn_on it sets the mode to ‘heat’ even if it was on cool when climate.turn_off was executed.

I am trying to avoid using variables to store the HVAC mode - but I think that may be the only way.

Appreciate any input you have.

thanks,
monkers

- id: '0000000321'
  alias: Door or Window Closed
  trigger:
  - entity_id: group.doorwindow_sensors
    platform: state
    to: 'off'
    for:
      minutes: 2
  condition:
  - condition: state
    entity_id: input_boolean.override_hvac
    state: 'off'
  - condition: numeric_state
    entity_id: sensor.uptime
    above: 3
  action:
  - service: climate.turn_on
    data:
      entity_id: climate.thermostat

You can do it with AppDaemon, @packetinspector shared this plugin for app daemon for state restore:

https://github.com/packetinspector/HASS/tree/master/StateRestore