Making a xiaomi PIR sensors a super sensor

Hmm , still don’t get it…

Setting the state to “off” that’s indeed rest command, not hardware… It’s just setting a state … No big deal here…
So when state is off… The new “motion detected” is coming from the xiaomi, to deconz to, home assistant setting the binary state to “on” again… So in my opinion that’s the deconz hardware that’s sending the new event… So that’s hardware (conbee) related?

Or am I wrong?

Think you are wrong. I tried to see the zigbee2mqtt logs to identify the sensor activity. When automation from homeassistant is sent to force “off state”, no logs can be seen in zigbe2mqtt.
Besides, the author said that this code worked with conbee and was not hardware related…

Hmm, but the motion should come from somewhere… Normally it can only be triggered by an event from the hardware…

Ok need to check again … I used another command to set the state to off, didn’t use that same rest command. … I use a curl

But if I used my command, the state was also to off…

But I was only be able to send a new motion after 40 seconds , not after 6…
If I could use 6 , then this is a huge improvement!!

I can trigger motion again after 6 seconds. I used rest_command like it was in github code. I was wondering about the state, because after 6 seconds the motion is not “clear” but is “offline” (direct translate). But works like a charm.

ok, can you share that rest command again, video is offline, i created below, allthough dont remember anymore exactlu
i tried below, but get invalid authentication error


rest_command:
  wc_onder_off:
    url: https://localhost:8123/api/states/binary_sensor.xiaomi_sensor_1
    method: POST
    headers: 
      authorization: !secret bearer
      content-type: 'application/json'
    payload: '{"state":"off"}'
    verify_ssl: false




2020-02-19 19:16:56 WARNING (MainThread) [homeassistant.components.http.ban] Login attempt or request with invalid authentication from 127.0.0.1
2020-02-19 19:16:56 WARNING (MainThread) [homeassistant.components.rest_command] Error. Url: https://localhost:8123/api/states/binary_sensor.xiaomi_sensor_1. Status code 401.
2020-02-19 19:16:58 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_track_state_change.<locals>.state_change_listener(<Event state_...ew_state=None>) at /usr/src/homeassistant/homeassistant/helpers/event.py:81
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 103, in state_change_listener
    event.data.get("new_state"),
  File "/usr/src/homeassistant/homeassistant/core.py", line 364, in async_run_job
    target(*args)
  File "/usr/src/homeassistant/homeassistant/components/automation/state.py", line 92, in state_automation_listener
    call_action()
  File "/usr/src/homeassistant/homeassistant/components/automation/state.py", line 78, in call_action
    context=to_s.context,
AttributeError: 'NoneType' object has no attribute 'context'

There it is:

rest_command_sensor:
    url: http://[IP_ADRESS]:8123/api/states/binary_sensor.movimento
    method: 'POST'
    headers:
      authorization: Bearer [TOKEN]
      content_type: "application/json"
    payload: '{"state": "off"}'

You have authentication problem. First I had the same. You need to write “Bearer” before the code of token

yeah, i noticed , i was already changing :slight_smile:

ok, now it works, it was indeed the word “Bearer”
but need to be home to test it offcourse :slight_smile:

allthough i think my end result stays the same
if i change with a rest command or a curl, state is in both cases off anyway

this is also correct , right? my current automation

- alias: Motion WC Onder On
  initial_state: 'on'
  trigger:
  - entity_id: binary_sensor.xiaomi_sensor_1 
    platform: state
    from: 'off'
    to: 'on'
  action:
  - service: light.turn_on
    entity_id: light.wc
  - delay: '00:00:6'
  - service: rest_command.wc_onder_off

- alias: Motion WC Onder Off
  initial_state: 'on'
  trigger:
  - entity_id: binary_sensor.xiaomi_sensor_1 
    platform: state
    from: 'on'
    to: 'off'
    for:
      seconds: 60   
  action:
  - service: light.turn_off
    entity_id: light.wc

No. You need to have an automation to send off state after X seconds. After that you can do another automation to turn on lights.
See the automation of an example:

- id: '1582110183955'
  alias: Teste
  trigger:
  - platform: state
    entity_id: binary_sensor.movimento
    to: 'on'
  condition: []
  action:
  - delay: '00:00:06'
  - service: rest_command.testesensor

After that I would do 2 automations, one to turn on light after sensor ON, and another to turn off light when sensor OFF for x seconds

yes, thats what i do
in my first automation, when motion is detected , so state from off to on, then i turn on the light, and i turn off the binary sensor to off after 6 sec

my second automation i turn my light off, when motion sensor goes to off, but with a delay of 60 secs offcourse … otherwise my light would go already off after 6 seconds

so in my opinion thats a good automation? it should work like that normally

Hi everyone,

That’s my video featured here. I took it down after a day as it doesn’t work as well as I’d hoped. :slightly_frowning_face:

While the rest_command and automation part is good - I’ve since found it only works for a short time. :flushed:

Sorry for the confusion

1 Like

That’s a real bummer… But what’s the issue then? Why should it not work more then a day?

No, I took the video down after a day LOL

The issue is that when you first start or pair a sensor it does in-fact re-trigger after 5-6 seconds but, then reverts back to 1 min after about 30 minutes… I didn’t test it for long enough.

So, unless there’s a way to re-pair the sensors (every 20-30 minutes) in software, I can’t see a ‘fix’ coming soon.

Ahhh, that’s why it only worked for me, my new motion was only triggered again after 60 seconds… Even when I put the state to off…

But I thinks the code is still an improvement, because I think the default timeout in deconz is 120 sec

But in your video you didn’t do a pairing process

Aww thanks… At least I did post something useful after-all - My API calling code LOL

Pairing was done off-camera, I didn’t realise it was necessary at the time :frowning:

In my next video, that’ll be up later today, I show how to use a SONOFF Basic to control an ordinary home light fitting, all while keeping the wall switch working as normal.

With your motion sensor and an automation (off an off timer) you’ll be able to use that for your bathroom light and still be able to turn off the light when you leave.

2 Likes

ok, thnx for the input