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

Ah, ok, so just a gpio output.
I prefer to do automations completely on Esphome, that way they work independently if HA or wifi is down.

You can use Lock component (or switch) and Time component to control your light.
Something like this:

time:
  - platform: sntp
    on_time:
      - seconds: 0
        minutes: 0
        hours: 6
        then:
          - light.turn_off: light_1

      - seconds: 0
        minutes: 0
        hours: 18
        if:
          condition:
            lock.is_locked: my_lock
          then:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 100%
                green: 0%
                blue: 0%
          else:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 0%
                green: 100%
                blue: 0%

output:
  - platform: gpio
    pin: GPIO4
    id: lock_output

lock:
  - platform: output
    name: "Door Lock"
    id: my_lock
    output: lock_output

    on_lock:
      then:
        if:
          condition:
            light.is_on: light_1
          then:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 100%
                green: 0%
                blue: 0%
     on_unlock:
      then:
        if:
          condition:
            light.is_on: light_1
          then:
            - light.turn_on:
                id: light_1
                brightness: 100%
                red: 0%
                green: 100%
                blue: 0%

You need to add light output of course…

Thank you for your code, but I don’t have a clue what happens.
When I validate the code, I get some errors:

while parsing a block mapping
  in "/config/esphome/esphome-web-1a5888.yaml", line 35, column 5
expected <block end>, but found '<block mapping start>'
  in "/config/esphome/esphome-web-1a5888.yaml", line 52, column 6

Is it a problem to use my code and get the link between the LED and the switch?
I’m a total NOOB and I understand the code which I already have.

Might be some small yaml error, feel free to post your code.

Hi,

Again many thanks for your help//
This is my code, for which I do understand. When its not possible to create the link between the switch and the LED, I like to know. I have to try something different instead. Your code is way out of my reach.


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();
          }

Why. It’s simple, there are not even lambdas. Time component gives time when to keep led on and Lock component controls the color when it’s on.
Feel welcome to ask any questions.

Hi,

Thanks again.
it’s probably simple for you… Not For me.
In the future I would like to use the samen kind of script to control switches based on temperature and more.
I do not want to control esphome my time based script, but home assistent is my control center.
Is it possible to link the Led to the switch (in my script) or not?

While I haven’t tried, I don’t see why not
I have doubts you can operate light with .make_call.
so change it to
auto call = id(alert_rgbled).turn_on();

Also, template switch looks weird here, with a lambda returning it’s own state…
If you need to operate a lock, you need gpio_switch.

switch:
  - platform: gpio
    pin: GPIO4
    name: Deur Bijkeuken
    id: switch1

Many thanks. Had a rough weekend, so didn’t got time to react. I’m sorry.

my code is now

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: 10.0.0.20
    gateway: 10.0.0.120
    subnet: 255.255.255.0
    dns1: 10.0.0.120

captive_portal:
ota:
  - platform: esphome
safe_mode:
  disabled: false
  reboot_timeout: 10min
  num_attempts: 5
    
switch:
  - platform: gpio
    pin: D7
    name: Deur Bijkeuken
    id: switch1
           
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
   
# 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).turn_on();
            call.set_rgb(1.0,0.0,0.0);
            call.perform();
          } else {
            auto call = id(alert_rgbled).turn_on();
            call.set_rgb(0.0,1.0,0.0);
            call.perform();
          }

It will turn the LED red when I first switch on the LED and then lock the door. WHen I switch off the lock. The LED is still red. When I switch off the LED and turn it on, the LED will glow green…

I think I’ll have to do something with :
auto call = id(alert_rgbled).turn_on();
but I’m not sure…

`

There is no automation to run the script. Only component running the script is light: on_turn_on…

1 Like

Wow,

That did the trick. Probably not the most clean code, but it works…

captive_portal:
ota:
  - platform: esphome
safe_mode:
  disabled: false
  reboot_timeout: 10min
  num_attempts: 5
    
switch:
  - platform: gpio
    pin: D7
    name: Deur Bijkeuken
    id: switch1
    on_turn_on:
    - script.execute: set_rgb_colour
    on_turn_off:
    - script.execute: set_rgb_colour
           
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
   
# 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).turn_on();
            call.set_rgb(1.0,0.0,0.0);
            call.perform();
          } else {
            auto call = id(alert_rgbled).turn_on();
            call.set_rgb(0.0,1.0,0.0);
            call.perform();
          }

Karosm,
thank you so much

You’re welcome.
At the end, it’s very similar to the code I wrote for you, the logic is just done in a separate script…
Except your code doesn’t present the timer for 6am/6pm