Template switch turns ON at OFF action

I have a working 16 x relay board. I’m going to use it to control my underfloor heating. The individual relays turn on and off the actuators. Some rooms have multiple actuators controlling the water flow. So with one switch, I want to turn on 6 relays at the same time. I created a template switch, but I must be missing something.

switch:
  - platform: template
    name: "Vloerverwarming - Woonkamer"
    turn_on_action:
      - switch.turn_on: relay_woonkamer_1
      - switch.turn_on: relay_woonkamer_2
      - switch.turn_on: relay_woonkamer_3
      - switch.turn_on: relay_woonkamer_4
      - switch.turn_on: relay_woonkamer_5
      - switch.turn_on: relay_woonkamer_6                 
    turn_off_action:
      - switch.turn_off: relay_woonkamer_1
      - switch.turn_off: relay_woonkamer_2
      - switch.turn_off: relay_woonkamer_3
      - switch.turn_off: relay_woonkamer_4
      - switch.turn_off: relay_woonkamer_5
      - switch.turn_off: relay_woonkamer_6

The template switch get’s created and when I turn it on, it turns on the relays. But turning off doesn’t do anything. When I look at the webserver of the ESP board, it shows me it’s turning them on again.

Tried the switch.toggle with the turn_off action too, but that makes it even more weird because then the template switch turns the relays on, but immediately turns the template switch off, while keeping the relays on.

I’d like to have the 6 relays just turn off.

Feel free to post your whole yaml.

Sure:

esphome:
  name: vloerverwarming
  friendly_name: vloerverwarming

esp8266:
  board: esp12e

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxx"

ota:
  - platform: esphome
    password: "xxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Vloerverwarming Fallback Hotspot"
    password: "xxxx"

captive_portal:

# Enable Web server
web_server:
  port: 80

substitutions:
  pin_status: GPIO02        # Internal
  pin_onboard_button: GPIO0 # Internal

# Status LED
light:
  - platform: status_led
    name: "RelayBoard LED"
    restore_mode: ALWAYS_ON
    pin:
      number: ${pin_status}
      inverted: True

sn74hc595:
  - id: 'sn74hc595_hub_0'
    data_pin: GPIO14
    clock_pin: GPIO13
    latch_pin: GPIO12
    oe_pin: GPIO5
    sr_count: 2

# Individual outputs
switch:
  - platform: template
    name: "Vloerverwarming - Woonkamer"
    turn_on_action:
      - switch.turn_on: relay_woonkamer_1
      - switch.turn_on: relay_woonkamer_2
      - switch.turn_on: relay_woonkamer_3
      - switch.turn_on: relay_woonkamer_4
      - switch.turn_on: relay_woonkamer_5
      - switch.turn_on: relay_woonkamer_6
    turn_off_action:
      - switch.turn_off: relay_woonkamer_1
      - switch.turn_off: relay_woonkamer_2
      - switch.turn_off: relay_woonkamer_3
      - switch.turn_off: relay_woonkamer_4
      - switch.turn_off: relay_woonkamer_5
      - switch.turn_off: relay_woonkamer_6
  - platform: gpio
    name: "Relay Woonkamer #1"
    id: relay_woonkamer_1
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 0
      number: 0
      inverted: false
  - platform: gpio
    name: "Relay Woonkamer #2"
    id: relay_woonkamer_2
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 1
      number: 1
      inverted: false
  - platform: gpio
    name: "Relay Woonkamer #3"
    id: relay_woonkamer_3    
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 2
      number: 2
      inverted: false
  - platform: gpio
    name: "Relay Woonkamer #4"
    id: relay_woonkamer_4    
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 3
      number: 3
      inverted: false
  - platform: gpio
    name: "Relay Woonkamer #5"
    id: relay_woonkamer_5    
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 4
      number: 4
      inverted: false
  - platform: gpio
    name: "Relay Woonkamer #6"
    id: relay_woonkamer_6    
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 5
      number: 5
      inverted: false
  - platform: gpio
    name: "Relay #7"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 6
      number: 6
      inverted: false
  - platform: gpio
    name: "Relay #8"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 7
      number: 7
      inverted: false
  - platform: gpio
    name: "Relay #9"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 8
      number: 8
      inverted: false
  - platform: gpio
    name: "Relay #10"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 9
      number: 9
      inverted: false
  - platform: gpio
    name: "Relay #11"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 10
      number: 10
      inverted: false
  - platform: gpio
    name: "Relay #12"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 11
      number: 11
      inverted: false
  - platform: gpio
    name: "Relay #13"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 12
      number: 12
      inverted: false
  - platform: gpio
    name: "Relay #14"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 13
      number: 13
      inverted: false
  - platform: gpio
    name: "Relay #15"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 14
      number: 14
      inverted: false
  - platform: gpio
    name: "Relay #16"
    pin:
      sn74hc595: sn74hc595_hub_0
      # Use pin number 15
      number: 15
      inverted: false

Didn’t think it was needed. It is obviously a work-in-progress, but the relays work like a charm. I can switch on and off every one of the 16 relays without any delays.

When I turn off the template switch the following shows in the logs:

16:56:43	[D]	[switch:012]    'Vloerverwarming - Woonkamer' Turning ON.
16:56:43	[D]	[switch:012]	'Relay Woonkamer #1' Turning ON.
16:56:43	[D]	[switch:012]	'Relay Woonkamer #2' Turning ON.
16:56:43	[D]	[switch:012]	'Relay Woonkamer #3' Turning ON.
16:56:43	[D]	[switch:012]	'Relay Woonkamer #4' Turning ON.
16:56:43	[D]	[switch:012]	'Relay Woonkamer #5' Turning ON.
16:56:44	[D]	[switch:012]	'Relay Woonkamer #6' Turning ON.

So in web server you turn the switch off and only thing you get on log is “Turning ON”?

Yes correct, weird right?

Doesn’t sound normal…

Agreed, it shouldn’t be that hard, right?

See turnoffaction.mp4 for a small vid.

so how you manage to turn them off?

With the individual switches. It is only the template switch acts like this.

why 16 relays,if you need only two? Connect 6 actuators to one relay and other 6 to second relay. Yaml will be “lighter” and easier.Or add delay to yaml(turn on 1,delay,turn on two ,delay and so on).Sorry about my english.

Well, that is an option too, haven’t even thought of this option…. . The other six are rooms on their own but still would need only seven.

Doesn’t explain this weirdness. Shouldn’t it just werk like this?

I think too many operations at same time

It should work like you expect. I have never had this kind of behavior with template switch.

Try like this:

turn_on_action:
- switch.turn_on: relay_woonkamer_1
- delay: 1s
- switch.turn_on: relay_woonkamer_2
- delay: 1s
- switch.turn_on: relay_woonkamer_3
- delay: 1s
- switch.turn_on: relay_woonkamer_4
- delay: 1s
- switch.turn_on: relay_woonkamer_5
- delay: 1s
- switch.turn_on: relay_woonkamer_6

If there is some bug, that could work.
But switching without delays should work without tinkering. For Esp to write 6 pins low takes few microseconds. One loop on esphome is 16 milliseconds, so it’s not anyhow busy with those actions.

ChatGPT said:
ChatGPT
If the ESPHome device crashes when 8 relays are activated at once, this may indicate a hardware limitation or a power source problem. Here are some possible causes and solutions:

  1. The power supply is not able to carry the load
    If the relays receive power through an ESP device, or if the power supply is too weak, then turning on a large number of relays at once can lead to a voltage drop and the device restarts.

The turning on isn’t the problem. It is turning off. I did however add the delay, to both the turning on as the turning off:

switch:
  - platform: template
    name: "Vloerverwarming - Woonkamer"
    turn_on_action:
      - switch.turn_on: relay_woonkamer_1
      - delay: 1s
      - switch.turn_on: relay_woonkamer_2
      - delay: 1s      
      - switch.turn_on: relay_woonkamer_3
      - delay: 1s      
      - switch.turn_on: relay_woonkamer_4
      - delay: 1s      
      - switch.turn_on: relay_woonkamer_5
      - delay: 1s      
      - switch.turn_on: relay_woonkamer_6
    turn_off_action:
      - switch.turn_off: relay_woonkamer_1
      - delay: 1s      
      - switch.turn_off: relay_woonkamer_2
      - delay: 1s      
      - switch.turn_off: relay_woonkamer_3
      - delay: 1s      
      - switch.turn_off: relay_woonkamer_4
      - delay: 1s      
      - switch.turn_off: relay_woonkamer_5
      - delay: 1s      
      - switch.turn_off: relay_woonkamer_6
19:47:29 [D] [api.connection:1446] Home Assistant 2025.1.2 (192.168.21.92): Connected successfully
19:47:41 [D] [switch:012] ‘Vloerverwarming - Woonkamer’ Turning ON.
19:47:41 [D] [switch:012] ‘Relay Woonkamer #1’ Turning ON.
19:47:41 [D] [switch:055] ‘Relay Woonkamer #1’: Sending state ON
19:47:42 [D] [switch:012] ‘Relay Woonkamer #2’ Turning ON.
19:47:42 [D] [switch:055] ‘Relay Woonkamer #2’: Sending state ON
19:47:43 [D] [switch:012] ‘Relay Woonkamer #3’ Turning ON.
19:47:43 [D] [switch:055] ‘Relay Woonkamer #3’: Sending state ON
19:47:44 [D] [switch:012] ‘Relay Woonkamer #4’ Turning ON.
19:47:44 [D] [switch:055] ‘Relay Woonkamer #4’: Sending state ON
19:47:45 [D] [switch:012] ‘Relay Woonkamer #5’ Turning ON.
19:47:45 [D] [switch:055] ‘Relay Woonkamer #5’: Sending state ON
19:47:46 [D] [switch:012] ‘Relay Woonkamer #6’ Turning ON.
19:47:46 [D] [switch:055] ‘Relay Woonkamer #6’: Sending state ON
19:47:50 [D] [switch:012] ‘Vloerverwarming - Woonkamer’ Turning ON.
19:47:50 [D] [switch:012] ‘Relay Woonkamer #1’ Turning ON.
19:47:52 [D] [switch:012] ‘Relay Woonkamer #2’ Turning ON.
19:47:52 [D] [switch:012] ‘Relay Woonkamer #3’ Turning ON.
19:47:53 [D] [switch:012] ‘Relay Woonkamer #4’ Turning ON.
19:47:54 [D] [switch:012] ‘Relay Woonkamer #5’ Turning ON.
19:47:56 [D] [switch:012] ‘Relay Woonkamer #6’ Turning ON.

As you can see, there is a delay in the turning on. But hitting the off button still results in another on command.

If it were a power issue, shouldn’t it occur during the turning on commands?


This line:

19:47:50 [D] [switch:012] ‘Vloerverwarming - Woonkamer’ Turning ON.

I switched to off on the website of the ESP.

I have 24 esp devices,that works perfectly,some of them includes over 1000 rows of codes.Try to aad delays or better power supply

what kind of i2c you have? bilt in on relayboard or separate?

try to use “on_turn_on:” instead of “turn_on_action:”