Entity-button with service-template?

Hello.

I’m struggling with an Entity Button Card Configuration. I would like to do one thing if the light is already on, and another thing if it’s off. Kind of like toggle, except “turning on” and “turning off” are scripts. I’m trying to solve this with using templates, but I don’t know if I get it all wrong, or if templates aren’t supported in this case.

Here’s the entity button card configuration:

type: entity-button
tap_action:
  action: call-service
  service: |
    {% if is_state('light.color_light_1', 'on') %}
      script.1573422009426
    {% else %}
      script.1573422362904
    {% endif %}  
hold_action:
  action: call-service
  service: script.1573422080784
show_icon: true
show_name: true
entity: light.color_light_1
name: Nattbord
icon_height: 24px
theme: Backend-selected

The configuration editor keeps changing my ‘>’ for a ‘|’ after saving for some reason. Not quite sure what either of those really mean.

Edit: When clicking it, I get the error Failed to call service {% if is_state( ... ) %} script. Service not found..

Any suggestions?

I don’t think templating is supported at this level but obviously you could push the test into the script and then branch to two other scripts depending on the results. The change from > to | it’s not a big deal, minor difference in New line and space handling. If your interested Google YAML > or | and read all about it.

Templating is only allowed in template fields. IIRC, only markdown card has templating in lovelace, everything else for lovelace does not unless you have a custom card. This may change in the future.

You’ll need to make that a service_template script.

Thanks. I did make a toggle script with a service_template, and I could call that from the entity button. Works like a charm. :slight_smile: