I sometimes want to do multiple actions from one frontend button. I searched for it, multiple threads (1, 2, 3, 4, 5, 6, 7, 8 and even a PR by thomasloven), but the only option was to build an script for each set of actions. I don’t like that clutter
So, here’s my Proof of Concept approach.
1. ONE generic script for all
Right now it’s very basic, it only supports up to 3 actions, and limited to services with one entity.
How about passing a list of actions with possibility of different params then you are not limited to number of actions and can call different service types. Example below only shows 2 service param types but more could be added.
Yes they should accept an entity id in the data block for backwards compatibility, but no that is not always the case. In fact there have been examples where a service does not work at all with the entity_id in the data block. I’ll see if I can find an example, but they were all from quite a while ago. They may have been fixed.
Ah ok, then we should go for the higher certainty. I think then there are 4 scenarios for a service call.
Without entity or data
With only entity
With only data
With entity and data
As such, maybe this is the script…
alias: Multi Tap Action
sequence:
- repeat:
for_each: "{{actions}}"
sequence:
- if:
- condition: template
value_template: "{{repeat.item.entity_id is not defined and repeat.item.data is not defined }}"
then:
service: "{{ repeat.item.service }}"
- if:
- condition: template
value_template: "{{repeat.item.entity_id is defined and repeat.item.data is not defined }}"
then:
service: "{{ repeat.item.service }}"
target:
entity_id: "{{repeat.item.entity_id}}"
- if:
- condition: template
value_template: "{{repeat.item.entity_id is not defined and repeat.item.data is defined }}"
then:
service: "{{ repeat.item.service }}"
data: "{{ repeat.item.data }}"
- if:
- condition: template
value_template: "{{repeat.item.entity_id is defined and repeat.item.data is defined }}"
then:
service: "{{ repeat.item.service }}"
target:
entity_id: "{{repeat.item.entity_id}}"
data: "{{ repeat.item.data }}"
mode: parallel
4 examples to show (what I think!?) is all combinations.
That is exactly what I would need too!
I navigate to a sub dashboard with a tap_action, showing camera snapshots with the ability to browse through them. I would love so much being able to reset the chosen snapshot to the newest one in the same tap_action that navigates to the sub dashboard.
Currently I always need to take a separate action like double click to reset…
@gastonche and @Tarkoon, apologies I did not reply before - not notified of further posts to this.
Not sure if you have solved this but for future info.
As navigate is a function of the button actions (and you can only have 1 action on a button - exactly why this script got created), this needs a little bit of a workaround.
You will need to install browser mod as this creates a service that you can call to navigate to a page in your browser.
Modify the multi_tap_action script to be this:
alias: Multi Tap Action
sequence:
- repeat:
for_each: "{{actions}}"
sequence:
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is not defined and repeat.item.data is
not defined }}
then:
service: "{{ repeat.item.service }}"
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is defined and repeat.item.data is not
defined }}
then:
service: "{{ repeat.item.service }}"
target:
entity_id: "{{ repeat.item.entity_id }}"
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is not defined and repeat.item.data is
defined }}
then:
service: "{{ repeat.item.service }}"
data: >-
{% set b = {"browser_id": browser_id} if
browser_id is defined and "browser_mod" in repeat.item.service else {} %} {{ dict(
repeat.item.data, **b) }}
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is defined and repeat.item.data is
defined }}
then:
service: "{{ repeat.item.service }}"
target:
entity_id: "{{repeat.item.entity_id}}"
data: >-
{% set b = {"browser_id": browser_id} if
browser_id is defined and "browser_mod" in repeat.item.service else {} %} {{ dict(
repeat.item.data, **b) }}
mode: parallel
and then on your button use like this (I have used the previous example showing all scenarios and now adding the navigate with browser_mod scenario. Note the button tap_action must be fire-dom-event and you must include the browser_id: THIS under data.
One caveat of this is that is basically uses browser mod to navigate to the page on the device you have clicked the button on. However, all browser windows that have HA on them on that device will navigate to your lovelace view, not just the one you are currently viewing.
I’ve tried to use this automation, but I ran into some problems:
here is the automation copied from above:
alias: Multi Tap Action
sequence:
- repeat:
for_each: "{{actions}}"
sequence:
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is not defined and repeat.item.data is
not defined }}
then:
service: "{{ repeat.item.service }}"
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is defined and repeat.item.data is not
defined }}
then:
service: "{{ repeat.item.service }}"
target:
entity_id: "{{ repeat.item.entity_id }}"
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is not defined and repeat.item.data is
defined }}
then:
service: "{{ repeat.item.service }}"
data: >-
{% set b = {"browser_id": browser_id} if browser_id is defined
and "browser_mod" in repeat.item.service else {} %} {{ dict(
repeat.item.data, **b) }}
- if:
- condition: template
value_template: >-
{{repeat.item.entity_id is defined and repeat.item.data is
defined }}
then:
service: "{{ repeat.item.service }}"
target:
entity_id: "{{repeat.item.entity_id}}"
data: >-
{% set b = {"browser_id": browser_id} if browser_id is defined
and "browser_mod" in repeat.item.service else {} %} {{ dict(
repeat.item.data, **b) }}
mode: parallel
Failed to call service script/multi_tap_action. extra keys not allowed @ data[‘value’] Failed to call service script/multi_tap_action. extra keys not allowed @ data[‘variable’] Service input_number.set called service script.multi_tap_action which was not found. Failed to call service script/multi_tap_action. Error rendering data template: Result is not a Dictionary Service input_number.set called service script.multi_tap_action which was not found.
Hope someone that can help is still watching and thanks in advance