How to template a simple button with a "confirmation" value

Hey,
i’m absolutely new to Home Assistant and I’m actually working on a small Idea.
Unfortunately I’m unable to get it to work.

The following is what I want:

I Have a switch in lovelace called “testswitch” and I simply want that this switch toggles if there is a confirmation when clicking a button in lovelace or not…
Relatively simple I thought… but it isn’t for me… I just googled for hours asked friends with more experience but no one was able to help til now…
Maybe you can explain me what I’m doing wrong.

I just Broke down the Button to the bare minimum. The code looks like this:

type: button
tap_action:
  confirmation: "{% if is_state('input_boolean.testswitch', 'on') %} true {% else %} false {% endif %}"
  action: call-service
  service: script.1642074332166
icon: mdi:play

I’m looking absolutely forward for your answers. Thank you very very much in advance.

Regards,
Sebastian

you can’t template in lovelace

Ufff… okay… weird. But it does work with type: Markdown, doesn’t it?

easiest example:

type: markdown
content: |2
    {% if is_state('input_boolean.testswitch', 'on') %} true {% else %} false {% endif %}

this gives me exactly what I#m expecting “True” or “False”

Yes, you can tell what can be templated by looking at the docs

Example how its normally done:

Example from lovelace, markdown card

confirmation in tap action

1 Like

Welcome to the forums!

You may like to take a look at custom:button-card or card-mod

card-mod can only template styles, it won’t work in this situation. Custom:button-card will, with a different configuration

type: custom:button-card
tap_action:
  confirmation: >
    [[[
        return states['input_boolean.testswitch'] === 'on'; 
    ]]]
  action: call-service
  service: script.1642074332166
icon: mdi:play

Okay,
at first: Thanks for your answers!
I will have to try custom:button-card tomorrow. For today I’ve tried long enough.

Maybe I (try to) shortly explain what I’m trying to achieve:

In my home we have a Roborock Vacuum. I’m already controling it via HA with his basic features like Play Pause and Stop.
For each service I use a “Button”.
Sometimes it happened that I started it and forgot to clean the Mop or the Dust bin.

To prevent this in future I count the Cycles the “Start” Button has been pressed and count upwards.

If the Number is higher than 1 (so it cleaned the appartment already one time) I want a Notification in Lovelace, asking me if I really want to run the vacuum because it hasn’t cleaned yet(input sensor mop attached was never false during the last mop).
If I continue anyway it’s supposed to start and add another count in the cycles (in this case 2)

If you try now, after the 2nd time without cleaning, to start the vacuum it’s thought to give an “error” or something and it doesn’t call the service to start.

I Hope it’s understandable… even if it’s not absolutely necessary to explain it may interests someone…

So, as far as I know I am unable to bring up an Popup message on the device I pressed the “Start” Button. I only thought the function “Confirmation” could help here, but unfortunately it doesn’t.

Maybe anyone else has an good idea how to solve it? I Also thought about an entity filter card which shows only the corresponding button (with or without textmessage) regarding on the state, but I don’t have any idea yet how.

I want to give an Update:

I made it, my Button works now as planned. I am sure there are many (better) ways to solve this but for whats intended in my case it works.

Screenshot:

Code:

type: custom:button-card
tap_action:
  confirmation:
    text: |
      [[[
        if (states["counter.roborock_laufe_seit_reinigung"].state == '1') 
          return "Sicher? Roborock wurde seit dem letzten Lauf noch nicht gereinigt.";
        if (states["counter.roborock_laufe_seit_reinigung"].state == '0') 
          return "Starten? Roborock ist sauber und bereit.";
        if (states["counter.roborock_laufe_seit_reinigung"].state > '1')
          return  "Abbruch! Roborock wurde ewig nichtmehr gereinigt, Bitte Reinigen!";
      ]]]
  action: call-service
  service: script.1642074332166
icon: mdi:play
color: null
name: Start
aspect_ratio: 4/4

Thanks for the support so far!

two further questions:

Any Idea how to bring the Icon in the right color? I don’t know how the standard homeassistant color is named so i don’t know what to enter in the icon_color field.

Any Idea how to Align the text “Start” with the other “Pause” or “Stop” text?

Cheers,
Sebastian

1 Like