Hello,
I built a small Home Assistant script that generates the next available automation ID from a numeric prefix.
I originally created this for my own installation where I organize automations by domain prefixes (for example 1001 for aeration, 1012 for system, etc.).
Since the solution turned out to be fully native Home Assistant, I thought it might be useful for others.
The script:
• scans all states.automation
• reads their id attribute
• filters IDs matching the given prefix
• calculates the next available suffix
• returns the result in an input_text
Works with both YAML automations and UI-created automations.
No custom integrations required.
Screenshot
Example
Prefix:
1007
Result:
10070000000025
Requirements
Create the following helpers:
Input text
input_text.prefix_id
Stores the numeric prefix used to generate the next automation ID.
input_text:
prefix_id:
name: Prefix ID
mode: text
max: 10
input_text.next_id_result
Stores the generated ID.
input_text:
next_id_result:
name: Next ID Result
mode: text
max: 20
Input select
input_select.automation_domain
Used to select the automation domain / prefix group.
input_select:
prefix_id_select:
name: Prefix ID
options:
# Replace these with your own automation domains
- "1001 - domain 1"
- "1002 - domain 2"
- "1003 - domain 3"
- "1004 - domain 4"
- "1005 - domain 5"
After creating these helpers, add the script provided in this repository.
Script
script:
generate_next_id_from_prefix:
alias: "Generate next automation ID from prefix"
mode: single
sequence:
- variables:
prefix: "{{ (states('input_text.prefix_id') | default('') | trim) | string }}"
- choose:
- conditions:
- condition: template
value_template: "{{ prefix in ['unknown','unavailable','none','', None] }}"
sequence:
- stop: "Prefix empty"
- choose:
- conditions:
- condition: template
value_template: "{{ not (prefix | regex_match('^[0-9]+$')) }}"
sequence:
- stop: "Prefix must be numeric"
- variables:
suffixes: >-
{% set ns = namespace(ids=[]) %}
{% set p = (prefix | string) %}
{% for s in states.automation %}
{% set aid = (state_attr(s.entity_id, 'id') | default('') | string) %}
{% if aid.startswith(p) %}
{% set tail = aid[p | length:] %}
{% if tail | regex_match('^[0-9]+$') %}
{% set ns.ids = ns.ids + [tail | int] %}
{% endif %}
{% endif %}
{% endfor %}
{{ ns.ids }}
- variables:
next_suffix: "{{ (suffixes | max + 1) if (suffixes | length > 0) else 1 }}"
next_id: "{{ prefix }}{{ '%010d' | format(next_suffix) }}"
- service: input_text.set_value
target:
entity_id: input_text.next_id_result
data:
value: "{{ next_id }}"
GitHub
Full files and example configuration:
GitHub repo
If anyone finds this useful or has improvements, feel free to contribute.

