How to switch on light

Hi, I have an ESP8266 that mimic a real candle flame via an LED. It is working fine as it is but currently I am relying on home assistant to turn it on and off.

I would like it to turn on/off automatically without wifi presence and as a standalone device without needing home assistant to be always connected.

I have tried to modify the code but i am stuck and do not know how to continue.

I would like the ESP8266 to always turn on the candle at 5pm each day and then turn off at 8am the next day.

I am not sure how to call the ESP8266 to automatically turn on the GPIO via ESPHOME itself.

Also, will the ESP8266 remember what time it is without wifi? I am not sure about the use of the SNTP platform too.

Could someone please steer me in the right direction?

esphome:
  name: candle
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "MyWifi"
  password: "Redacted"
  
  manual_ip:
    static_ip: 192.168.1.151
    gateway: 192.168.1.1
    subnet: 255.255.255.0      

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Candle on timer"
    password: "ippMYcFYdccn"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  safe_mode: True
  password: "mypassword"

web_server:
  port: 80

light:
  - platform: monochromatic
    name: "Candle"
    id: candle
    output: candle_pwm
    default_transition_length: 0s
    effects:
      - flicker:
          name: Flicker
          alpha: 90%
          intensity: 5%

output:
  - platform: esp8266_pwm
    id: candle_pwm
    pin: GPIO2
    frequency: 1000 Hz
    
time:
  - platform: sntp
    on_time:
      # Every evening everyday
      - seconds: 0
        minutes: 0
        hours: 17
        days_of_week: MON-SUN
        then:
          - light.turn_on: my_light
      # Every morning everyday
      - seconds: 0
        minutes: 0
        hours: 8
        days_of_week: MON-SUN
        then:
          - light.turn_off: my_light  

Where is my_light defined?
Isn’t that supposed to be candle?

1 Like

ahh yes it is candle. I mistype there.

When i flash the code to the ESP8266, the code does not seem to run and the candle does not get turn on.

Is there any other error on my code per se? I wonder if the SNTP code is correct to be used this way?

Is it because you have a manual IP address and need to specifiy DNS ?

Also you might need reboot_timeout: 0s if you plan on using it without WIFI as it will reboot every 10 minutes without WIFI connection. Not sure time will work without WIFI though

I would add time check and time conditions. otherwise, if you turn off the electricity after executing your code, the light will not turn on

    on_time:
      - seconds: /5

I can give an example of how I redid sonoff B1 bulb to turn on and off the outdoor lighting in the house according to the height of the sun

esphome:
  name: sonoff-b1
  platform: ESP8266
  board: esp01_1m
  esp8266_restore_from_flash: true

  on_boot:
    priority: -10
    # ...
    then:
      - if:
          condition:
            - switch.is_on: onboot_sonoffb1
            - switch.is_on: stateonboot_sonoffb1
          then: 
            - light.turn_on: blight

      - if:
          condition:
            - switch.is_off: onboot_sonoffb1
            - switch.is_on: stateonboot_sonoffb1
          then: 
            - light.turn_off: blight


wifi:
  ssid: "SId"
  password: "PaSSs"
  use_address: 192.168.0.153
  fast_connect: true
  manual_ip:
    static_ip: 192.168.0.153
    gateway: 192.168.0.1
    subnet: 255.255.255.0
    dns1: 192.168.0.1
    dns2: 8.8.4.4



api:
web_server:
  port: 80
logger:

ota:


my9231:
  data_pin: GPIO12
  clock_pin: GPIO14 
  num_channels: 6
  num_chips: 2
  bit_depth: 8

output:
  - platform: my9231
    id: output_blue
    channel: 0
  - platform: my9231
    id: output_red
    channel: 1
  - platform: my9231
    id: output_green
    channel: 2
  - platform: my9231
    id: output_warm_white
    channel: 4
  - platform: my9231
    id: output_cold_white
    channel: 5

light:
  - platform: rgbww
    name: Sonoff B1
    #restore_mode: ALWAYS_OFF
    id: blight
    color_interlock: true
    red: output_red
    green: output_green
    blue: output_blue
    cold_white: output_cold_white
    warm_white: output_warm_white
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2800 K
    
    on_turn_on:      
     switch.template.publish: 
      id: onboot_sonoffb1
      state: ON

    on_turn_off:      
     switch.template.publish: 
      id: onboot_sonoffb1
      state: OFF      
    

    effects:
      
      - random:
          name: Random Effect With Custom Values
          transition_length: 5s
          update_interval: 7s    

      - flicker:
          name: Flicker Effect With Custom Values
          alpha: 95%
          intensity: 1.5%

      - strobe:
          name: Strobe Effect With Custom Values
          colors:
            - state: true
              brightness: 100%
              red: 100%
              green: 90%
              blue: 0%
              duration: 500ms
            - state: false
              duration: 250ms
            - state: true
              brightness: 100%
              red: 0%
              green: 100%
              blue: 0%
              duration: 500ms

      - pulse:
          name: "Fast Pulse"
          transition_length: 0.5s
          update_interval: 0.5s
      
      - pulse:
          name: "Slow Pulse"
          # transition_length: 1s      # defaults to 1s
          update_interval: 2s



sensor:

  - platform: sun
    name: "sonoffb1 elevation"
    id: elevation_sonoffb1
    type: elevation

  - platform: sun
    name: "sonoffb1 azimuth"
    id: azimuth_sonoffb1c
    type: azimuth

binary_sensor:
  - platform: status
    name: "Sonoff B1 Status"



time:
  - platform: sntp
    id: sonoffb1_time
    servers:
      - 0.ua.pool.ntp.org
      - 1.ua.pool.ntp.org
      - 2.ua.pool.ntp.org
    on_time_sync:
      then:
        - logger.log: "ВрСмя синхронизированно"
    on_time:
      - seconds: /5
        then:
          - if:
              condition:
                - sensor.in_range:
                    id: elevation_sonoffb1 
                    below: '-0.0'
                - switch.is_on: deynight_sonoffb1
                - light.is_off: blight
              then:
                - light.turn_on:
                    id: blight
                    brightness: 100%
                    cold_white: 100%
                    color_temperature: 154 mireds
                    #color_mode: WHITE
          - if:
              condition:
                - sensor.in_range:
                    id: elevation_sonoffb1 
                    above: '-0.0'
                - switch.is_on: deynight_sonoffb1
                - light.is_on: blight
              then:
                - light.turn_off:
                    id: blight


sun:
  latitude: 46.2931Β°
  longitude: 30.3438Β°  


switch:
  - platform: restart                     # Кнопка ΠΏΠ΅Ρ€Π΅Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ
    name: "Sonoff-b1 restart"
    id: sonoffb1_restart  

  - platform: template                    # ΠΊΠ½ΠΎΠΏΠΊΠ° дСнь Π½ΠΎΡ‡ΡŒ
    name: "Day/Night sonoff-b1 "
    icon: 'mdi:theme-light-dark'
    id: deynight_sonoffb1
    optimistic: true

  - platform: template                    # ΠΊΠ½ΠΎΠΏΠΊΠ° ΠΊΠ½ΠΎΠΏΠΊΠ° состояния ΠΏΡ€ΠΈ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅
    #name: "On boot sonoff-b1 "
    #icon: 'mdi:theme-light-dark'
    id: onboot_sonoffb1
    optimistic: true
    restore_state: true

  - platform: template                    # ΠΊΠ½ΠΎΠΏΠΊΠ° ΠΊΠ½ΠΎΠΏΠΊΠ° состояния ΠΏΡ€ΠΈ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅
    name: "State on boot sonoff-b1 "
    #icon: 'mdi:theme-light-dark'
    id: stateonboot_sonoffb1
    optimistic: true
    restore_state: true
################################################################################
  - platform: template                   
    name: "Green"
    icon: 'mdi:lightbulb-group-outline'
    id: green
    optimistic: true
    turn_on_action:
      then:
        - light.turn_on:
            id: blight
            brightness: 100%
            red: 0
            green: 100%
            blue: 0
        - delay: 1s
        - switch.template.publish:
            id: green
            state: Off
    
    
  - platform: template                   
    name: "Blue"
    icon: 'mdi:lightbulb-group-outline'
    id: blue
    optimistic: true
    turn_on_action:
      then:
        - light.turn_on:
            id: blight
            brightness: 100%
            red: 0
            green: 0
            blue: 100%
        - delay: 1s
        - switch.template.publish:
            id: blue
            state: Off


  - platform: template                   
    name: "Red"
    icon: 'mdi:lightbulb-group-outline'
    id: red
    optimistic: true
    turn_on_action:
      then:
        - light.turn_on:
            id: blight
            brightness: 100%
            red: 100%
            green: 0
            blue: 0
        - delay: 1s
        - switch.template.publish:
            id: red
            state: Off    
    
  - platform: template                   
    name: "Magenta"
    icon: 'mdi:lightbulb-group-outline'
    id: magenta
    optimistic: true
    turn_on_action:
      then:
        - light.turn_on:
            id: blight
            brightness: 100%
            red: 50%
            green: 0
            blue: 50%
        - delay: 1s
        - switch.template.publish:
            id: magenta
            state: Off     


  - platform: template                   
    name: "Vivid cyan"
    icon: 'mdi:lightbulb-group-outline'
    id: vivid_cyan
    optimistic: true
    turn_on_action:
      then:
        - light.turn_on:
            id: blight
            brightness: 100%
            red: 9%
            green: 86.7%
            blue: 92.2%
        - delay: 1s
        - switch.template.publish:
            id: vivid_cyan
            state: Off 

  - platform: template                   
    name: "Vivid yellow"
    icon: 'mdi:lightbulb-group-outline'
    id: vivid_yellow
    optimistic: true
    turn_on_action:
      then:
        - light.turn_on:
            id: blight
            brightness: 100%
            red: 90.6%
            green: 100%
            blue: 0.4%
        - delay: 1s
        - switch.template.publish:
            id: vivid_yellow
            state: Off 



    
################################################################################    
text_sensor:
  - platform: wifi_info
    ssid: 
      name: "Sonoff-b1 ssid"
    ip_address:
      name: "Sonoff-b1 IP Address"    


  - platform: sun
    name: Sunrise
    id: sunrise_sonoffb1
    type: sunrise
  - platform: sun
    name: Sunset
    id: sunset_sonoffb1
    type: sunset 

switch id: daynight_sonoffb1 doors the ability to enable and disable automation.

Thanks for that reboot_timeout. That might have been the issue why it wasnt working in my case but I am not entirely sure. I have now put in that line.

Thanks lordzid.

Am I understanding correctly that is running the script every 5 seconds? If so, I don’t quite understand why?

Thanks mate. If I am going to run without wifi, will the SNTP part still work?

I will give an example: the sunset has come. the light should turn on. power outage for some reason and then appeared. your condition will not be met.

Sntp will work until the power is turned off. after turning on, you need to synchronize the clock. There are separate modules for this.

Do you mean that the 5seconds time delay will happen everytime before the switching on of the GPIO?

so in my case if i wanted to switch on the GPIO at 5pm everyday, then this is telling the ESP8266 to wait 5 seconds first before switching on?

so what happens if my ESP8266 has a power failure, it won’t continue to boot because it is trying to connect to internet to synchronize the clock?

no, the device will turn on and everything will work, but no time-related automations will be performed

sorry not sure what you are referring to…are u referring to nothing will work because it cannot connect to the internet to synchronize the clock or are you referring to the 5 seconds delay?

Getting more confused.

I mean: if the Internet is disconnected, there will be no time synchronization and the conditions will not be met. when there is synchronization, every 5 seconds a check for conditions is performed and everything will work

I see! I understand now.

in my case i wanted the esp8266 to switch on automatically at 5pm and off at 8am even when there is no internet and no wifi. What should i do?

you will need This is a module And customize it. and make code.

Thanks. I will check out the GPS module - although I am not sure if my ESP8266 supports GPS.