Skip to content

Instantly share code, notes, and snippets.

@xbmcnut
Last active May 20, 2024 01:01
Show Gist options
  • Save xbmcnut/6d239de9523246d7a831ca55be6564c2 to your computer and use it in GitHub Desktop.
Save xbmcnut/6d239de9523246d7a831ca55be6564c2 to your computer and use it in GitHub Desktop.
Making an outdoor motion sensor smart using a Shelly 1
/**
* I figured out you can fit a Shelly 1 inside the case of many outdoor motion sensors.
* Coupled with that fact that recently, Shelly added a feature to de-couple the switch from the relay
* so they act independently, makes this a great combo! Simply wire the output of the PIR motion sensor
* (the wire that normally goes to your outdoor light) to the switch input of the Shelly and wire
* the lightbulb to the L output of the Shelly. Then, in the Shelly app or directly via the devices webpage,
* select the button type as 'Detached Switch' and use the code below to make it work normally.
* Don't forget to adjust the daylight sensitivity to full daylight if you want the motion detection to work 24/7.
*/
#### AUTOMATIONS ####
automation:
- alias: Shelly PIR Auto
# initial_state: true
# hide_entity: true
trigger:
- platform: state
entity_id: sensor.pir_motion_sensor_status
condition:
condition: or
conditions:
- condition: sun
before: sunrise
- condition: sun
after: sunset
action:
- service_template: "{% if is_state('sensor.pir_motion_sensor_status' , '1')
%} light.turn_on {% else %}
light.turn_off {% endif %}"
entity_id: light.pir_motion_sensor
light:
- platform: mqtt
name: "PIR Motion Sensor"
state_topic: "shellies/shelly1-0x0x0x/relay/0"
command_topic: "shellies/shelly1-0x0x0x/relay/0/command"
availability_topic: shellies/shelly1-0x0x0x/online
payload_available: "true"
payload_not_available: "false"
qos: 2
payload_on: "on"
payload_off: "off"
retain: false
optimistic: false
## SENSORS ##
sensor:
#### Status of Shelly switch input for PIR ###
- platform: mqtt
name: "PIR Motion Sensor Status"
state_topic: "shellies/shelly1-0x0x0x/input/0"
@BenHarris
Copy link

Posting my question here since you seem to know your stuff. I have a similar setup, output of motion sensor to switch input on Shelly, but my Shelly keeps on detecting 'on' probably because there is still a small voltage on the motion sensor's output, but high enough for Shelly to detect it as 'on'... Any tips?

The 330kΩ resistors resolved the issue in my case (see my reply above). For reference, these are the once I bought: https://www.ebay.co.uk/itm/252024737412. I soldered one inline and stuck some heat shrink over it. Been working perfectly for 18 months now.

@xbmcnut
Copy link
Author

xbmcnut commented Jun 8, 2023

@BenHarris Thanks for chiming in. That is what I was going to suggest. Appreciate the input. Actually might need to do that to mine as it's been a bit erratic lately too!

@craigcurtin-dev
Copy link

If you have a device that supports Shelly Scripts (I'm using a Shelly 1 Plus), you can use this setup, but run the automation locally (basic script below). This has the benefit that it will work as expected even if the wifi drops. You can obviously still control the switch and get the PIR state in Home Assistant. I bought a Sebson IR_OUT_E on Amazon, and mine didn't work initially, although sticking a 330kΩ resistor between the switched live and SW port on the shelly sorted it.

Shelly script:

 Shelly.addEventHandler(
    function (event, ud) {
        if((event.info.state || false) && event.component === 'input:0'){
            // PIR Triggered
            Shelly.call('Switch.Set', {id: '0', on: true});
        }
    },
    null
);

I then use an auto-off timer on the Shelly, which means I can configure it without having to turn the dial on the PIR sensor.

@BenHarris Just wanted to confirm this before i go ahead and do something similar - i have a couple of mains powered PIR units and there is a continual voltage output from them to the Shelly (i have flashed with Tasmota) - are you saying you just put the Resistor in between the (supposedly !) switched live output of the PIR and where it goes into the SW1 input on the Shelly ? i.e. wired it inline with the cable and then put the input to the SW1 input of the shelly ?

Regards

Craig

@michamichamicha
Copy link

@BenHarris Just wanted to confirm this before i go ahead and do something similar - i have a couple of mains powered PIR units and there is a continual voltage output from them to the Shelly (i have flashed with Tasmota) - are you saying you just put the Resistor in between the (supposedly !) switched live output of the PIR and where it goes into the SW1 input on the Shelly ? i.e. wired it inline with the cable and then put the input to the SW1 input of the shelly ?

Regards

Craig

I tried this (the inline method) with various resistor values but no luck: Shelly never detects the signal, so was thinking the resistance was too high, but lowering it did also not help. Really looking forward to a solution where one could measure something and calculate what resistor values would make sense.

@xbmcnut
Copy link
Author

xbmcnut commented Jul 6, 2023

@michamichamicha There is a bit of chatter at the beginning of this thread about adding another relay between the motion sensor and the Shelly input. It seems the Shelly SW input is 'floating' as indicated by this post here. Mine was working flawlessly in the early days but in the last 12 months, it triggers randomly all the time so I'm going to remove it and debug and I'll report the findings here.

https://community.home-assistant.io/t/shelly-1-25vac-on-input-motion-sensor-attached-gives-100-vac/100943

@craigcurtin-dev
Copy link

craigcurtin-dev commented Jul 6, 2023 via email

@xbmcnut
Copy link
Author

xbmcnut commented Jul 6, 2023

For reference, I just popped a Shelly 1 PM on the bench with just a 230V AC supply and unloaded, the SW input measures 126V AC.

@craigcurtin-dev
Copy link

craigcurtin-dev commented Jul 7, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment