How to create a toggle button for two scripts

I have three scripts.

  1. Turns on AC to cool, sets temp
  2. Turns on Heat, sets temp
  3. Turns off the Unit.

i want to create two toggle buttons.
One for the AC, the other for the Heat.

When you hit the button, it turns on the unit (using one of the scripts).
When you hit the button again, it turns off the unit (either using the script or just turning it off).

Same for the heater part.

Anyone have any idea if this is possible?

let me add some more info.

here is turning the AC on

alias: Garage AC
sequence:
  - service: climate.set_temperature
    data:
      temperature: 71.7
      hvac_mode: cool
    target:
      device_id: 53ae43ac8c9f554dea20fd2295ba20d8
mode: single

Heat

alias: Garage Heater
sequence:
  - service: climate.set_temperature
    data:
      temperature: 80
      hvac_mode: heat
    target:
      device_id: 53ae43ac8c9f554dea20fd2295ba20d8
mode: single

off script

alias: Garage Air Conditioner Off
sequence:
  - device_id: 53ae43ac8c9f554dea20fd2295ba20d8
    domain: climate
    entity_id: d23bf708efc48b2f905a0a02d381faae
    type: set_hvac_mode
    hvac_mode: "off"
mode: single

Entity of hte actual garage air conditioner (no clue why it looks like this)

climate.40681930235404_climate

Are you planning on using input button helpers or just buttons on a dashboard card? Both are workable, but the method needed depends on the choice of input.

ah sorry, yes, this is just a button on the dashboard.

A basic button card allows you to assign actions for tap. You can use those actions to call a script. To get the toggle action you have described, you will need a script that handles the logic of which button has been pressed and compare it to the current mode of your device. Your buttons will both call that script.

  - type: button
    tap_action:
      action: call-service
      service: script.choose_heat_or_ac
      data:
        mode: cool
        temp: 71.7
    name: Cool
    icon: mdi:snowflake

You’ll need a similar one for Heat.

  - type: button
    tap_action:
      action: call-service
      service: script.choose_heat_or_ac
      data:
        mode: heat
        temp: 80
    name: Heat
    icon: mdi:fire

You can actually handle all the logic and actions in the script being called instead of using three separate scripts, but do yourself a favor and stop using devices… use entities instead.

alias: Choose Heat or AC
fields:
  mode:
    name: Mode
    example: "cool"
  temp:
    name: Temperature
    example: 72.0
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode != states('climate.YOUR_ENTITY') }}"
        sequence:
          - service: climate.set_temperature
            data:
              temperature: "{{ temp }}"
              hvac_mode: "{{ mode }}"
            target:
              entity_id: climate.YOUR_ENTITY
    default:
      - service: climate.turn_off
        data: {}
        target:
          entity_id: climate.YOUR_ENTITY
mode: single
1 Like

What about a template?

I tried to go this route as it seemed the easiest.
But the switch never shows up?

From my config file:

template:
   - switches:
        GarageACOnOff:
        friendly_name: "Garage AC On/Off"
        value_template: "{{ hvac_mode('53ae43ac8c9f554dea20fd2295ba20d8', 'cool') }}"
        turn_on:
            - service: script.Garage_AC
        turn_off:
            - service: script.Garage_Air_Conditioner_Off

I have tried it with that deviceID and tried it with climate.40681930235404_climate
I never see the switch come up.

Fixed my config based on the template:

switch:
  - plateform: template
    switches:
     GarageACOnOff:
        friendly_name: "Garage AC On/Off"
        value_template: "{{ hvac_mode('switch.GarageACOnOff_toggle', 'cool') }}"
        turn_on:
            - service: script.Garage_AC
        turn_off:
            - service: script.Garage_Air_Conditioner_Off

Error i am getting:

The system cannot restart because the configuration is not valid: Invalid config for [switch]: required key not provided @ data[‘platform’]. Got None. (See /config/configuration.yaml, line 79).

So I created a new script and tried it with your example.
The script saves.
Created a button. When I press the button, it turns on. When I press it again, nothing happens, it stays on. My button also shows Off.
no errors.

Here is my script and button:

alias: GarageAC_OnOff
fields:
  mode:
    name: Mode
    example: cool
  temp:
    name: Temperature
    example: 72
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode != states('climate.40681930235404_climate') }}"
        sequence:
          - service: climate.set_temperature
            data:
              temperature: 72
              hvac_mode: cool
            target:
              entity_id: climate.40681930235404_climate
    default:
      - service: climate.turn_off
        data: {}
        target:
          entity_id: climate.40681930235404_climate
mode: single

I changed it up based on the OFF script that I have. Same behavior

alias: GarageAC_OnOff
fields:
  mode:
    name: Mode
    example: cool
  temp:
    name: Temperature
    example: 72
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode != states('climate.40681930235404_climate') }}"
        sequence:
          - service: climate.set_temperature
            data:
              temperature: 72
              hvac_mode: cool
            target:
              entity_id: climate.40681930235404_climate
    default:
      - device_id: 53ae43ac8c9f554dea20fd2295ba20d8
        domain: climate
        entity_id: d23bf708efc48b2f905a0a02d381faae
        type: set_hvac_mode
        hvac_mode: "off"
mode: single

BUTTON:

show_name: true
show_icon: true
type: button
tap_action:
  action: toggle
entity: script.1689247316109
show_state: true

If you want to use Template switches, you can but they must be configured properly or they will not be created.

The value_template gives the switch it’s state value, so it can’t reference itself…

switch:
  - platform: template
    switches:
      garage_ac_on_off:
        friendly_name: "Garage AC On/Off"
        value_template: "{{ is_state('climate.40681930235404_climate', 'cool') }}"
        turn_on:
            - service: script.garage_ac
        turn_off:
            - service: script.garage_air_conditioner_off

You are not using the action I said to use and, because of that, you’re not sending the mode data with your button action. You must use a call_service action. The condition in the script expects to receive a variable named “mode”, it you don’t supply that, the script will not work as intended.

You need to decide which type of input you want to use before proceeding… it makes it difficult to help when you are jumping back and forth between button cards and Template switches.

1 Like

Yep, you are right.
Ok, I am almost there.
I got it to turn on and off.

script:

alias: GarageAC_OnOff
fields:
  mode:
    name: Mode
    example: cool
  temp:
    name: Temperature
    example: 72
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode != states('climate.40681930235404_climate') }}"
        sequence:
          - service: climate.set_temperature
            data:
              temperature: "{{ temp }}"
              hvac_mode: "{{ mode }}"
            target:
              entity_id: climate.40681930235404_climate
    default:
      - service: climate.turn_off
        data: {}
        target:
          entity_id: climate.40681930235404_climate
mode: single

My issue is the button. I clicked on show state, but it does not show it, but the big thing is when you hit the button, it does the turn on/off, but when its on, I expected the icon to turn that amber color like my other buttons, but it stays blue, which makes it hard to see if it ran.

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: script.1689247316109
  data:
    mode: cool
    temp: 72
name: Cool
icon: mdi:snowflake
show_state: true

In a Button card, show_state is based on the entity ID you supply in the entity field. If you don’t supply an entity ID for it to monitor, then there will be no state shown. You can add the climate entity to the card configuration, but it won’t change the icon color the way you want… that’s just not how it works with climate entities. It will just show the state value under the name:

image

The combination of dashboard card and method you have chosen does not lend itself to what you have just described as your goal.

Options:

  1. Create a pair of Template binary sensors to use in as your entities in the standard Button cards you have already created.
  2. Use the Template switches discussed earlier.
  3. Use a different button card such as Mushroom Cards or the custom Button Card which have templating options to make them more flexible.
  4. Use a Thermostat card or Climate Mode Entity Row.

I am holding off on the color piece for now and since it is separate from my original if I need to look at this again will start a new thread.
Accepted your original solution thank you!

Hi Didgeridrew, “… but do yourself a favor and stop using devices… use entities instead”.
I’m facing the same problem, and i would like to create an entity but i don’t know how.
I have a Moes IR blaster which is ZHA functional using quirk. My purpose is to control an old air conditioner.
Now i can control it using scripts. How to create an entity for such device? Then I could use climate control.

alias: LG AC
fields:
  code_name:
    name: IR Code
    description: The code to be blasted through the IR blaster
    required: true
    selector:
      attribute:
        entity_id: sensor.ir_blaster_codes
sequence:
  - service: zha.issue_zigbee_cluster_command
    data:
      cluster_type: in
      endpoint_id: 1
      command: 2
      ieee: 90:39:5e:ff:fe:2f:4c:7b
      command_type: server
      params:
        code: "{{ state_attr('sensor.ir_blaster_codes', code_name) }}"
      cluster_id: 57348
mode: single
icon: mdi:remote

The basic parts of a climate entity are

  1. A way for Home Assistant to know the temperature
  2. A “switch”

Do you have a means of getting feedback from the AC unit?

You may be able to combine existing sensors with your script actions into a Template Switch then use that and a temperature sensor in the Generic Thermostat integration to create a usable climate entity. For the Template Switch you will need a way to know whether the AC is running.

Thank you for your time!
I can’t understand how to bind climate entity here.
Lets say i have external thermometer, and i will buy a smart socket to monitor power state of the AC. I don’t understand how to bind between thermostat temperature setpoint values, or modes? In order to change temperature i have to send a command with different code to the ir blaster.

No, you have to give over temperature control to HA.

1: Set up a Template Binary sensor. Use the value from the power monitoring sensor to define when the binary sensor is on or off. This will be used to provide the state for the Template switch.

2: Set up the Template Switch: The “turn on” actions in in the switch configuration will be to send the codes to turn the unit on/set the temp to slightly lower the lowest value you want it to ever use. The “turn off” actions send the codes to set the unit to idle or off.

3: Set up the Generic Thermostat

Now I understand your point. It is simple on off operation.
I thought there is a way to create an entity that will open the functionality of AC - change the mode, set timers etc…
Thank you.

The actions can be more complex, but you have to design the automations around the inputs and outputs available to you. Accurate and reliable automation without accurate or reliable sensing is difficult and often frustrating.

You haven’t shared what type of AC unit you are using, but you may want to check out the following threads to see if your AC unit is compatible with the Midea hack:

Compatible Models thread

Midea Hardware Hack thread

1 Like

You helped a lot, I will check the links.
Thank you!