How do you control your smart lights?

Sorry for late reply didn’t get notifcation

This is pasted directly there are some entities that need to be cleared from there

substitutions:
  devicename: shelly1_toilet
  upper_devicename: Shelly1 Toilet
  ip_address: 10.10.4.51
  sensor_prefix: esp
  
esphome:
  name: shelly1_toilet
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: on
  use_address: ${ip_address}
  manual_ip:
    static_ip: ${ip_address}
    gateway: 10.10.4.1
    subnet: 255.255.255.0

web_server:
  port: 80

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  safe_mode: True
switch:
  - platform: gpio
    pin: 4
    name: "Toilet"
    id: relay1
    restore_mode: RESTORE_DEFAULT_ON
  - platform: restart
    name: ${upper_devicename} Restart
    
binary_sensor:
  - platform: gpio
    pin:
      number: 5
    name: ${upper_devicename} SW

    on_multi_click:
    - timing:
        - ON for at most 0.3s
        - OFF for at most 0.3s
        - ON for at least 0.2s
      then:
        - logger.log: "Double Clicked"
        - switch.toggle: relay1
    - timing:
        - OFF for at most 0.3s
        - ON for at most 0.3s
        - OFF for at least 0.2s
      then:
        - logger.log: "Double Clicked"
        - switch.toggle: relay1
    - timing:
        - ON for at least 0.4s
      then:
        - script.execute: sw1_action
    - timing:
        - OFF for at least 0.4s
      then:
        - script.execute: sw1_action

  - platform: status
    name: ${sensor_prefix} ${upper_devicename} Status
    id: network_available
    on_state:
      then:
        - if:
            condition:
              lambda: 'return id(network_available).state;'
            then:
              - logger.log: "API connection available"
            else:
              - logger.log: "API connection NOT available"

sensor:
  - platform: uptime
    name: ${upper_devicename} Uptime
  - platform: wifi_signal
    name: ${upper_devicename} Signal
    update_interval: 300s    


text_sensor:
  - platform: homeassistant
    id: yeelight_available
    entity_id: binary_sensor.yeelight_toilet_available
  - platform: homeassistant
    id: light_toilet
    entity_id: light.toilet

script: 
  - id: sw1_action
    then:
      - logger.log: "SW1 single click"
      - if:
          condition:
            and:
              - lambda: 'return id(yeelight_available).state == "on";'
              - api.connected:
              - wifi.connected:
          then:
            - logger.log: "WiFi, hass API and yeelight is available"
            - if:
                condition:
                  switch.is_on: relay1
                then:
                  - logger.log: "Relay state is ON"
                  - if:
                      condition:
                        - lambda: 'return id(light_toilet).state == "on";'
                      then:
                        - logger.log: "Turning off light"
                        - homeassistant.service:
                            service: light.turn_off
                            data:
                              entity_id: light.toilet
                  - if:
                      condition:
                        - lambda: 'return id(light_toilet).state == "off";'
                      then:
                        - logger.log: "Turning on light"
                        - homeassistant.service:
                            service: scene.turn_on
                            data_template:
                              entity_id: >-
                                {% set full_light_time_start = now().strftime('%Y-%m-%d') + ' ' + '06:00:00.0000+10:00' %}
                                {% set full_light_time_end = now().strftime('%Y-%m-%d') + ' ' + '21:00:00.0000+10:00' %}
                                {% if as_timestamp(now()) >= as_timestamp(full_light_time_start) and as_timestamp(now()) <= as_timestamp(full_light_time_end) %} 
                                  scene.toilet_light_full
                                {% else %}
                                  scene.toilet_light_soft
                                {% endif %}
                else:
                  - logger.log: "Control relay is OFF, turning back on"
                  - switch.turn_on: relay1
          else:
            - logger.log: "No hass connection. Toggle relay state"
            - switch.toggle: relay1

6 Likes

Well i only use the switches to control lights, no more than 20-30W loads, i wouldn’t think of using them as socket control for a clothes dryer or AC, however the shelly1 is rated 16A and is the only one with dry contact. The input/output connectors are very tight together so prevent any crossing using crimp ferrules. More than that not sure what else can you do, they are already in the switch box, placing them inside another box would increase the size.

The latest ones 1pm and 2.5 versions as i understand have temp sensors inside, so you can deactivate them in case there are temp spikes or the load exceeds some threshold. This is with stock fw, with esphome you’ll have to check if the sensors are supported and craft your own automation

Looks like solution is just to use the Shelly 2.5 UL listed versions. Then at least you meet electrical code.

@subzero79 Thank you so much for the code.

I am working on a solution for a few of my lights.
What I want to happen is when they boot use the ‘esp8266_restore_from_flash: true’ function is ESP home.
However, I also want to run a ‘on_boot’ that runs a script to check if home assistant API is available. and if it’s not, to turn the lights on a certain brightness.

I tried this

esphome:
  name: ceiling_light_1
  platform: ESP8266
  board: esp01_1m
  esp8266_restore_from_flash: true
  on_boot:
    priority: -100
    then:
      - script.execute: boot

in combination with this script

script:
  - id: boot
    then:
      - logger.log: "API Check"
      - if:
          condition:
            and:
              - lambda: 'return id(ceiling_light_1_available).state == "on";'
          then:
            - logger.log: "HA is available"
          else:
            - logger.log: "HA is not available"
            - light.turn_on:
                id: me
                brightness: 75%
                white: 85%
                red: 15%

In an attempt to mimic you code but with no luck. I would appreciate any feedback.
Thanks,
Ctech

This should be an imported text sensor. But I see that unnecessary, because that light is “me” right?
There is a built in condition called api.connected you should maybe use that, also the “and” operator is unnecessary I think.
Also bear in mind that api connection is initiated by ha, not the device so in a reboot you have to wait for it to happen, not sure about this but it could be a minute maybe in worst case, before the device goes available (api connected).
Add a delay in the boot script see if that helps.

1 Like

Just in case this helps anybody in the UK regarding the lack of neutral wiring at the switch…

Broadlink TC2 switches work fine - I have been using them for years :slight_smile:

@subzero79
Perfect, I was trying to over complicate it for my situation. Got it working just how I wanted, and I also added in an interval command to check that API is still connected, and if not to fall back to the default mode. Having them turn on/change if API is disconnected is nice because it works as kind of an alert if there is an issue with my system. Thank you again for all your help and guidance.

For anyone that is interested here is the config for one of my lights:

esphome:
  name: ceiling_light_1
  platform: ESP8266
  board: esp01_1m
  esp8266_restore_from_flash: true
  on_boot:
    priority: -50
    then:
      - script.execute: failsafe

wifi:
  ssid: "<(o.O<)"
  password: "(>O.o)>"
  manual_ip:
    static_ip: 192.168.1.113
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1
    dns2: 192.168.1.1
    
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

light:
  - platform: rgbww
    id: me
    name: "Fan Light 1"
    red: output_component1
    green: output_component2
    blue: output_component3
    cold_white: output_component4
    warm_white: output_component5
    cold_white_color_temperature: 6536 K
    warm_white_color_temperature: 2000 K

output:
  - platform: esp8266_pwm
    id: output_component1
    pin: 4
  - platform: esp8266_pwm
    id: output_component2
    pin: 12
  - platform: esp8266_pwm
    id: output_component3
    pin: 14
  - platform: esp8266_pwm
    id: output_component4
    pin: 5
  - platform: esp8266_pwm
    id: output_component5
    pin: 13

interval:
  - interval: 5min
    then:
      - script.execute: failsafe

script:
  - id: failsafe
    then:
      - logger.log: "API Check.."
      - delay: 30s
      - if:
          condition:
            api.connected:
          then:
            - logger.log: "API Connected, all is well"
          else:
            - logger.log: "API Not Connected, defaulting light mode"
            - light.turn_on:
                id: me
                brightness: 75%
                white: 85%
                red: 15%