Template Switch - How to save state of switch based on if last turned on or off?

For some context, I am trying to turn my old mechanical thermostat into a smart thermostat using two SwitchBots. (I cannot replace the thermostat due to it being an apartment). The SwitchBots work great and my idea is to use one to pull up on the lever to turn on the thermostat and use the other SwitchBot to pull down on the level to turn it off. Essentially these two switches would act as one virtual switch.

I found the Generic Thermostat integration and noticed it requires a switch, so I have been looking into how to make this virtual switch work. The answer seems to be to use a Template Switch, but it is unclear to me how to set up the value_template part of it. Ideally the switch would just remember if it was last turned off or on and return that as the switch value. How should I go about setting up this yaml?

Here is what I have so far:

switch:
  - platform: template
    switches:
      thermostat:
        value_template: <<<what to put here?>>>>
        turn_on:
          - service: switch.turn_on
            target:
              entity_id: switch.switch.thermostat_top
          - delay:
              seconds: 3
          - service: switch.turn_off
            target:
              entity_id: switch.switch.thermostat_top
        turn_off:
          - service: switch.turn_on
            target:
              entity_id: switch.switch.thermostat_bottom
          - delay:
              seconds: 3
          - service: switch.turn_off
            target:
              entity_id: switch.switch.thermostat_bottom

Set up a trigger-based template binary sensor that triggers when either Switchbot switch turns “on”, then base your template switch’s state off that. Also, you had a lot of doubled switch. in your entity ids.

template:
  - trigger:
    - platform: state
      to: 'on'
      entity_id:
        - switch.thermostat_top
        - switch.thermostat_bottom
    binary_sensor:
      - name: Thermostat Switch
        state: >
          {{ trigger.entity_id == "switch.thermostat_top" }}

switch:
  - platform: template
    switches:
      thermostat:
        value_template:  "{{ states('binary_sensor.thermostat_switch') }}"
        turn_on:
          - service: switch.turn_on
            target:
              entity_id: switch.thermostat_top
          - delay:
              seconds: 3
          - service: switch.turn_off
            target:
              entity_id: switch.thermostat_top
        turn_off:
          - service: switch.turn_on
            target:
              entity_id: switch.thermostat_bottom
          - delay:
              seconds: 3
          - service: switch.turn_off
            target:
              entity_id: switch.thermostat_bottom
2 Likes

Thank you so much! This was perfect!

1 Like