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!