One switch multiple gpio on and off

Hello forum
I ve TV and MUSIC system connected to two different sockets which are connected to nodemcu with 2 relays (GPIOs) individually.

Here is what I’m looking.
There should be only one UI button on HA homepage.
When is press the switch on UI, the switch has to activate two relays (two GPIOs ON).
I searched all materials couldnt find it, can you please help?

please dont restrict to two GPIOs there can be more GPIO and followed by IR code aswell.

Thanks

This can be done several ways, look into the integrations Group, Scripts and Automation, each has slight variations on how they operate and call services. I would suggest starting with scripts, which can be done through the UI, - configuration panel - scripts - add button. Use a button card to call whichever entity or service you choose to make. Good luck

Hi Kadem

Thanks for your kind reply.
I tried scripts. switching ON is OK, but how to switch off? it dont have a clue how to do it . each time i "Execute " it turns ON as expected. but i need the same toggle button to turn off aswell.

the best way im preferring is
something like below

output:
  - platform: gpio
    pin: D5
    id: 'relay1'
  - platform: gpio
    pin: D6
    id: 'relay2'

switch:
  - platform: output
    name: "SONY TV"
    output: 'relay1'
    output: 'relay2'

so, with above code i will easily turn on two relays at one click and turn off at one click.

please guide how to achieve it.

1 Like

So if I understand what you want: the code you have is on the esp device, the output part looks fine, but I am unsure if you can have two outputs on one switch, but if that works great, then all you need is to find the entity_id for the esp switch( probably switch.sony_tv ), on the entities page in HA, and make an entities card, using that entity_id. Because it is a switch it will be a toggle device on the card you can turn on and off.
If you can’t have two outputs then on the esp:

switch:
  - platform: output
    name: "SONY TV 1"
    output: 'relay1'
  - platform: output
    name: "SONY TV 2"
    output: 'relay2'

then group them like this in HA configuration.yaml:

group:
  sony:
    name: 'Sony TV'
    entities:
      - switch.sony_tv_1
      - switch.sony_tv_2

Then use the entity_id for the group in the entities card. I have not used groups in a while, so this may not be perfect, but should get you going.
Your issue with scripts and automations, yes they are kind of one way, not easily back to off, they just execute. The reason I mentioned it was because you said you may want to add IR, and you may not be able to group them effectively as the state wont be on or off for that IR entity. And you probably don’t want to run the same IR Code when you shutdown anyway. What you could do is make an input boolean, (see toggle under helpers on the configuration page) in HA, and make two automations in HA, one operates when the input boolean goes from off to on, the other runs when it goes from on to off., one automation can turn on the relays, switches, or call an IR service, even after a short delay to allow time for startup. The other turns them back off. Use an entities card to toggle the input boolean. You could also use a button card and set tap action to toggle. Lots to read, hope this helps a bit. Good luck.

I needed to do exactly this, for my pool pump controller. There is a way:

# Define output pins
output:
  - platform: gpio
    pin: GPIO21
    id: output_pin_1 # pump power
  - platform: gpio
    pin: GPIO18
    id: power_led

switch:
  - platform: output
    id: pool_pump_power
    output: output_pin_1
    name: "Pool Pump Power"
    icon: "mdi:pool"
    on_turn_on:
      then: 
        - output.turn_on: power_led
    on_turn_off:
      then:
        - output.turn_off: power_led

1 Like