How to Create a Toggle Button for AC and Heat

Hey everyone! I’m new to the world of smart home automation and I’m really excited to dive in. I’ve been working on setting up my smart home with some basic automation, and I came across this challenge that I wanted to share and hopefully get some feedback on.

So, here’s the deal: I have three scripts set up for controlling my HVAC system. The first one turns on the AC and sets the temperature, the second one turns on the heat and sets the temperature, and the third one turns the unit off completely. I wanted to create two toggle buttons—one for the AC and one for the heat. The idea is that when I press the button, it turns on the unit using the respective script, and when I press it again, it turns the unit off. It sounds pretty straightforward, but I’m running into some issues getting it to work seamlessly.

Let me walk you through what I’ve tried so far. I’ve been using the Home Assistant UI to create these buttons, and I’ve set up the scripts in my configuration.yaml file. The buttons themselves are showing up in the UI, but when I press them, they don’t toggle as I expected. Instead, they just run the script once and that’s it. I’ve read through some documentation and forums, and I think I need to use some sort of state tracking to make the toggle functionality work.

I came across a solution where you can use an input_boolean to track the state of the HVAC system. So, when the button is pressed, it checks the state of the input_boolean. If it’s off, it turns the AC or heat on and sets the input_boolean to on. If it’s already on, it turns the unit off and sets the input_boolean back to off. This seems like a solid approach, but I’m having trouble implementing it correctly.

Here’s what my current setup looks like:

yaml
input_boolean:
ac_mode:
name: AC Mode
initial: false
heat_mode:
name: Heat Mode
initial: false

script:
turn_on_ac:
sequence:
- service: climate.set_temperature
data:
entity_id: climate.my_hvac
temperature: 72
- service: climate.set_hvac_mode
data:
entity_id: climate.my_hvac
hvac_mode: cool
- service: input_boolean.turn_on
data:
entity_id: input_boolean.ac_mode

turn_on_heat:
sequence:
- service: climate.set_temperature
data:
entity_id: climate.my_hvac
temperature: 72
- service: climate.set_hvac_mode
data:
entity_id: climate.my_hvac
hvac_mode: heat
- service: input_boolean.turn_on
data:
entity_id: input_boolean.heat_mode

turn_off_hvac:
sequence:
- service: climate.set_hvac_mode
data:
entity_id: climate.my_hvac
hvac_mode: off
- service: input_boolean.turn_off
data:
entity_id: input_boolean.ac_mode, input_boolean.heat_mode

button:
ac_toggle:
name: AC Toggle
icon:mdi:air-conditioner
tap_action:
action: call-service
service: input_boolean.toggle
entity_id: input_boolean.ac_mode

heat_toggle:
name: Heat Toggle
icon:mdi:radiator
tap_action:
action: call-service
service: input_boolean.toggle
entity_id: input_boolean.heat_mode

This setup creates two toggle buttons in the UI, and when you press them, they toggle the state of the input_booleans. The scripts then check the state of these input_booleans and either turn on the AC or heat or turn everything off. It’s working, but I feel like there might be a more efficient way to do this.

I also read about using templates and conditions in the scripts to make the toggle functionality more dynamic. For example, you could have a single script that checks the current state of the HVAC system and decides whether to turn it on or off. But I’m not entirely sure how to implement that yet.

Another thing I’m considering is using a scene instead of individual scripts. Scenes in Home Assistant allow you to group together multiple services and actions, which might make the setup cleaner and easier to manage. I’m not entirely sure how to transition from scripts to scenes, though, so I might need to do some more research on that.

I also came across some custom integrations and automations that could potentially simplify this process. For example, there’s an integration called climate_control that allows you to set up more advanced HVAC controls, including toggle functionality. I’m not sure if it’s compatible with my setup, though, so I might need to do some testing.

In the meantime, I’m continuing to tweak my current setup to see if I can make it more efficient. I’m also reaching out to some more experienced community members to get their take on the best way to implement this toggle functionality.

Overall, I’m really enjoying the process of learning about smart home automation. It’s a bit challenging at times, but it’s also incredibly rewarding to see these systems come together and make my home smarter and more efficient. I can’t wait to see what other projects I can tackle next!

If anyone has any tips or suggestions on how to improve my current setup, I’d love to hear them. Happy automating everyone!