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"
@TomBa01
Copy link

TomBa01 commented Nov 10, 2020

Issue Solved! After everything hinted towards it... - and you also guessed that 'If your PIR does not use a relay internally and uses a triac or something similar, the Shelly may not like that.' - i found out that there is an entry in the Shelly User Forum that shows Supported PIRs and Non Supported PIRs.
Here is the link: https://www.shelly-support.eu/forum/index.php?thread/581-shelly1-compatible-pir-sensors/&postID=5347#post5347

I thought gave it a try, bought the cheapest PIR Sensor in my local hardware store (REV Ritter AP 180 ° Motion Detector, White, 0075184112), hooked it up and - it worked exactly as you described it!

Wanted to say thanks for your support on that matter, i guess if anything doesn't work like in your description its most likely a non-supported PIR Sensor.

Ever thought of adding a physical toggle (on off) switch to your setup? Not sure how that might interfere or disturb the Home Assistant Automation.. but i think it could be of advantage in some cases. My initial setup was a PIR Light and physical switch... without Home assistant... but maybe a second shelly is needed to make this work with a physical switch? For instance an input from a physical switch turns your outside lights on for good or for a specific preset time before it returns back to PIR and automation.... - Anyhow, just an idea... Thanks again!

@TomBa01
Copy link

TomBa01 commented Nov 11, 2020

What benefit is it to have a light: mqtt (your code line 33-45)? The shelly1 is a switch entity already that can turn on the light... The guy here didnt use the mqtt:light entity.... https://cyan-automation.medium.com/improving-a-motion-detecting-led-light-with-shelly-home-assistant-7c0902698f54 Any benefit doing this like you did it?

@xbmcnut
Copy link
Author

xbmcnut commented Nov 17, 2020

@TomBa01 Defining it as a MQTT light makes it appear in HA as a light, not a switch with a lightbulb icon by default but it's entirely up to you.

@Yonny24
Copy link

Yonny24 commented Dec 15, 2020

Nice project. Set it up last night as I have 2 of these PIRs.

But bit confused by the detached button setting. If this is set how do I get an update of the PIR activating?
I have an odd deviceid under the settings in the cloud : 68c63af9789f (115200602244567).
Why the brackets?

At the moment I do not have any load(light), just looking for the notifications. So the O output is not attached to anything, everything else is wired up as you described.

Ideally yes I want a notification from the shelly but without the realyturning on and off all the time. What am I missing? Possibly the ID is wrong in my config.

Currently I have the shelly switch configured in nodered - so everytime the realy comes on I get the alert.

Thanks!

@xbmcnut
Copy link
Author

xbmcnut commented Dec 16, 2020

@Yonny24. Thanks. How have you got your stock Shelly coming into HA; manual MQTT or with the integration? I read a release note somewhere in the last 7 days for either supervisor, core or the Shelly integration that mentioned that the detached switch has only just been added so you'll need to make sure everything is up to date to check.

If you use MQTT Explorer, you can watch the topics coming from your stock Shelly assuming MQTT is all set up on it to see if your getting a 0/1 on shellies/shelly1-0x0x0x/input/#. In regards your Tasmota version, there is some info here in regards to detaching the switch from the relay https://tasmota.github.io/docs/Buttons-and-Switches/#detach-switches-based-on

@Yonny24
Copy link

Yonny24 commented Dec 16, 2020

@Yonny24. Thanks. How have you got your stock Shelly coming into HA; manual MQTT or with the integration? I read a release note somewhere in the last 7 days for either supervisor, core or the Shelly integration that mentioned that the detached switch has only just been added so you'll need to make sure everything is up to date to check.

If you use MQTT Explorer, you can watch the topics coming from your stock Shelly assuming MQTT is all set up on it to see if your getting a 0/1 on shellies/shelly1-0x0x0x/input/#. In regards your Tasmota version, there is some info here in regards to detaching the switch from the relay https://tasmota.github.io/docs/Buttons-and-Switches/#detach-switches-based-on

Hi Pete,

I've tried both. I have the integration and tried manual mqtt in the switches yml.
I'm using mqtt explorer also but nothing coming in on explorer from the shelly, only if I publish manually.

I am on 0.188.5 version so maybe I need to upgrade to their new release.

Thanks I'll give that a go.

@Yonny24
Copy link

Yonny24 commented Dec 16, 2020

I ended up flashing it with tasmota. Now just got to find the detached relay option in tasmota.
setoption114 looked hopeful but I can still here the relay clicking

@4romany
Copy link

4romany commented Feb 7, 2021

TASMOTA CONFIGURATION - VERSION 9.2*

You need 3 CONSOLE commands:

Enabling setoption114 detaches the relay.  Until you do this the PIR will cause the light to turn on.
    Enabling switchmode 15.  Tasmota doc says "Send only MQTT message on switch change".  When this is set 
    you will immediately receive these messages when the PIR is tripped and resets:
	
	tele/shelly-deck-window/SENSOR = {"Time":"2021-02-07T02:58:32","Switch1":"ON"}
	tele/shelly-deck-window/SENSOR = {"Time":"2021-02-07T02:58:32","Switch1":"OFF"}
	
Disable MQTT autodiscovery from HA with so19 0. in tasmota - by default it normally disabled.  You don't want  Mosquitto broker
automatically discovering the device - it needs to be manually defined in configuration.yaml.

COMMANDS:

setoption114 1
switchmode 15
setoption19 0

configuration.yaml configuration:

switch:

  • platform: mqtt
    name: "shelly-deck-window"
    state_topic: stat/shelly-deck-window/POWER
    payload_on: "ON"
    payload_off: "OFF"
    command_topic: cmnd/shelly-deck-window/POWER
    qos: 2
    retain: false
    optimistic: false

sensor:

  • platform: mqtt
    name: "shelly-deck-window-sensor"
    state_topic: tele/shelly-deck-window/SENSOR
    value_template: "{{ value_json.Switch1 }}"

This will create 2 entities:

sensor.shelly_deck_window_sensor
switch.shelly_deck_window

The sensor values will be OFF or ON - so that what you would trigger on in your automation...
I could never have figured this stuff out without using MQTT EXPLORER.

For the 3 entries for QOS, RETAIN, and OPTIMISTIC in the above configuration - they are there simply because I saw them in
similar setups.

Hope this helps someone.

@Yonny24
Copy link

Yonny24 commented Jun 1, 2021

TASMOTA CONFIGURATION - VERSION 9.2*

You need 3 CONSOLE commands:

Enabling setoption114 detaches the relay.  Until you do this the PIR will cause the light to turn on.
    Enabling switchmode 15.  Tasmota doc says "Send only MQTT message on switch change".  When this is set 
    you will immediately receive these messages when the PIR is tripped and resets:
	
	tele/shelly-deck-window/SENSOR = {"Time":"2021-02-07T02:58:32","Switch1":"ON"}
	tele/shelly-deck-window/SENSOR = {"Time":"2021-02-07T02:58:32","Switch1":"OFF"}
	
Disable MQTT autodiscovery from HA with so19 0. in tasmota - by default it normally disabled.  You don't want  Mosquitto broker
automatically discovering the device - it needs to be manually defined in configuration.yaml.

COMMANDS:

setoption114 1
switchmode 15
setoption19 0

configuration.yaml configuration:

switch:

* platform: mqtt
  name: "shelly-deck-window"
  state_topic: stat/shelly-deck-window/POWER
  payload_on: "ON"
  payload_off: "OFF"
  command_topic: cmnd/shelly-deck-window/POWER
  qos: 2
  retain: false
  optimistic: false

sensor:

* platform: mqtt
  name: "shelly-deck-window-sensor"
  state_topic: tele/shelly-deck-window/SENSOR
  value_template: "{{ value_json.Switch1 }}"

This will create 2 entities:

sensor.shelly_deck_window_sensor
switch.shelly_deck_window

The sensor values will be OFF or ON - so that what you would trigger on in your automation...
I could never have figured this stuff out without using MQTT EXPLORER.

For the 3 entries for QOS, RETAIN, and OPTIMISTIC in the above configuration - they are there simply because I saw them in
similar setups.

Hope this helps someone.

ahh is this how you get the mqtt message but not trigger the relay? I've been struggling with this.
Always hear the relay click. I've had mine set to 'setoption114 1'
So I need the 3 commands in sequence to disable the relay?

But I'm still on version 9.1.0(tasmota). Needs upgrading first I assume.
Just updated to 9.4.0(tasmota) OTA

So with the new 3 commands, it will output : '"Switch1":"ON"' and I need to explore this topic now instead of '"Action":"ON"'
and this will not switch the relay?
I need to go out and test it.

Thanks

@Yonny24
Copy link

Yonny24 commented Jun 1, 2021

@4romany
Still clicks. Now I'm wondering if this is a click from the stock motion sensor or shelly. Don't recall if the original makes a clicking sound.
Most definitely the shelly relay clicking. I'm out of ideas of how to disable the relay, maybe not possible?

tons of activity when people are in the garden:

13:15:24.243 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:16:03.141 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:16:20.772 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:16:36.739 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:17:04.943 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:17:37.976 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:17:47.787 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:19:01.358 MQT: tele/tasmota_F9599F/SENSOR = {"Time":"2021-06-02T13:19:01","Switch1":"OFF"} 13:19:37.557 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:19:47.379 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:19:54.106 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:20:13.149 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:20:28.842 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:20:38.658 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:20:52.395 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:21:02.221 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}} 13:21:08.258 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"ON"}} 13:21:18.086 MQT: stat/tasmota_F9599F/RESULT = {"Switch1":{"Action":"OFF"}}

@xbmcnut
Copy link
Author

xbmcnut commented Jun 17, 2021

@Yonny24 I can't comment as there are so many options with Tasmota that seem to change with nearly every upgrade. My demo was done with Shelly stock firmware and has never missed a beat, especially now the Shellies are natively integrated to HA so MQTT is no longer required.

@zierbeek
Copy link

zierbeek commented Aug 6, 2021

Hi! just a small question. Why did you make your motion sensor smart? To track activity?

@xbmcnut
Copy link
Author

xbmcnut commented Sep 4, 2021

@zierbeek Sorry for the delayed reply. I added a Shelly so I could get 24/7 motion events from an outdoor mains powered sensor. Is this way, I can use it as an extension to both my alarm system and surveillance system. So when the alarm is set, if this outdoor PIR triggers, it means someone is hovering around outside my house so I get HA to notify me.

@BenHarris
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.

@xbmcnut
Copy link
Author

xbmcnut commented Dec 8, 2021

@BenHarris Thank you so much for your input. That's very helpful.

@Deblen
Copy link

Deblen commented Apr 18, 2022

Hi,
I have been trying to get the above working with Shelly1 and Home Assistant. As per fist post
I can see in HA, PIR Motion Sensor, Sensors shows as input = Off, PIR trigger's it then shows On.
Light does not come ON.
On Shelly screen the switch is all off,when PIR is triggered, little bar lights up, still no light.
If I click the button,light comes on, and stays on.

Any help appreciatted. Deblen

@xbmcnut
Copy link
Author

xbmcnut commented Apr 18, 2022

@Deblen Did you detach the switch input from the relay using the Shelly's webpage? Have you got the Shelly in HA using MQTT or with the Shelly integration? Can you control the relay (light) in HA?

@Deblen
Copy link

Deblen commented Apr 18, 2022

Hi Thanks for the quick reply.
Detached via Shelly,
Using Shelly in HA using MQTT
No cannot control the light,
I can hear the PIR click.
The only way light turns on is clicking the button on the actual shelly, circle turns blue.
If I trigger pir, the little line in the circle will turn on,
Deblen

@xbmcnut
Copy link
Author

xbmcnut commented Apr 19, 2022

@Deblen Seems there is something wrong with your MQTT YAML if you can't control the relay from HA? Without that, nothing is going to work. I'd be tempted to migrate everything to the Shelly integration as that works really well and is just a whole lot easier as no YAML is required to get the device into HA.

@Deblen
Copy link

Deblen commented Apr 21, 2022

Thanks, will try Shelly Intergration.
Deblen

@Deblen
Copy link

Deblen commented Apr 26, 2022

Hi, I could not find how to do it via Shelly.
I then decided to try the post from 4romany, above.
I was able to get it to work.
Deblen

@michamichamicha
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?

@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