In my YAML code on ESPHOME I created a structure that basically turns on 3 status LEDs in my bedroom meaning that: 1) GREEN LED all doors and windows are closed; 2) YELLOW LED: at least one door/window on the first floor is open; 3) RED LED: at least one door/window on the ground floor is open.
To do this I created the following code in each binary sensor:
Since i have to repeat the conditions for all the sensors, is there a way to group them (I mean the conditions) and call them with something like “void”?
If I’m not asking too much, could you give a quick example. Tks in advance
binary_sensor:
- platform: template
name: "Green Alert"
id: green_alert
device_class: power
- platform: template
name: "Red Alert"
id: red_alert
device_class: power
- platform: template
name: "Yellow Alert"
id: yellow_alert
device_class: power
# how do i implement binary GPIO sensors?
- platform: gpio
pin:
number: GPIO17
inverted: false
mode:
input: true
pullup: True
name: "MAIN DOOR"
device_class: door
id: main_door
Update: Ok, now I understand the solution you meant. Basically the correlated sensors (at each floor) at each on_press and on_release updated the respective alert (which is the binary sensor template) and then, the template with another on_press / on_release turns the LEDs on or off. However, even if the solution is perhaps better than mine, it doesn’t solve the problem of duplicating lines of code to verify the status of single entities. So I think the best solution is to integrate the answer of “petro” with that of “Mahko_Mahko”. Thank you both.
Thanks for the further reply. I find it very elegant and will test it right away. Regarding the code duplication issue, since I didn’t understand your approach and, erroneously, I thought that I would have to repeat the status check for each binary sensor (which you clarified with your example). So sorry again, but that was due to my misinterpretation.