How can I simplify automation duplicates

I have two tesla cars I want to be able to charge.
I have some flows to start/stop charging and modify the amps and switch to 1phase or 3 phase charging.
But then I need to make duplicates of all these automations with the only difference the name of the car.

How can I simplify this?
I was thinking of a function that runs on the selected car name?

Here is an example of the code where ************ is the name of the car.
Is there a way to run the automation with *************** = value of a variable
Variable is set before running the automation.
So when I get home, I plug in the car and select the car that needs to be charged and I select in what condition it has to charge. (pur solar, solar with some degree of grid, charge with no restrictions except captar (max power of the house))
Thx
Kevin

alias: YQS Start charging
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.p1_meter_5c2faf055da6_active_power
    below: input_number.start_charging_at
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition:
  - condition: state
    entity_id: device_tracker.*******************_location_tracker
    state: home
  - condition: state
    entity_id: switch.*******************_charger
    state: "off"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id:
        - switch.*******************_polling
        - switch.*******************_charger
  - service: number.set_value
    data:
      value: "5"
    target:
      entity_id: number.*******************_charging_amps
  - service: notify.mobile_app_keviniphone12
    data:
      message: YQS start charging
  - service: automation.turn_on
    data: {}
    target:
      entity_id:
        - automation.yqs_charging_stop
        - automation.yqs_charging_flow
  - service: tesla_custom.polling_interval
    data:
      scan_interval: 29
  - wait_for_trigger: []
    timeout:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: automation.turn_off
    data:
      stop_actions: true
    target:
      entity_id: automation.yqs_start_charging
mode: single

You might be able to achieve this by using scripts.
The ‘repeated’ routine is written a script ( see Home Assistant Scripts).
From the automation you can call the script and pass the name of the car via parameters.

example for the action section in the automation:

  action:
    service: script.charge_car
    data:
      carname: mycar

script:

charge_car:
  alias: Charge the right car
  sequence:
  - service: whatever service you need to call
    data_template:
      entity_id: '{{ carname }}'

Just use specific triggers for each car and use these triggers in the automation to decide, which car you want to let the automation run on. :slight_smile:

Okay, I will try that.
thx

I don’t understand how automation trigger is going to help.
I don’t want to trigger an automation.
I want that the entidy_id of an automation is modified depending on the car that is selected.
That way I can prevent to have similar automations depending on what car I am charging. Also when a car name chages, I don’t need to reprogram a lot.

I created a Text helper ‘selected car’ where the name of the car is saved that needs to be charged.
If the target entity_id is ‘number.********************_amps’ then the ********* need to be replaced with the value of the Text helper ‘selected car’

How can I do that??

Thx
Kevin

Make a blueprint.

But the blueprint is used to create new automations, so at the end I have 4 automations for car1 and 4 automations for car2???

So if you have a helper input text named input_text.selected_car, then you might be able to do something like this in your automations…?

condition:
  - condition: state
    entity_id: "{{ 'device_tracker.' ~ states('input_text.selected_car') ~ '_location_tracker' }}"

And likewise throughout.

For the trigger you might need to do in two steps using trigger variables. Hope this helps!

You would have two automations, one for each car, both using the same blueprint, which you can modify in common.

That seems very reasonable.

So what you posted above isn’t an automation you want to fire? :wink: Interesting. :laughing:

Let me explain that a little further:
The one thing that is changing in your automation between the two cars is the trigger. It is, where you decide which car you want to charge. So to use this in a trigger, and therefor use it downstream in the automation, I’d suggest an input_select rather than a text field. A text field can have different values, eg. you write the name wrong or something like that. That’s where working with configured values, like in an input_select, comes in handy. :slight_smile:

If you now come home, you select which car to charge from the input_select. This change is the one that triggers your automation to charge. And as you now know, what car should be charged, you can use this along the way, eg.

trigger:
  - platform: state
    entity_id: input_select.my_car_select
[...]
action:
  - service: switch.turn_on
    entity_id: {{trigger.entity_id}}

This is just an example, but it should show the way you can go. This will not be the only way to do things, but imho it’s the easiest way, to have only a minimum of user interaction needed to run.

Btw., and this is not meant rude, your writing style comes off as very demanding. Not only one, but three question marks at the end and things like that?! I know, in your assumed native language french, this is nothing to watch out for, but in english it doesn’t come off as nice. :open_mouth: Just wanted to say this, I didn’t take it that way, as I do know and try to speak french (still trying to learn, can’t really get the hang of it, but I’m trying!). :slight_smile: But others might not. So just take it as a hint, not meant rude, not meant to anger you or criticise you, just a note. :blush:

And another thing, I’d really advise to start thinking about a naming convention of some kind for your devices, it will make things easier in the future. :slight_smile: