Australian Light switch with motion sensor local control - show and tell

Hi all,

I’m from Australia and have these DETA Grid Connect Smart Switchs that I replaced the stock chip with an ESP into and got working with esphome! :partying_face:

They no longer come with an esp8266 however it is easy enough to swap for an esp as it has the same pinout.

Video explains how to replace chip
Reference 1
Reference 2

I also individually wired up the leds for the buttons as outputs, as before they were tied to the light status.

I also managed to fit in a motion sensor and have that as an input to one of the ESP pins.

This is the config I landed with (exposes 3 lights and PIR + status led to HA) - click to view
substitutions:
  device_name: deta3gangswitch
  friendly_name: "3 Gang Switch"
  device_ip: 192.168.1.100

#################################

esphome:
  platform: ESP8266
  board: esp12e
  name: ${device_name}
  esp8266_restore_from_flash: true

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: ${device_ip}
    gateway: 192.168.1.1
    subnet: 255.255.255.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "ESPHOME"
    password: "12345678"

api:
  password: !secret api_password

ota:
  password: !secret ota_password

logger:

# The web_server & sensor components can be removed without affecting core functionaility.
web_server:
  port: 80

sensor:
  - platform: wifi_signal
    name: ${device_name} Wifi Signal Strength
    update_interval: 60s
  - platform: uptime
    name: ${device_name} Uptime

#################################

# status_led:
#   pin:
#     number: GPIO0
#     inverted: True
output:
  # 1st button
  - platform: gpio
    pin: GPIO5
    id: relay1

  # 2nd button
  - platform: gpio
    pin: GPIO14
    id: relay2

  # 3rd button
  - platform: gpio
    pin: GPIO12
    id: relay3
    
  # 1st button led
  - platform: esp8266_pwm
    pin: GPIO1
    id: led1

  # 2nd button led
  - platform: esp8266_pwm
    pin: GPIO13
    id: led3

  # 3rd button led
  - platform: esp8266_pwm
    pin: GPIO15
    id: led2

light:
  # Status LED
  - platform: status_led
    name: "Status Led"
    pin:
      number: GPIO0
      inverted: True
    id: statusLed
    
  # 1st button
  - platform: binary
    name: "${friendly_name} 1st"
    output: relay1
    id: light1
    on_turn_on:
    - light.turn_on:
        id: light_led1
    on_turn_off:
    - light.turn_off:
        id: light_led1

  # 2nd button
  - platform: binary
    name: "${friendly_name} 2nd"
    output: relay2
    id: light2
    on_turn_on:
    - light.turn_on:
        id: light_led2
    on_turn_off:
    - light.turn_off:
        id: light_led2

  # 3rd button
  - platform: binary
    name: "${friendly_name} 3rd"
    output: relay3
    id: light3
    on_turn_on:
    - light.turn_on:
        id: light_led3
    on_turn_off:
    - light.turn_off:
        id: light_led3

  # 1st led
  - platform: monochromatic
    name: "${friendly_name} 1st led"
    output: led1
    id: light_led1
    default_transition_length: 0s
    effects:
      - pulse:
      - 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
      - pulse:
          name: "Fast Strobe"
          transition_length: 0s      # defaults to 1s
          update_interval: 0.5s
      - pulse:
          name: "Slow Strobe"
          transition_length: 0s      # defaults to 1s
          update_interval: 1s


  # 2nd led
  - platform: monochromatic
    name: "${friendly_name} 2nd led"
    output: led2
    id: light_led2
    default_transition_length: 0s
    effects:
      - pulse:
      - 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
      - pulse:
          name: "Fast Strobe"
          transition_length: 0s      # defaults to 1s
          update_interval: 0.5s
      - pulse:
          name: "Slow Strobe"
          transition_length: 0s      # defaults to 1s
          update_interval: 1s

  # 3rd led
  - platform: monochromatic
    name: "${friendly_name} 3rd led"
    output: led3
    id: light_led3
    default_transition_length: 0s
    effects:
      - pulse:
      - 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
      - pulse:
          name: "Fast Strobe"
          transition_length: 0s      # defaults to 1s
          update_interval: 0.5s
      - pulse:
          name: "Slow Strobe"
          transition_length: 0s      # defaults to 1s
          update_interval: 1s


# Buttons
binary_sensor:
  # 1st button
  - platform: gpio
    pin:
      number: GPIO16
      mode: INPUT
      inverted: True
    name: "${friendly_name} 1st Button"
    #toggle relay on push
    on_press:
        - light.toggle: light1

  # 2nd button
  - platform: gpio
    pin:
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: True
    name: "${friendly_name} 2nd Button"
    #toggle relay on push
    on_press:
      - light.toggle: light2

  # 3rd button
  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
      inverted: True
    name: "${friendly_name} 3rd Button"
    #toggle relay on push
    on_press:
      - light.toggle: light3
    
  # Motion PIR
  - platform: gpio
    pin: 
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: True
    name: "PIR Sensor"
    device_class: motion
    on_press:
      then:
        - light.turn_on: statusLed
    on_release:
      then:
        - light.turn_off: statusLed

switch:
  - platform: restart
    name: "${friendly_name} REBOOT"

I have always wanted smart lights but have been looking for a solution that:

  • Is an Australian approved product
  • Has a motion sensor to auto off after X time
  • Ability to change modes on the switch between automatic and manual
  • Has an LED per switch that can be dimmable / flash (ie not attached to light output) to show current mode.
  • Connects to Home Assistant

I think with the above hardware + included mods and esphome I finally found a good solution.
(Better solution would be above product if it came with a motion sensor inbuilt :grinning_face_with_smiling_eyes:)

Now to the meat of my post, I need help deciding on the logic for automatic/manual of the switch.
I have developed the below flow chart which I think captures all the modes.

The LED in the flowchart refers to the blue led on the switch.
The Light refers to the light (load) connected to the switch.

esphome_motion_light.drawio
Diagram (click for larger image)

I would ideally like to keep the logic local (in esphome automations) and not rely on Home Assistant (node red) for the local switch logic.

I would however like to have HA change the mode status (ie turn the light on / off / place it into off_Standby etc)

Some things I need to figure out:

  1. What kind of data type the main logic (off_Standby,on_Standby,Off_Manual,On_Manual). I am thinking of using the text_sensor in esphome. I code in typescript and would usually reach for an enum here.

  2. How will I go about letting HA change the modes? (Node red). I would like to have the traditional light (on/off) entity in HA on the frontend, somehow mapped to the above modes. Thinking via a proxy sensor in the esphome config that will simply trigger different modes based on on/off action from HA. And maybe via node red too, For example if its daytime put it into Off_Manual, when it gets to night place it into off_Standby).

  3. I would like to use motion sensors from other switches to be an input to the logic of a switch. (ie a 2 way switch at two ends of a hall, where any end has motion to turn on the hallway light).
    I think I will expose the motion sensor via the normal binary_sensor to HA, then on the switch config using the Home Assistant Sensor to get status of other motion sensors in other switches.

What do you think?
Has anyone looked at achieving something similar?
How should I go about coding this logic in the esphome config; automations/lambda? (Don’t need someone to do it for me, just general tips/ideas of approach - I have’t got to this step myself yet)

Just wanted to get my ideas fleshed out before writing up the config.

4 Likes

Firstly nice work!!.. I am in Australia too we have strict electrical laws… you really need a approved device… I know you done a lot of work here and spent a few $$ but just a quick suggestion. Have a look at the shelly, we have used them and it will do what you need out of the box. Then just purchase any dumb switch and your good to go (LED per switch… to show mode… not sure what your doing here or why :grinning: ). I would then just use automations to do the rest

Below is a link to one but they have many options including a motion sensor (my motion sensors are on ESP32)

https://shelly.cloud/shelly-plus-1/

your questions and I note you just looking for ideas not code or how to :+1:

1/ HA automation
2/ Just a simple light switch code + HA automation
3/ I have motion sensors on ESP32 and then I just link it in a HA automation to do what you would like.

Hi all.

Just posting an update regarding this project.

I have designed a custom PCB that holds the PIR sensor and fits exactly into the space of the original PCB cutout. Due to this i have had to swap to an ESP-07S with external wi-fi antenna.

The custom pcb has mounting holes for the panansonic EKMC1607111 PIR sensor, which is small enough to work in this project. The pcb then also has a pull down 1206 resistor, a red led and resistor for debugging. It is attached onto the main pcb by glue.

I have the input from the PIR to a spare gpio on the esp and works well.




Regarding the code, I am using esphome-state-machine to manage all the states and model the (previous post) flowchart in code.

I have a basic (1 switch version / single gang) working on my bench, now to modularize the code for multiple switches / 3 gang version.

Regarding interfacing to HA, I think some switches such as in pantry / garage and closet would want to be on PIR motion activated and timeout mode, and some such as in bedrooms would want to be in manual mode - but still provide a PIR input to HA.
I am still in the process of how best to expose this device to HA, as the simple light.on / off would hijack the state machine on the device.

more to come once all coded and installed.

Nice, however i am a bit annoyed at motion sensors that require a battery. My project would be mains powered :slight_smile:
The local control of motion to activate lights at the switch is what I was going after here (local - as in even if hub goes offline - not just internet out)

But of course my mod you could just use the motion as an input to HA with no on device actions.

Yes I think I am coming to the realisation that I would want some switches to be HA controlled for things like day/night modes (bedroom), but others for pure motion activated at any time (think closets etc) is still useful to have on the local device.

Cheers for your thoughts :slight_smile: