Hello,
I’m trying to make a template button as described here: https://www.home-assistant.io/integrations/template/ with this code:
template:
- button:
- name: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
{{ Start }}
{% else %}
{{ Stop/Pause }}
{% endif %}
- icon: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
{{ mdi:play-circle-outline }}
{% else %}
{{ mdi:stop-circle-outline }}
{% endif %}
- press:
- service: input_boolean.toggle
target:
entity_id: input_boolean.start_stop_chrono
But I get this error:
Invalid config for [template]: required key not provided @ data['button'][0]['press']. Got None.
I haven’t found any example about how to create a template button.
Can anybody help me fix the code?
Thanks!
jrunning
(Jordan)
July 25, 2022, 9:07pm
2
You have name
, icon
, and press
as separate array elements under button
but I think they’re all supposed to be keys of the same item. Get rid of the extra dashes before icon
and press
:
template:
- button:
- name: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
{{ Start }}
{% else %}
{{ Stop/Pause }}
{% endif %}
icon: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
{{ mdi:play-circle-outline }}
{% else %}
{{ mdi:stop-circle-outline }}
{% endif %}
press:
- service: input_boolean.toggle
target:
entity_id: input_boolean.start_stop_chrono
1 Like
I tried your code but unfortunately I continue getting the same error.
calisro
(Rob)
July 25, 2022, 9:31pm
4
template:
- button:
- name: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
{{ Start }}
{% else %}
{{ Stop/Pause }}
{% endif %}
icon: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
{{ mdi:play-circle-outline }}
{% else %}
{{ mdi:stop-circle-outline }}
{% endif %}
press:
service: input_boolean.toggle
target:
entity_id: input_boolean.start_stop_chrono
I think its the dash before the service call…
1 Like
It continued giving error, but removing double curly braces works ok (really I don’t know why):
template:
- button:
- name: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
Start
{% else %}
Stop/Pause
{% endif %}
icon: >-
{% if states('input_boolean.start_stop_chrono') == 'off' %}
mdi:play-circle-outline
{% else %}
mdi:stop-circle-outline
{% endif %}
press:
service: input_boolean.toggle
target:
entity_id: input_boolean.start_stop_chrono
In addition to Jordan’s change, your return values need to be strings… you can do this as I have below by removing the {{ }}
or you could use " "
inside the expressions i.e. {{"Start"}}
.
template:
- button:
- name: >-
{% if is_state('input_boolean.start_stop_chrono', 'off') %}
Start
{% else %}
Stop/Pause
{% endif %}
icon: >-
{% if is_state('input_boolean.start_stop_chrono', 'off') %}
mdi:play-circle-outline
{% else %}
mdi:stop-circle-outline
{% endif %}
press:
- service: input_boolean.toggle
target:
entity_id: input_boolean.start_stop_chrono
1 Like
This code gives no error, but pressing the button does not have any effect. The code I’ve copied just up works fine.
For the sake of completeness, as the button’s name changes, adding an unique_id
, the final working code is:
template:
- button:
- unique_id: 'start_stop_chrono'
name: >-
{% if states('input_boolean.start_chrono') == 'off' %}
Start
{% else %}
Stop/Pause
{% endif %}
icon: >-
{% if states('input_boolean.start_chrono') == 'off' %}
mdi:play-circle-outline
{% else %}
mdi:stop-circle-outline
{% endif %}
press:
service: input_boolean.toggle
target:
entity_id: input_boolean.start_chrono
Thank you everybody for your help!
2 Likes
pilot1981
(Pilot1981)
February 4, 2023, 12:15pm
8
Where I have to insert it? In configuration.yaml? Under button: section?
You can add it in configuration.yaml at the end of the file, or under template section if you have more template entities defined as shown here: https://www.home-assistant.io/integrations/template/ , or wherever you want.
Or you can create a file for template entities if you have a lot, with:
template: !include template.yaml
in your configuration.yaml.
Or even you can create a package file inside config/packages if you have in your configuration.yaml:
homeassistant:
packages: !include_dir_named packages
allowlist_external_dirs:
- /config
as the example in: https://community.home-assistant.io/t/stopwatch-with-start-stop-resume-lap-and-reset/443994
1 Like