This is a follow-up to my thread about what I can use instead of copy
.
I’m using anchors and aliases so I can use one component as a parent class for other components that are similar. Here’s the part of the YAML file I’m having problems with:
binary_sensor:
AnchorSwitchMain: &AnchorSwitch
- platform: gpio
id: "SwitchMain"
name: "SwitchMain"
web_server: &AnchorSwitchWebServer
sorting_group_id: sorting_group_switchmain
sorting_weight: 10
pin: &AnchorSwitchPin
number: ${SwitchMainPin}
mode:
input: true
pulldown: true
filters:
delayed_on: 10ms
device_class: power
on_press:
- logger.log: "DEBUG PRESS 1: ==> Switch pressed"
- logger.log:
format: "DEBUG PRESS 2: Delay value: %.0f"
args: 'id(TestDelay).state'
on_release:
- logger.log:
format: "DEBUG RELEASE 1: Delay value: %.0f"
args: 'id(TestDelay).state'
- logger.log: "DEBUG RELEASE 2: <== Switch released"
Switch01:
<<: *AnchorSwitch
id: "Switch01"
name: "Switch 1"
web_server:
<< : *AnchorSwitchWebServer
sorting_group_id: sorting_group_switch1
pin:
<< : *AnchorSwitchPin
number: ${Switch1Pin}
The problem is the 2nd line:
AnchorSwitchMain: &AnchorSwitch
I’m not sure if I’m doing this correctly. The section I’m attempting to reference si the entire section from the 3rd line to the last line (with logging debug statements) before the blank line and Switch01
. I’m not worried about TestDelay
at this point - that’s defined elsewhere.
What is the correct way to specify the component AnchorSwitchMain
so I can use it as an alias later (like for Switch01
)?
(Also note that I’m using 2 more anchors within AnchorSwitchMain, both AnchorSwitchWebserver and AnchorSwitchPin. I’ve been working with this and haven’t had a problem using those two anchors within the other anchor, but I am having trouble and suspect I’m not doing something right at the top of AnchorSwitchMain. It’s easy to add the anchor to the other two, since the anchor goes after teh name of the upcoming dictionary or list, but I’m not clear what the reasoning is and where the anchor for AnchorSwitchMain should be.
Edited to add:
Here’s a second example:
number:
SwitchMainDelay: &AnchorDelay
- platform: template
id: "SwitchMainDelay"
name: "Main Switch Delay"
optimistic: true
min_value: 0
max_value: 600
step: 1
initial_value: 0
web_server: &AnchorDelayWebServer
sorting_group_id: sorting_group_switchmain
sorting_weight: 30
Switch01Delay:
<<: *AnchorDelay
id: "Switch01Delay"
name: "Switch 01 Delay"
web_server:
<< : *AnchorDelayWebServer
sorting_group_id: sorting_group_switch1
Switch02Delay:
<<: *AnchorDelay
id: "Switch02Delay"
name: "Switch 2 Delay"
web_server:
<< : *AnchorDelayWebServer
sorting_group_id: sorting_group_switch2
In this formatting, I get an error that number:
needs a platform
. I tried swapping the order and putting SwitchMainDelay: &AnchorDelay" after
–platform: template` and it didn’t help.
I’m not at all clear how I can label that section and specify the platform.