433toMQTTto433 and one code devices

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?

Hi @clyra, you could use something like this

It uses only the ‘on’ mqtt payload, like your “one code device”.
You can delete the condition in the automation, it’s only for a light sensor.
If you change the light i’m switching with a binary sensor, you could use

for the notification.

Hi @VDRainer.

I was not aware of the alarm! I’m sure i will find a place to use it :-). If I got it right, the script part is doing the timer rigth? If you call it before it ends it kills the timer script and start it again?

Yes, you’re right.

Hi. The scripts are working. Thanks for the hint. Now… Is there a way to make this even better? For now I have two scripts for each sensor, but I was thinking if there’s a way to have just one set of scripts and pass the sensor as a parameter. How should we identify the correct instance of the timer? And, is it possible to have more than one instance of the same script running?