Hello, first time poster here
I’m working on ESP32 with a bunch of relays and switches - final version should consist of about 20 of each. However for test purposes there are only two relays and four switches connected.
The same piece of code is being applied to each relay/button. It works perfectly by copying code over and over again, but that’s not what I want. My goal is (in pseudocode) something like that:
function do_something(component){
turn_off(component)
delay
log_event
}
do_something(relay1)
do_something(relay2)
Now for the real part I’m using scripts in esphome, mostly because I rely on restart mode. The code (partial) is:
switch:
- platform: gpio
pin: 16
inverted: True
name: "Main relay 1"
id: mn_relay_1
- platform: gpio
pin: 14
name: "Switching relay 1"
id: sw_relay_1
binary_sensor:
- platform: gpio
pin:
number: 18
mode:
input: true
pullup: true
filters:
delayed_on: 10 ms
name: "Button up"
on_press:
then:
if:
condition:
switch.is_on: mn_relay_1
then:
script.execute:
id: stop_it
id_mn_rel: mn_relay_1
id_sw_rel: sw_relay_1
else:
script.execute:
id: lift_it
id_mn_rel: mn_relay_1
id_sw_rel: sw_relay_1
script:
- id: stop_it
parameters:
id_mn_rel: string
id_sw_rel: string
mode: restart
then:
- switch.turn_off: id_mn_rel
- delay: 10ms
- switch.turn_off: id_sw_rel
- id: lift_it
mode: restart
parameters:
id_mn_rel: string
id_sw_rel: string
then:
- switch.turn_off: id_sw_rel;
- delay: 10ms
- switch.turn_on: id_mn_rel
- delay: 5s
- switch.turn_off: id_mn_rel
- delay: 10ms
- switch.turn_off: id_sw_rel
And it fails with “Couldn’t find id_mn_rel”. I’ve tried to replace line with
- switch.turn_off !lambda return id_mn_rel;
but no success - it fails with “This option is not templatable!.”
I would be really grateful for any advice as I’m sure that there is some simple trick which I can’t work out.