Easiest way to trigger an automation with one tap from lovelace

Just to make things a bit easier for the other members of my household, I want to have a simple lovelace card with 4 buttons that will run my standard daily routines “good morning, good night, good bye, and welcome home”. We dont need to use these often, but once in a while (especially the goodnight) routine, using the UI is easier/quieter. I know I could create input booleans, etc. But is there just a simple way without creating new entiries, scripts, etc to just trigger an automation from lovelace, peferably as a button that I can assign an icon to?

1 Like

Edit, answering my own question

    tap_action:
      action: call-service
      service: automation.trigger
      service_data:
        entity_id: automation.goodnight_routine

worked well in a glance card

You may already know this but for those who don’t, when you trigger an automation using the automation.trigger service, it will ignore the automation’s condition section and simply execute the action.

So if we have this (contrived) automation:

- alias: 'Test automation'
  trigger:
    entity_id: light.hallway
    platform: state
    from: 'off'
    to: 'on'
    for: '00:10:00'
  condition:
    condition: time
    after: '17:30:00'
    before: '21:31:00'
  action:
    - service: light.toggle
      data:
        entity_id: light.kitchen

Executing it with automation.trigger will skip the trigger and condition sections and simply execute the action (i.e. toggle the kitchen light’s state).

So anyone who wants the condition section to always be evaluated, it won’t happen if you use automation.trigger.

If you want that behaviour, you could just move the condition down to the first element of the ‘action’ sequence. The automation will still ‘run’, but it’ll quit after evaluating the condition as the first step.

Very true but based on the many examples of automations posted in the documentation, and this forum, very few incorporate that technique … so most people will learn about this ‘gotcha’ the hard way.

Nevertheless, you’re 100% correct, there is a way to mitigate it, and it requires a minor re-write of the automation.

something like this?

  action:
    - service_template: >
        {% if (is_state('device_tracker.iphone_jimmy', 'home')) and (is_state('device_tracker.iphone_van_steph', 'home'))  %}
          notify.ios_notifier
        {% elif is_state('device_tracker.iphone_jimmy', 'home') %}
          notify.ios_iphone_jimmy
        {% elif is_state('device_tracker.iphone_van_steph', 'home') %}
          notify.ios_iphone_van_steph
        {% endif %}
      data:
        message: 'De Wasmachine heeft zijn programma voltooid en kan leeggemaakt worden!'
        title: 'Wasmachine'
        data:
          push:
            badge: 0
            category: "wasmachine_alert" 

I left out the conditions (as the template would pretty much almost do the same). Works like a charm.

No, not like that. Your example will always execute the action.

condition can be specified outside of action (the way most examples show it) or inside action. Your example does not contain a condition inside the action.

Here’s an example taken from the documentation. If the condition is not met then the final part of the automation, service: scene.turn_on, will not be executed.

- alias: 'Enciende Despacho'
  trigger:
    platform: state
    entity_id: sensor.mini_despacho
    to: 'ON'
  action:
    - service: notify.notify
      data:
        message: Testing conditional actions
    - condition: or
      conditions:
        - condition: template
          value_template: '{{ states.sun.sun.attributes.elevation < 4 }}'
        - condition: template
          value_template: '{{ states.sensor.sensorluz_7_0.state < 10 }}'
    - service: scene.turn_on
      entity_id: scene.DespiertaDespacho

If I apply the same technique to the example I posted earlier, it would look like this:

- alias: 'Test automation'
  trigger:
    entity_id: light.hallway
    platform: state
    from: 'off'
    to: 'on'
    for: '00:10:00'
  action:
    - condition: time
      after: '17:30:00'
      before: '21:31:00'
    - service: light.toggle
      data:
        entity_id: light.kitchen

If I use automation.trigger to execute this revised automation, it will evaluate the condition , and it will have to evaluate to true, before it can execute light.toggle.

using conditions in the action part is a fine technique and much used on this side. Especially useful for testing automations (and trigger them manually) and calling them directly by scripts and other HA techniques. In fact one can use that for all situations that aren’t triggered by the trigger, but use the same condition and action…

the condition would have to be written like this though:

 action:
    - condition: time
      after: '17:30:00'
      before: '21:31:00'
    - service: light.toggle
      data:
        entity_id: light.kitchen

or even:

 action:
    - condition: time
      after: '17:30:00'
      before: '21:31:00'
    - service: light.toggle
      entity_id: light.kitchen

yep, just a copy-paste error. fixed it.

Lot’s of good back and forth on this subject. Yes, you were spot on for my scenario. I wanted to ignore the trigger and the conditions. My need was to run the goodnight routine (and maybe sometimes other routines) when they did not run as expected or just wanted to run at a different time. I actually use conditions in the action section of my routines quite a bit, can be very helpful and reduce the need for extra automatons.

For me a simple one is in my goodbye routine, turns off most lights, then checks a condition and if the sun is still up, turn off any outdoor lights too. Most of the time the outdoor lights would not be on if the sun is up, but this is a catch all just in case.

Sorry for the thread bump but google sends me here every time I search for a lovelace button triggering an automation.

The snippet from ptdalen pointed me in the right direction.
I was able to compose it and thought I should post the result here for a more complete example for others to work from:

My target was to have a thermostat control, with 2 buttons beneath it to automatically turn-on and off the heating to a predefined point. (Done in an automation, normally triggered by a movement sensor)

type: vertical-stack
cards:
  - type: thermostat
    entity: climate.radiator_livingroom_climate
    theme: default
  - type: glance
    entities:
      - entity: climate.radiator_livingroom_climate
        name: Heating on
        show_state: false
        tap_action:
          action: call-service
          service: automation.trigger
          service_data:
            entity_id: automation.heating_livingroom_enable_heating_on_movement
      - entity: climate.radiator_livingroom_climate
        name: Heating off
        show_state: false
        tap_action:
          action: call-service
          service: automation.trigger
          service_data:
            entity_id: automation.heating_livingroom_disable_heating_when_empty

Maybe I’m missing something, but isn’t it easier to just write this as a SCRIPT instead of an automation? I have numerous instances like this. Here’s one for my home alarm when the icon is clicked

ui-lovelace.yaml:

          - entity: sensor.alarm_dot_com_sensor_home
            tap_action:
              action: call-service
              service: script.turn_on
              service_data:
                entity_id: script.set_home_alarm_click_ui

scripts.yaml

set_home_alarm_click_ui:
  sequence:
  - condition: state
    entity_id: alarm_control_panel.home
    state: 'disarmed'
  - service: script.turn_on
    data_template:
      entity_id: >-
        {% if is_state('sensor.google_maps_wife_and_me_ishome', 'home') %}
          script.set_home_alarm_stay
        {% else %}
          script.set_home_alarm_away
        {% endif %}
  - delay: 00:00:05
  - service: homeassistant.update_entity
    entity_id: alarm_control_panel.home

set_home_alarm_stay:
  sequence:
  - service: alarm_control_panel.alarm_arm_home
    entity_id: alarm_control_panel.home

set_home_alarm_away:
  sequence:
  - service: alarm_control_panel.alarm_arm_away
    entity_id: alarm_control_panel.home

sensors.yaml:

    alarm_dot_com_sensor_home:
      friendly_name: "Alarm.com Home Sensor"
      value_template: >-
        {% if is_state('alarm_control_panel.home', 'armed_away') %}
          away
        {% elif is_state('alarm_control_panel.home', 'armed_home') %}
          stay
        {% else %}
          disarmed
        {% endif %}
      entity_picture_template: >-
        {% if is_state('alarm_control_panel.home', 'armed_away') %}
          /local/custom_icons/shield-home-away.png
        {% elif is_state('alarm_control_panel.home', 'armed_home') %}
          /local/custom_icons/shield-home-yellow-FDD835.png
        {% else %}
          /local/custom_icons/shield-home-blue-44739E.png
        {% endif %}

Added the lovelace and sensors for grins. Changed the color of the icon depend on home/away and such. Anyhow, just seems like automations should be just that - AUTOmatic, and instances like this scripts are great - even if you have to bury a condition in the script or nest a script or two. Anyways, just my $.02 and what works for me…

1 Like

Fully agree with you and wondering why no one suggested this before. When you still want to use the same “actions” in an automation, you just call the script from the automation.

Yes, I definitely have automations that call one or more scripts (an evening routine that runs at a certain time every night, conditioned on if me/the wife are home, which then calls the above set alarm script :slight_smile: )

There are almost ALWAYS multiple ways to ‘skin a cat’ in HA, and really just up to each individual user how best works for THEM. I try never (where possible) to dictate / mandate to anyone how things ‘should’ be done - but rather provide optionality and let anyone pick what works best for them.

1 Like

You know I always struggle as to when to use scripts vs automation. I will say that I have a lot of automations, 3000-4000 lines worth, and only a handful of scripts( a few hundred lines). I generally understand that if I have an action that I want to do often, that would probably be a good candidate for a script. Text to Speech is a good example. I had one for notifications a while back as well, that allowed me to pass variables, etc.

I feel like most of my automations are unique enough that using a script would not save any YAML. Maybe I’m wrong. Either way, I dont see it as bad to want to occasionally run an automation with a button. It’s not a common thing for me, but sometimes something did not work, and clicking a button takes care of it. Ultimatly if the button runs an automation or a script, or turns on an input boolean that triggers an automation or a script, what’s really the difference.

I’ll say that I probably have a lot of automations, because that’s how I got started with HA, they work, and the idea of going back to fix them seems a but combersum, haha.

1 Like

Just saw this in .104 docs FWIW:

1 Like