Templating Automation of Etekcity Switches for use with Broadlink

Hi all,

I have a bunch of Etekcity switches that I have set up like this:

- platform: rpi_rf
  gpio: 22
  switches:
    etekcity_0305_1:
      pulselength: 183
      protocol: 1
      code_on: 21888
      code_off: 21899
      signal_repetitions: 5

Because some of the switches are too far away from my Pi3 to work reliably I have bought a Broadlink RM Pro+ and would like to use it as a backup/secondary driver for the switch actions.
I have the Broadlink working separately from the rpi-rf switches by triggering some scripts, but now I would like for the switch to trigger the script.

And that’s where I need somebody to put me on the right track, please.

I know I could write an automation for each switch for each of the two cases, i.e. turning the switch on through rpi-rf and turning it off the same way each triggers its own script.

- alias: etekcity switch triggers etekcity script on
  trigger:
    - platform: state
      entity_id: switch.etekcity_0305_1
      from: 'off'
      to: 'on'
  action:
    - service: script.turn_on
      entity_id: script.etekcity_0305_1_on
- alias: etekcity switch triggers etekcity script off
  trigger:
    - platform: state
      entity_id: switch.etekcity_0305_1
      from: 'on'
      to: 'off'
  action:
    - service: script.turn_on
      entity_id: script.etekcity_0305_1_off

But I think there must be a smarter way by templating this so that I only need two automations for all switches.
One that triggers the correct script if any of the switches changes the state to ‘on’ and another that does the same thing in case a switch is turned ‘off’?

As you can see the switches are named switch.etekcity_0305_1 and the scripts are named script.etekcity_0305_1_on and script.etekcity_0305_1_off respectively.

Anybody got any ideas?

-Chairstacker

Okay, I’ve made some progress, I think.

I learned that I can use the following string to extract the entity_id of the trigger: {{ state.entity_id[7:] }}
This give me etekcity_0305_1

But when I try to insert that into the ‘action’ section of the automation by replacing

  action:
    - service: script.turn_on
      entity_id: script.etekcity_0305_1_on

with

  action:
    - service: script.turn_on
      data_template:
        entity_id: script.{{ state.entity_id[7:] }}_on

it doesn’t seem to trigger the script.

I have tried to use the template editor but no success so far either - this works fine there:

{%- for state in states.switch -%}
  script.{{ state.entity_id[7:] }}_on
{%- endfor -%}

This is probably just a templating issue with {}, ’ ', or " " - but I’m stuck here at the moment.

Okay, found the solution:

- alias: etekcity switch triggers etekcity script on
  trigger:
    - platform: state
      entity_id: switch.etekcity_0305_1, switch.etekcity_0305_2, switch.etekcity_0305_3, switch.etekcity_0305_4, switch.etekcity_0305_5, switch.etekcity_0315_1, switch.etekcity_0315_2, switch.etekcity_0315_3, switch.etekcity_0315_4, switch.etekcity_0315_5, switch.etekcity_0320_1, switch.etekcity_0320_2, switch.etekcity_0320_3
      from: 'off'
      to: 'on'
  action:
    - service: script.turn_on
      data_template:
        entity_id: script.{{ trigger.from_state.entity_id[7:] }}_on

Using trigger.from_state instead of simply state did the trick.

This means I now have 1 automation instead of the previous 13 :+1: