Door magnet + RGB LED

Hi all,

I would like to use a door (electro)magnet to my garage door. There is also built in a little RGB LED, which I would like to turn its colour depending on the state of the magnet
The script what I have (copied and edited) is so far:

substitutions:
  device_name: deur-bijkeuken
  friendly_name: "Deur Bijkeuken"
  device_description: "Deur Bijkeuken switch en LED"

esphome:
  name: '${device_name}'
  comment: '${device_description}'
  platform: ESP8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: xxxxx
    gateway: xxxx
    subnet: xxxxx
    dns1: xxxxxx

captive_portal:

ota:
  - platform: esphome

safe_mode:
  disabled: false
  reboot_timeout: 10min
  num_attempts: 5
    

switch:
  - platform: template
    name: Deur Bijkeuken
    id: switch1
    optimistic: true
    lambda: return id(switch1).state;
           

light:
  - platform: rgb
    name: "Alert Led"
    id: alert_rgbled
    red: output_red
    green: output_green
    blue: output_blue
    on_turn_on:
    - script.execute: set_rgb_colour


    # Output components for LED pins
output:
  - platform: esp8266_pwm
    id: output_red
    pin: D1
  - platform: esp8266_pwm
    id: output_green
    pin: D2
  - platform: esp8266_pwm
    id: output_blue
    pin: D3

# Every 15 seconds, if the LED is on, then call a script to check the colour is set correctly  
interval:
  - interval: 15sec
    then:
      - if:
          condition:
            light.is_on: alert_rgbled
          then:
            - script.execute: set_rgb_colour
            
   
# Script to set the colour of the RGB LED based on power flows
script:
  - id: set_rgb_colour
    then:
      - lambda: |-
          if (id(switch1).state) {
            auto call = id(alert_rgbled).make_call();
            call.set_rgb(1.0,0.0,0.0);
            call.perform();
          } else {
            auto call = id(alert_rgbled).make_call();
            call.set_rgb(0.0,1.0,0.0);
            call.perform();
          }

Fir what I can see the script is using an interval to check the state of the switch. This isn’t suitable for me.
Day over the LED could be turned off. The magnet can be turned on. At 18pm the LED can be turned on and can be changed depending on the state of the switch.
I don’t like to use the interval, which is useless for my case I think. And bashes home assistant.

Any help will be much appreciated, thanks in advance

Your post is quite confusing. Electromagnet is a device creating magnetic field when current is applied, solenoid is one example. I expect you have some magnetic sensor, reed switch maybe.
Post what you have and what you want to accomplish with your setup.

HI,

Thanks for your reply.
I have a door lock which uses an electromagnet. When the magnet is energized, it pulls the door closed.
If the door is closed, the LED should glow red. if the magnet is not energized, the LED glows green.
The LED should come on between 6pm and 6am. This can be controlled in Home assistant

kind regards