Climate toggle not restoring last climate mode?

I’m calling climate.toggle on my mini split and it always turns on to heat regardless of what the previous mode was (ie cooling or fan_only). Is there a way to make it turn on to the previous mode with a simple toggle button?

        tap_action:
          action: call-service
          service: climate.toggle
          target:
            entity_id: climate.hvac_master_bedroom

It is probably a limitation in the integration, so you might have to make a script to save and restore the values when pressing the toggle button.

Bummer, any recommendations on best way to do this?

Make a script for toggling off that reads the state and then store it in a helper and then turns off the climate device.
Make another script for toggling on that reads the helper and then turn on the climate device and make a service call to set the mode that was retrieved from the helper.
You can then make a template switch and then call that in the tap_action.

You could create a trigger-based template sensor to store the previous state of the climate entity. A trigger-based template sensor essentially is a combination of an automation and a helper, with the added benefit that the helper is not editable from the UI.

Add this to your configuration.yaml:

template:
  - trigger:
      - platform: state
        entity_id: climate.my_thermostat_entity
        not_from:
          - unavailable
          - unknown
    sensor:
      - name: Thermostat Previous Mode
        unique_id: 386e4e76-fc43-4692-a6d5-a2df2545da0g
        state: "{{ trigger.from_state.state }}"

If that is your first template entity in YAML then you’ll need to reboot HA for it to take effect. If not, make sure to not duplicate the template: heading, and then in developer tools → YAML you can click the button to reload template entities.

Then you can create a script that sets the HVAC based on the state of that new template entity:

alias: Toggle HVAC
sequence:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "{{ states('sensor.thermostat_previous_mode') }}"
    target:
      entity_id: climate.my_thermostat_entity

Any time you run that script it will toggle your HVAC. If you want a button on your dashboard, just have the button call the script.

Would it not be better to use not_to: ??
The current setup would not store the state if the previous state is unavailable or unknown, even though it is a state on the actual climate device. It might store unavailable and unknown though, because it might be coming from a state on the actual climate device.

No it’s specified correctly:

  • coolunavailable: set state to cool
  • unavailableheat: no change, keep as cool

I never want this sensor to store either unavailable or unknown, and it always stores the state the climate entity comes from, so I never want to trigger when the climate entity comes from unavailable or unknown.

Note that this is the previous state of the climate entity: trigger.from_state.state

Thanks for pointing out that last line. As you guessed I had misread it. :smiley:

Why toggling does not have the behaviour of manual “on/off” device?
Is there no way either to set this up in ESPHome yaml file either?

Thank you,

It depends on the hardware and the integration you use. Some do have that behavior. Other climate entities don’t support being turned on & off. Some support being turned off, but don’t support being turned on without specifying the mode. For example on my Nest thermostat, when I physically interact with it, I can select modes of “Off, Heat, Cool, Heat & Cool”. There is no “on” state that I can choose. When I interact with it programmatically via HA, the same behavior applies: I have to choose the mode; I can’t simply tell it to turn “on”. However the integration I use will remember the state it was in prior to being off, so the toggle command will restore that previous state.

You can do pretty much anything in ESPHome. Best to start a new post in that category.

1 Like

Thanks @mekaneck for the suggestion, this works amazingly well.

The syntax has changed slightly in the current version of HA.
In configuration.yaml:

template:
  - trigger:
      - trigger: state
        entity_id: climate.my_thermostat_entity
        not_from:
          - unavailable
          - unknown
    sensor:
      - name: Thermostat Previous Mode
        unique_id: 386e4e76-fc43-4692-a6d5-a2df2545da0g
        state: "{{ trigger.from_state.state }}"

In scripts.yaml:

  toggle_hvac:
    alias: Toggle HVAC
    sequence:
      - action: climate.set_hvac_mode
        data:
          hvac_mode: "{{ states('sensor.thermostat_previous_mode') if states('climate.my_thermostat_entity') == 'off' else 'off' }}"
        target:
          entity_id: climate.my_thermostat_entity

I have added a condition in the script so that if you change HVAC mode, toggling will turn off instead of going back to the previous mode.

I went a slightly different path:
At the core, what’s driving the climate state shouldn’t necessarily be it’s previous state but rather the temperature. So I just made an automation that runs a basic if then on the internal temp of the room to decide what you likely want to do. The only downside I see for this is when I want to only use the fan mode instead of heat or cool. I’ll have to deal with that when it gets a bit warmer.