Hi all. Apologies if this is covered elsewhere.
I have a number (a lot!) of template switches that use more than one switch entities to provide redundancy, for example where a physical relay can be controlled by more than one switch entity via different integrations. I also do the same with template sensors. For example, here’s a couple of switch templates.
switch:
- platform: template
switches:
flat_2_green_room_bug_zapper:
friendly_name: Flat 2 Green Room Bug Zapper
unique_id: bc8944bc-0b5d-40de-adb8-d72920604521
availability_template: >-
{{
is_state('switch.flat_2_green_room_bug_zapper_tapo', 'on') or
is_state('switch.flat_2_green_room_bug_zapper_tapo', 'off') or
is_state('switch.flat_2_green_room_bug_zapper_tplink', 'on') or
is_state('switch.flat_2_green_room_bug_zapper_tplink', 'off')
}}
value_template: >-
{{
is_state('switch.flat_2_green_room_bug_zapper_tapo', 'on') or
is_state('switch.flat_2_green_room_bug_zapper_tplink', 'on')
}}
turn_on:
- action: switch.turn_on
target:
entity_id:
- switch.flat_2_green_room_bug_zapper_tapo
- switch.flat_2_green_room_bug_zapper_tplink
turn_off:
- action: switch.turn_off
target:
entity_id:
- switch.flat_2_green_room_bug_zapper_tapo
- switch.flat_2_green_room_bug_zapper_tplink
icon_template: >-
{% if is_state('switch.flat_2_green_room_bug_zapper_tapo', 'on') or
is_state('switch.flat_2_green_room_bug_zapper_tplink', 'on') %}
mdi:flash
{% elif is_state('switch.flat_2_green_room_bug_zapper_tapo', 'off') or
is_state('switch.flat_2_green_room_bug_zapper_tplink', 'off') %}
mdi:flash-off
{% else %}
mdi:flash-alert
{% endif %}
flat_2_bedroom_bug_zapper:
friendly_name: Flat 2 Bedroom Bug Zapper
unique_id: 683fad81-2c01-401b-b272-480cd9578ee6
availability_template: >-
{{
is_state('switch.flat_2_bedroom_bug_zapper_tapo', 'on') or
is_state('switch.flat_2_bedroom_bug_zapper_tapo', 'off') or
is_state('switch.flat_2_bedroom_bug_zapper_tplink', 'on') or
is_state('switch.flat_2_bedroom_bug_zapper_tplink', 'off')
}}
value_template: >-
{{
is_state('switch.flat_2_bedroom_bug_zapper_tapo', 'on') or
is_state('switch.flat_2_bedroom_bug_zapper_tplink', 'on')
}}
turn_on:
- action: switch.turn_on
target:
entity_id:
- switch.flat_2_bedroom_bug_zapper_tapo
- switch.flat_2_bedroom_bug_zapper_tplink
turn_off:
- action: switch.turn_off
target:
entity_id:
- switch.flat_2_bedroom_bug_zapper_tapo
- switch.flat_2_bedroom_bug_zapper_tplink
icon_template: >-
{% if is_state('switch.flat_2_bedroom_bug_zapper_tapo', 'on') or
is_state('switch.flat_2_bedroom_bug_zapper_tplink', 'on') %}
mdi:flash
{% elif is_state('switch.flat_2_bedroom_bug_zapper_tapo', 'off') or
is_state('switch.flat_2_bedroom_bug_zapper_tplink', 'off') %}
mdi:flash-off
{% else %}
mdi:flash-alert
{% endif %}
Is there any way to create a macro, blueprint or other object that encapsulates the logic so that I can simply do something like:-
switch:
- platform: template
switches:
flat_2_green_room_bug_zapper: instance_of('redundant_switch', ['switch.flat_2_green_room_bug_zapper_tapo', 'switch.flat_2_green_room_bug_zapper_tplink'])
flat_2_bedroom_bug_zapper: instance_of('redundant_switch', ['switch.flat_2_bedroom_bug_zapper_tapo', 'switch.flat_2_bedroom_bug_zapper_tplink'])
I have some cases where there are two ‘source’ entities and some where there are three so ideally a list of entities would be ideal as shown but it would be okay if I had to define separate 2-entity & 3-redundant-switch macros (or whatever construct it is).
Here are some sample redundant sensors. I appreciate that I could use the HACS ‘average’ integration instead of these but something more directly expressing redundancy rather than statistics would be good.
template:
sensor:
- name: Flat 2 Bathroom Sensor Temperature
unique_id: 0145218f-6e79-4f2c-be6f-0d43399d9837
unit_of_measurement: "°C"
state_class: measurement
device_class: temperature
availability: >-
{{
has_value('sensor.flat_2_bathroom_sensor_temperature_matter') or
has_value('sensor.flat_2_bathroom_sensor_temperature_tapo') or
has_value('sensor.flat_2_bathroom_sensor_temperature_tplink')
}}
state: >-
{{
states('sensor.flat_2_bathroom_sensor_temperature_matter') if has_value('sensor.flat_2_bathroom_sensor_temperature_matter') else
states('sensor.flat_2_bathroom_sensor_temperature_tapo') if has_value('sensor.flat_2_bathroom_sensor_temperature_tapo') else
states('sensor.flat_2_bathroom_sensor_temperature_tplink')
}}
- name: Flat 2 Bedroom Sensor Temperature
unique_id: 7f5910e7-5210-4e1b-b742-d7bee6fd9172
unit_of_measurement: "°C"
state_class: measurement
device_class: temperature
availability: >-
{{
has_value('sensor.flat_2_bedroom_sensor_temperature_matter') or
has_value('sensor.flat_2_bedroom_sensor_temperature_tapo') or
has_value('sensor.flat_2_bedroom_sensor_temperature_tplink')
}}
state: >-
{{
states('sensor.flat_2_bedroom_sensor_temperature_matter') if has_value('sensor.flat_2_bedroom_sensor_temperature_matter') else
states('sensor.flat_2_bedroom_sensor_temperature_tapo') if has_value('sensor.flat_2_bedroom_sensor_temperature_tapo') else
states('sensor.flat_2_bedroom_sensor_temperature_tplink')
}}