MainSwitchDelay
is a global bool:
globals:
- id: TestText
type: String
initial_value: '"Original Text"'
- id: MainSwitchDelay
type: bool
initial_value: "false"
The problem is the delay, itself, is not from a sensor. I have this on MainSwitch
(lots of debugging statements in here, still):
binary_sensor:
#
#Toggle switch group first, with master switch first in that group.
#
- platform: gpio
id: "Mainswitch"
name: "Main Switch"
web_server:
sorting_group_id: sorting_group_master
sorting_weight: 1
pin:
number: ${MasterSwitchPin}
mode:
input: true
pulldown: true
filters:
delayed_on: 10ms
device_class: power
on_press:
- logger.log:
format: "DEBUG PRESS 1: Test Variable Value: %s"
args: id(TestText).c_str()
- logger.log:
format: "DEBUG PRESS 2: Test boolean value: %d"
args: 'id(MainSwitchDelay)'
- globals.set:
id: MainSwitchDelay
value: 'true'
- logger.log:
format: "DEBUG PRESS 3: Test boolean value: %d"
args: 'id(MainSwitchDelay)'
on_release:
- logger.log:
format: "DEBUG RELEASE 2: Test boolean value: %d"
args: 'id(MainSwitchDelay)'
- logger.log: "DEBUG RELEASE 2: <== Switch released, calling script"
- script.execute: MainSwitch_Delay_Script
- logger.log: "DEBUG RELEASE 3: <== Script returned"
- logger.log:
format: "DEBUG RELEASE 4: Test boolean value: %d"
args: 'id(MainSwitchDelay)'
So when MainSwitch
is turned on, of course its state will be ON
. When it is turned off, the sensor for the switch itself does not go to OFF
. Instead it runs the script with the delay (again, a lot of debugging statements still in here):
script:
- id: MainSwitch_Delay_Script
mode: restart
then:
- logger.log:
format: "DEBUG SCRIPT 1: Test boolean value: %d"
args: 'id(MainSwitchDelay)'
- delay: 5 seconds
- logger.log:
format: "DEBUG SCRIPT 2: Test boolean value: %d"
args: 'id(MainSwitchDelay)'
- globals.set:
id: MainSwitchDelay
value: 'false'
- logger.log:
format: "DEBUG SCRIPT 3: Test Variable value: %d"
args: 'id(MainSwitchDelay)'
- logger.log: "DEBUG SCRIPT 4: <-- Delay script ending"
MainSwitchDelay
is true
when the switch is turned on and stays true for, in this test, 5 seconds after it’s turned off. While this value is related to the state of MainSwitch
, its value is not always the same as MainSwitch
. So what I need to publish is not the state of a sensor, but the state of the delay. That’s why I’m making the Sensor Template dependent on a bool that I can turn on when the switch goes on and turn off later, after the switch goes off.
The two things I’m still trying to work out is how to use the value of a template number in a statement like this, to replace the fixed value of 5:
- delay: 5 seconds
and I need to have the delay available for 4 switches and 4 sensors. Since I can only use arrays for C++ variables, like bool, int, and String, I can’t have arrays of Binary Sensors, Number Templates, and scripts. (As best I can tell, I can’t make a script use paremeters, so I can’t call a script with a value to change.)
(Side note: On the control board, I have DTPST switches to turn on the board, the CNC, the laser driver, and the laser itself. One pole of each switch controls a device, like the CNC, the other is connected to a GPIO. In the code, I call these switches, but the component in ESPHome is a Binary Sensor. I do not want something like a CNC or an etching laser to be accessible from the ESPHome or HA interface. You can imagine the mess if even an etching laser was somehow accidentally turned on through HA and was not turned off. I want only direct physical control of these systems.)
.