Reuse part of a configuration with changes

Hi!

I’m trying to configure the lights, controlled my push buttons via Siemens Logo! 8 controller and make then appear as normal light switches in homeassistant.
I have implemented it via template integration, but my config file is now bloating with duplicated code…

Therefore I was wondering, can this be simplified? I have read about merges in yaml (<<) but I couldn’t figure out if it is possible to change some parameters in the embedded yaml.

Too many words already, let’s look at the code.
The configuration look like this:

garage:
  friendly_name: Garage
  unique_id: template.light.garage
  value_template: "{{ is_state('binary_sensor.garage', 'on') }}"
  turn_on:
    - condition:
        - condition: state
          entity_id: binary_sensor.garage
          state: "off"
    - service: switch.turn_on
      target:
        entity_id: switch.garage_button
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: switch.garage_button
  turn_off:
    - condition:
        - condition: state
          entity_id: binary_sensor.garage
          state: "on"
    - service: switch.turn_on
      target:
        entity_id: switch.garage_button
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: switch.garage_button

glue_hall:
  friendly_name: Glue Hall
  unique_id: template.light.glue_hall
  value_template: "{{ is_state('binary_sensor.hal_1', 'on') }}"
  turn_on:
    - condition:
        - condition: state
          entity_id: binary_sensor.hal_1
          state: "off"
    - service: switch.turn_on
      target:
        entity_id: switch.glue_hall
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: switch.glue_hall
  turn_off:
    - condition:
        - condition: state
          entity_id: binary_sensor.hal_1
          state: "on"
    - service: switch.turn_on
      target:
        entity_id: switch.glue_hall
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: switch.glue_hall

except that there are about 20 of such blocks.
All of them have similar code for turn_on and turn_off, except for entity_id for binary_sensor and switch.
I am wondering, would it be possible to do something like yaml merges with parameters:

  turn_on: &turn_on
    - condition:
        - condition: state
          entity_id: ${BINARY_SENSOR_ID}
          state: "off"
    - service: switch.turn_on
      target:
        entity_id: ${SWITCH_ID}
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: ${SWITCH_ID}
  turn_off: &turn_off
    - condition:
        - condition: state
          entity_id: ${BINARY_SENSOR_ID}
          state: "on"
    - service: switch.turn_on
      target:
        entity_id: ${SWITCH_ID}
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: ${SWITCH_ID}

garage:
  friendly_name: Garage
  unique_id: template.light.garage
  value_template: "{{ is_state('binary_sensor.garage', 'on') }}"
  <<: *turn_on( BINARY_SENSOR_ID=binary_sensor.garage , SWITCH_ID=switch.garage_button )
  <<: *turn_off( BINARY_SENSOR_ID=binary_sensor.garage , SWITCH_ID=switch.garage_button )

glue_hall:
  friendly_name: Glue Hall
  unique_id: template.light.glue_hall
  value_template: "{{ is_state('binary_sensor.garage', 'on') }}"
  <<: *turn_on (BINARY_SENSOR_ID=binary_sensor.hal_1, SWITCH_ID=switch.glue_hall)
  <<: *turn_off (BINARY_SENSOR_ID=binary_sensor.hal_1, SWITCH_ID=switch.glue_hall)

Any ideas are appreciated!

1 Like

I have also tried to solve it with templating:

{% 
  set templates = [
    {'name': 'garage', 'friendly_name': 'Garage', 'sensor': 'binary_sensor.garage', 'switch': 'switch.garage_button'},
    {'name': 'glue_hall', 'friendly_name': 'Glue Hall', 'sensor': 'binary_sensor.hal_1', 'switch': 'switch.glue_hall'}
  ]
%}

{% for t in templates %}
{{t.name}}:
  friendly_name: {{t.friendly_name}}
  unique_id: template.light.{{t.name}}
  value_template: "{{is_state(t.sensor, 'on') }}"
  turn_on:
    - condition:
        - condition: state
          entity_id: {{t.sensor}}
          state: "off"
    - service: switch.turn_on
      target:
        entity_id: {{t.switch}}
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: {{t.switch}}
  turn_off:
    - condition:
        - condition: state
          entity_id: {{t.sensor}}
          state: "on"
    - service: switch.turn_on
      target:
        entity_id: {{t.switch}}
    - delay:
        milliseconds: 100
    - service: switch.turn_off
      target:
        entity_id: {{t.switch}}
{% endfor %}

This template evaluates fine in template editor, however when I put it into configuration.yaml it gives lots of strange errors… I wonder if there are any other options…

You can try to use Lovelace gen

I‘m using it for my media player. I have one template and use it for all my media players

Example template

Example use of the template

1 Like

I will definitively have a look, but it seems it is only for lovelace (dashboard) config? Or will it work on other config files too?

Only for Lovelace

1 Like