Hi. I just built a 433toMQTT gateway and I’m using it with a couple of motion detector devices. My devices just send a code when motion is detect, so if you just configure them as binary_sensors, they will never turn off. On 433toMQTTto433 Bidirectional Arduino Gateway @masterkenobi posted a simple automation to turn those devices off after some seconds.
It worked fine, but I want something more. If you still have movement on the room you keep receiving messages at 10 seconds interval. What I want is to be notified once a motion is detected and have the sensor to turn off only if no more motion is detected in, say, a 50s interval. So, the trick part is how to start a timer and have it reseted if motion is detected before it expires? I guess that the best solution is to write a appdaemon to do that… In the meantime, I got this working with the configuration below:
First, create the “real” device and “fake” one. The “fake” one is the one with the “timer”.
- platform: mqtt
state_topic: "home/433toMQTT"
name: "realdevice"
payload_on: "<device_code>"
payload_off: "<device_code>off"
device_class: motion
- platform: mqtt
state_topic: "home/fakedevice/motion"
name: "fakedevice"
payload_on: "1"
payload_off: "0"
device_class: motion
this automation turn on the fake device and turn off the real one:
alias: realdevice
trigger:
platform: state
entity_id: binary_sensor.realdevice
to: 'on'
action:
- service: mqtt.publish
data:
topic: 'home/fakedevice/motion'
payload: '1'
retain: 'true'
- service: mqtt.publish
data:
topic: 'home/433toMQTT'
payload: '<device_code>off'
retain: 'true'
This automation send me the notification:
alias: 'send notification'
trigger:
platform: state
entity_id: binary_sensor.fakedevice
to: 'on'
action:
service: notify.you
data:
message: "motion detected on fakedevice!"
Now the tricky part. This automation turn off the fake device if no motion was detected on the last 50 seconds:
alias: turn off fake device
trigger:
platform: time
minutes: '/1'
seconds: 00
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.fakedevice
state: 'on'
- condition: template
value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.realdevice.attributes.last_triggered)) > 50 }}'
action:
service: mqtt.publish
data:
topic: 'home/fakedevice/motion'
payload: '0'
retain: 'true'
Is there a better way to do that?