Monday, November 11, 2024

Home Assistant button with automation on your dashboard and state not always on

If you want to add a button to your dashboard that will trigger an automation once clicked, but you don't like the fact that by default the state will be always on, you can add an additional input_boolean. Below are the steps how to do this.

To define an input_boolean in Home Assistant, you can add it to your configuration.yaml file. Here’s how to do it:

  1. Open your configuration.yaml file in Home Assistant.

  2. Add the Input Boolean definition under the input_boolean section. If the section doesn’t exist yet, you can create it.

    yaml
    input_boolean: rain_water_pump_button: name: Rain Water Pump Button initial: off
    • rain_water_pump_button: This is the entity ID for your input boolean.
    • name: This is the name that will appear in the UI.
    • initial: Setting this to off means the button will be off each time Home Assistant restarts.
  3. Save the Configuration and Restart Home Assistant:

    • After saving your changes, go to Settings > System > Restart to apply the new configuration.
  4. Create a Combined Automation: This automation will:

    • Turn the rain_water_pump_button “on” for 2 seconds to confirm the button press.
    • Trigger switch.rain_water_pump for 3 minutes, then turn it off.
    yaml
    alias: Enable Rain Water Pump for 3 Minutes trigger: - platform: state entity_id: input_boolean.rain_water_pump_button to: 'on' action: - delay: "00:00:02" # Keep button "on" for 2 seconds - service: input_boolean.turn_off target: entity_id: input_boolean.rain_water_pump_button - service: switch.turn_on target: entity_id: switch.rain_water_pump - delay: "00:03:00" # 3 minutes delay for the pump - service: switch.turn_off target: entity_id: switch.rain_water_pump mode: single
  5. Update the Dashboard Button: Here’s the updated configuration for the dashboard button to trigger the input_boolean instead of the automation directly. This way, pressing the button will activate the whole process:

    yaml
    type: entity-button entity: input_boolean.rain_water_pump_button icon: mdi:av-timer show_name: true show_icon: true show_state: true icon_height: 25px tap_action: action: toggle
  6. With this setup:

    • The dashboard button will change to "on" for 2 seconds when pressed.
    • The input_boolean state change triggers the switch.rain_water_pump to turn on for 3 minutes and then turn off automatically.

No comments:

Post a Comment