Automation service template with each it's own data_template

Hi guys, I’ve been trying to find a solution for one of my switches that can have multiple state changes. I would like to have just one automation handling all states but I can’t figure out (perhaps not possible?) how I can setup different services using service_templates and different attributes using data_templates.

In all examples, that I can find in the documentation and on this forum, you see services that require the same data_template attribute entity_id (but different entities) but this is not the case for me.

As an example:
I would like to set a hue scene with a single-click (which requires a group_name and scene name as data attributes), and turn on my tv with a double-click (which requires just an entity_id).

How can I put these things together? I can create separate automations for each click type but wouldn’t that be a waste of code and an unnecessary addition of triggers for HA to monitor?

You really can’t do that in YAML given HA’s current implementation. At least, I don’t know of a way. You should just create multiple automations. Yes, there will be more triggers for it to process on every state_changed event, but that’s what HA does. The alternative (if it were possible) would result in less trigger processing, but more execution time spent evaluating templates. Nothing is free. :slight_smile:

One other alternative would be to make the decisions and generate the service calls in a Python Script. You could then have just one automation, whose action would be to invoke the Python Script. You could even pass it the trigger variable.

1 Like

thanks! I’ll look into the python component in that case. Until now I’ve been able to do all with just yaml but I sort-of forgot about the (python) script components altogether there…