Simple Way For MQTT Switch State Change to Tigger Automation

I have a number of Sonoffs that I control via HA and am planning to add 433Mhz remotes to be able to control them. The way I have always done this is to add the remote as a switch (or binary sensor) and use a state change of that switch to trigger the automation to change the state of the Sonoff (via MQTT).

Is there a better way to do this? With this method, I have to define an automation for every remote switch entity and tie it to the correct Sonoff entity, but it seems like there should be a way to do this mapping more easily; more of a “this switch (or list of switches) controls this entity” type of config. Templating the automation seemed promising, but I don’t think I can define mappings so that switch.remote_1 = switch.sonoff_a.

Anyone know of a way to easily configure something like this? It would be nice if a switch entity (or a light entity or something) included a “controlling switch” parameter which would accept a list of switch entities.

If your entity ids were cleverly set up it should be easy.

e.g.

binary_sensor.number_1
switch.number_1 ( to be controlled by binary_sensor.number_1)
binary_sensor.number_2
switch.number_2 (to be controlled by binary_sensor.number_2)
etc…
As long as the object id of the binary sensor matches the object id of the switch this automation below should work.

e.g. could be binary_sensor.apples and switch.apples

Then set up an automation to trigger on all your binary sensors:

trigger:
  platform: state
  entity_id:
    - binary_sensor.number_1
    - binary_sensor.number_2
    - binary_sensor.number_3 
    - binary_sensor.apples         # add as manny as you need
action:
  service_template: 'service_template: switch.turn_{{ trigger.to_state.state }}'
  entity_id: 'switch.{{trigger.to_state.object_id}}'
1 Like

That is a heck of an idea. I was trying to determine how to do the template, but I never thought about having the whole object id match. I am going to run with that idea for now.

I do this kind of thing but what is going on here?
Or is it just a copy/paste typo?

action:
  service_template: 'service_template: switch.turn_{{ trigger.to_state.state }}'
1 Like

Typo. Should be:

action:
  service_template: 'switch.turn_{{ trigger.to_state.state }}'
1 Like