Wait_for_trigger never triggers

Hi!

I have run into a problem that has been flagged by various people multiple times already, however reading through their threads I have not found the solution for my case.

Situation:
I have a Shelly Dimmer2 built into the light switch behind the physical switch in my toilet. This works flawlessly. I also have an Aqara Motion Sensor in there, which I want to use to control the lights, without people needing to use the switch. I previously used the blueprint ‘Blackshome/sensor-light.yaml’ but I found I wanted more control over the light levels, having effectively 3 levels of brightness, instead of 2. So, I started writing my own blueprint, keeping an eye on the other blueprint for inspiration. My blueprint/automation works partly, however it always seems to get stuck at the ‘wait_for_trigger’ step. So: initial triggering to turn on the light works, ‘calculation’ of the light level works, turning the light on works, and then the wait_for_trigger gets activated. The problem is that this wait_for_trigger never clears, despite the actual sensor going to ‘clear’.

Things DID work well with Blackshome’s blueprint before, and I have managed to use the wait_for_trigger successfully in scripts and automations elsewhere. I just can’t seem to get it to behave in this blueprint :angry:

The code:
Blueprint:

blueprint:
  name: Toilet Light Control Blueprint
  description: Turn a light on based on a sensor with variable light levels
  domain: automation
  input:
    motion_sensor:
      name: Motion Sensor
      description: This sensor will control the light
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    target_light:
      name: Target Light
      description: This is the light that is being controlled
      selector:
        entity:
          domain: light
    brightness_day:
      name: Brightness for daytime use
      description: This is the brightness that will be used if the light is triggered during the daytime.
      selector:
        number:
          min: 0
          max: 255
    brightness_evening:
      name: Brightness for evening use
      description: This is the brightness that will be used if the light is triggered during the evening.
      selector:
        number:
          min: 0
          max: 255
          mode: slider
    brightness_night:
      name: Brightness for night use
      description: This is the brightness that will be used if the light is triggered during the night.
      selector:
        number:
          min: 0
          max: 255
          mode: slider
    transition_time:
      name: Transition time
      description: This is the time it takes to go from off to on, or on to off if the light supports that.
      selector:
        number:
          min: 0
          max: 10
          unit_of_measurement: 'seconds'
          mode: box
    minutes_after_cleared:
      name: Minutes after cleared
      description: This is the time the automation waits after the motion sensor cleared to turn the lights off again.
      selector:
        number:
          min: 0
          max: 10
          unit_of_measurement: 'minutes'
          mode: box
mode: restart
max_exceeded: silent
trigger:
  - platform: state
    entity_id: !input motion_sensor
action:
  - alias: Setting variables and calculating desired brightness
    variables:
      var_motion_sensor: !input motion_sensor
      var_brightness_day: !input brightness_day
      var_brightness_evening: !input brightness_evening
      var_brightness_night: !input brightness_night
      var_brightness: >-
        {% if states('input_boolean.alexanight') == 'off' and states('sun.sun') == 'above_horizon' %} {{ var_brightness_day | int }} {% elif
        states('input_boolean.alexanight') == 'off' and states('sun.sun') == 'below_horizon' %} {{ var_brightness_evening | int }} {% elif
        states('input_boolean.alexanight') == 'on' and states('sun.sun') == 'above_horizon' %} {{ var_brightness_evening | int }} {% elif
        states('input_boolean.alexanight') == 'on' and states('sun.sun') == 'below_horizon' %} {{ var_brightness_night | int }} {% endif %}
  - service: persistent_notification.create
    data:
      message: >
        Testing!
        motion sensor: {{ var_motion_sensor }}
        brightness: {{ var_brightness }}
      title: 'Template test'
    enabled: false
  - alias: Turn target_light on
    service: light.turn_on
    target:
      entity_id: !input target_light
    data:
      brightness: "{{ var_brightness }}"
      transition: !input transition_time
  - alias: Wait for motion sensor to clear
    wait_for_trigger:
      platform: state
      entity_id: !input motion_sensor
      from: 'on'
      to: 'off'
  - alias: Wait minutes_after_cleared number of minutes to continue
    delay:
      minutes: !input minutes_after_cleared
  - alias: Turn target_light off
    service: light.turn_off
    target:
      entity_id: !input target_light
    data:
      transition: !input transition_time

Note that the ‘input_boolean.alexanight’ is a boolean that is controlled elsewhere. Since it’s just read here and would be shared by all, I didn’t want to use an blueprint input for that (yet).

Section from automations.yaml:

- id: '1681241056220'
  alias: Toilet Light Downstairs
  description: Toilet Light Downstairs automation
  use_blueprint:
    path: shoikan/toilet_light_control.yaml
    input:
      motion_sensor: binary_sensor.lumi_toilet_downstairs_iaszone
      target_light: light.shellydimmertoilet1
      brightness_day: 255
      brightness_evening: 50
      brightness_night: 10
      transition_time: 2
      minutes_after_cleared: 2

Trace of such an event:

Conclusion
I am fairly certain the trigger entity is correct, since the light does get turned on. I feel the issue is there somewhere though, I just can’t seem to find what the wait_for_trigger is actually waiting for (maybe/probably I did something wrong there but I can’t find the smoking gun for this). Also note I do not want to use the timeout, I just want the trigger on the sensor clearing to work :slight_smile:

Any help in either troubleshooting this, or solving this would be very much appreciated.

1 Like

Try removing the from, so it will work even if your sensor is going unavailable before going off.

Something like this:

  - alias: Wait for motion sensor to clear
    wait_for_trigger:
      platform: state
      entity_id: !input motion_sensor
      to: 'off'

You have also to be aware that if your automations reload (it happens every time you save any automation) this will fail on that point.

Thanks! Going to try removing the ‘from’, and I was aware of the reload limitation.

Sorry, this is not the issue… It is in your trigger.

This will trigger on any state change, so if your motion sensor goes off it will start the automation again and wait until it goes from on to off again, which will never happens.

Try to change you trigger to this:

trigger:
  - platform: state
    entity_id: !input motion_sensor
    to: "on"

Ahhhhhhhh! Yes, ok. That makes sense! Doh! I saw the restart, but never connected it to the issue I was seeing. Let me try adding the from and to!

You probably don’t want the from, as you want your light on when some movement is detected, regardless if the sensor was unavailable before.

I realised that as I was making the modification :slight_smile: Thank you so much so far. This has already helped a great deal since I totally overlooked that aspect of the main trigger. If not here, that knowledge will be valuable elsewhere, I am sure. In any case, I modified things… Now we wait :slight_smile:

Success! It was indeed the main trigger (not the wait_for_trigger) not having the ‘to’ specified. Hats off, @EdwardTFN that fixed it. Much much appreciated.

1 Like