Hi,
I’ve got a situation which I guess is fairly common however I haven’t been able to find any particularly clever solutions (yet). Hoping some YAML / macro / anchor / jinja / automation geniuses can help here
Essentially I have a large number of devices which I need to define as fan template entities. The templates are fairly large and the only real differences between them are name & friendly name (which appear in multiple places, sometimes within a larger name, throughout the template). I’m really really hoping that there’s some clever way I can define these entities without copy/pasting 20+ times and manually editing - this would create a really large, unwieldy, and difficult to maintain config file.
I’ve managed to create a template fan for one of those that works really well.
With the way I’ve set up the template the only thing that needs to change between each fan instance is literally just the name (ie “master_bedroom” / “guest_bedroom” / “child1_bedroom” / “lounge01” etc etc etc). and friendly name (“Master Bedroom Fan” etc).
Whilst I could get some efficiencies by invoking scripts in the templates (ie for turn on/off), it’s really frustrating that I’d still have to duplicate a lot else.
Would it be possible to use scripting to create all these entities, or can I use some other ‘trick’ such as anchors, macros, automations etc?
Any & all help and hints much appreciated!
FYI, the template fan looks like this (each entity also relies on an additional input boolean and input number with the same naming convention). I guess it would be easy enough to write a script that takes a template and a list of names and creates a ginormous yaml file, but the problem is you still end up with a ginormous yaml file…:
(for extra credit, the fan speed calculation uses a constant that is just the speed_count divided by 100. Could we parameterise that as well???)
fan:
- platform: template
fans:
master_bedroom_fan:
friendly_name: "Master Bedroom Fan"
unique_id: master_bedroom_fan
speed_count: 6
value_template: >
{{ states('input_boolean.master_bedroom_fan_state') }}
turn_on:
- service: fan.set_percentage
target:
entity_id: fan.master_bedroom_fan
data:
percentage: "{{ states('input_number.master_bedroom_fan_percentage') }}"
turn_off:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.master_bedroom_fan_state
- service: remote.send_command
target:
entity_id: remote.linknlink01
data:
device: master_bedroom_fan
num_repeats: 1
delay_secs: 0.4
hold_secs: 0
command: turn_off
direction_template: "{{states('input_text.master_bedroom_fan_direction')}}"
set_direction:
- service: input_text.set_value
target:
entity_id: input_text.master_bedroom_fan_direction
data:
value: "{{direction}}"
- service: remote.send_command
target:
entity_id: remote.linknlink01
data:
device: master_bedroom_fan
num_repeats: 1
delay_secs: 0.4
hold_secs: 0
command: reverse
percentage_template: >
{{ states('input_number.master_bedroom_fan_percentage') if is_state('input_boolean.master_bedroom_fan_state', 'on') else 0 }}
set_percentage:
- service: input_boolean.turn_{{ 'on' if percentage > 0 else 'off' }}
target:
entity_id: input_boolean.master_bedroom_fan_state
- service: input_number.set_value
target:
entity_id: input_number.master_bedroom_fan_percentage
data:
value: "{{ percentage }}"
- service: remote.send_command
target:
entity_id: remote.linknlink01
data:
device: master_bedroom_fan
num_repeats: 1
delay_secs: 0.4
hold_secs: 0
command: >
{% set speed_value = (percentage|float * 0.06)|round(0) %}
{% if (speed_value|float) > 0 %}
speed_{{speed_value}}
{% else %}
turn_off
{% endif %}