MQTT payload delay

Hello

Am trying to use a Motion sensor with a MQTT payload to to turn a light on and off

The payloads are open / closed

Open turn = on and closed = off

All is working however, I want to wait for 30 seconds of constant closed being Detected before turning the light off, as if you stop moving for a second currently the lights go off then back on

I have the below however clearly missing something or doing something wrong, any thoughts or suggestions would be appreciated.


alias: Kitchen LED on With PIR
description: ""
trigger:
  - platform: mqtt
    topic: Pir/kitchen/prio/inputs/read/1004
    payload: OPEN
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - action: light.turn_on
    metadata: {}
    data:
      rgb_color:
        - 255
        - 255
        - 255
      brightness_pct: 50
    target:
      entity_id: light.kitchen_led
      device_id: 2db98dc8a48ca34413b9f00ab5a76ae6
  - wait_for_trigger:
      - platform: mqtt
        topic: Pir/kitchen/prio/inputs/read/1004
        payload: CLOSED
    timeout:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
    continue_on_timeout: false
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id: 2db98dc8a48ca34413b9f00ab5a76ae6
mode: single

Thank you

Maybe you can use the HA default embedded Blueprint Motion-activated Light?
If you want change it by your needs then you can use the option Take control.
More Info HERE

I find blueprints helpful because you can learn a lot of how they do it… :wink:

I noticed you make use of a custom MQTT device. I use MQTT Discovery for this kind of things.
It’s very usefull for adding a MQTT device to Home Assistant because it instantly adds/change/deletes a device without the need to restart Home Assitant and when detected, it’s showing all the info/options you need, and complies with Home Assistant basics.

For MQTT moderating/add/delete/change I find MQTT Explorer very helpful.

In the past I used the HA Node-Red addon to add/change/delete MQTT dicovered devices.

The funtion-node (orange) contained this, as an example:

var logTimeStamp = global.get('logTimeStamp')||"No Timestamp";
var _topicBase = "homeassistant";
var _topicComponent = "sensor"
var _topicNodeId = "presence";
var _topicDeviceName = "detection";
var _topic = _topicBase + "/" + _topicComponent + "/" + _topicNodeId + "/" + _topicDeviceName
var _qos = 2;
var _retain = true;
var _payload = {
    "name": "Presence Detection",
    "icon": "hassio:eye-outline",
    "unique_id": "mqtt_presencedetection",
    "has_entity_name": "true",
    //"unit_of_measurement": " x",
    "node_id": _topicNodeId,
    "state_topic": _topic + "/state",
    //"command_topic": _topic + "/state",
    "device": {
        "name": "Mqtt Security",
        "manufacturer": "MQTT Hass.io",
        "model": "Custom",
        "identifiers": ["Mqtt Security"],
        "sw_version": "20230803",
        "suggested_area": "Algemeen"
        }
    };
if (msg.payload === '' ){
     node.status({fill:"red",shape:"dot",text: logTimeStamp + " " + _topic});
     _topic = _topic + "/config";
     _qos = 0;
     _retain = false;
     _payload = '';
    } else {
     node.status({fill:"green",shape:"dot",text: logTimeStamp + " " + _topic});
     _topic = _topic + "/config";
    }    
var message = {
    topic: _topic,
    qos: _qos,
    retain: _retain,
    payload: _payload
};
return message;

(When using this do not use logTimeStamp because it’s custom)

In this example a MQTT device is automatically detected by HA by adding the device to the topic homeassistant/sensor/presence/detection/ with the proper payloads.
(be aware with paths in MQTT, /homeassistant is not the same as homeassistant without the /)

The MQTT-discovery path is:
homeassistant//[<node_id>/]<object_id>/config

A sensor example is documented: MQTT Sensor - Home Assistant

This is just one example, but there a lot ways to do this. I just posted this as an example, maybe you can pick what is usefull for you.

This should give you 30 seconds delay before turning the lights off:

...
  - wait_for_trigger:
      - platform: mqtt
        topic: Pir/kitchen/prio/inputs/read/1004
        payload: CLOSED
    timeout:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
    continue_on_timeout: false
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - action: light.turn_off
    metadata: {}
    data: {}
    target:
      device_id: 2db98dc8a48ca34413b9f00ab5a76ae6
mode: single

Thank you for your help and example I will have a look into this :slightly_smiling_face:

Thank you

May I ask is this just a delay regardless what happens or is it if the is Motion within the 30 seconds it will cancel turning the light off until is a full 30 seconds as past?

Would I be better setting a second automation up to wait for the CLOSED payload for a full 30 seconds before turning off the light?

It is just a simple delay which gets executed after the 30 seconds timeout before turning off the lights.

Did you try to use the Blueprint and take control? One way is to copy/paste the needed trigger/condition/actions from your automation to the Blueprint one.
It’s much easier can even be done from the UI.

Just a thought…

i am just looking at that now however the sensor is not showing in the Blueprint, so just trying to work out how i get my mqtt topic to show as a Motion Sensor :face_with_diagonal_mouth:

Just use another available entities and then copy/paste/disable the ones needed. But be aware you have to Take control first of the blueprint before modifying.

Thats why I posted the post about the MQTT-discovery, but that is something different. Just start with what you allready did and then go from there. I will help you if I can.

Am with you. this is what i give me on a different sensor

- alias: Wait until there is no motion from device
    wait_for_trigger:
      platform: state
      entity_id: binary_sensor.cctv_perimeter
      from: "on"
      to: "off"
  - alias: Wait the number of seconds that has been set
    delay: 30
  - alias: Turn off the light

Am trying to work out if this is still just 30 seconds wait after any no motion, as am after if motion is detected in that 30 seconds reset the timer for another 30 seconds until we get a full 30seconds without motion

Do you used the UI?
What I’m trying to explain is to copy/paste your working config to the blueprint config. For example, your Trigger part was working right? Then copy this in the Trigger part of the blueprint. In the UI you can disable/enable the parts which are (not) working (yet) and you can monitor/trace what is.

You don’t even have to do all those Yaml things…

I did some fiddling around and try to change it to your config and used the Blueprint. I don’t know or this is actualy working (I’m not a YAML master by far) but maybe you can experiment with it:

alias: Kitchen LED on With PIR
description: ""
trigger:
  - platform: mqtt
    topic: Pir/kitchen/prio/inputs/read/1004
    payload: OPEN
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - action: light.turn_on
    data:
      rgb_color:
        - 255
        - 255
        - 255
      brightness_pct: 50
    enabled: true
    target:
      entity_id: light.kitchen_led
      device_id: 2db98dc8a48ca34413b9f00ab5a76ae6
  - wait_for_trigger:
      - platform: mqtt
        topic: Pir/kitchen/prio/inputs/read/1004
        payload: CLOSED
  - delay: 30
    enabled: true
  - action: light.turn_off
    data: {}
    enabled: true
    target:
      entity_id: light.kitchen_led
      device_id: 2db98dc8a48ca34413b9f00ab5a76ae6
mode: restart
max_exceeded: silent

Does this make sense? I think it is close to what you started with. Maybe an YAML master (@Tamsy ?) can chime in to check… :grinning:

1 Like

Thank you very much :star_struck:

1 Like

You’re welcome! Is it working as intended?