Vibration sensor for "No Motion\Vibration" by a certain Time

I want to use the YoLink vibration sensor on my trash can to add automation that if there has been no Movement\Vibration on Monday by 8 PM then to yell at me (notification or alexa) to go do it. The notifications part is fine, but when I try to setup the trigger, I get “Starts Moving” when I change the Show As to Moving or Starts Vibrating for Show As Vibration, not seeing a way to poll it for If no Movement\Vibration has occurred today then do this.

Anyone have any thoughts here?

A quick side note: stop using devices in your triggers/conditions/actions.

But even with state triggers, you still have a problem. You want the automation to trigger at 8:00, so that is what you need as a trigger. So you’d need to use a state condition. But you can’t use for: in a condition. Edit: that’s incorrect, I was thinking of numeric state conditions.

Here are a couple options. With the most complex but my preference saved for last:

  1. If the sensor’s state only gets updated when it detects motion or detects vibration, you could set up a template condition to check that the last_changed property is before today at 00:00. However, rebooting HA will update that property, so you won’t get alerts if you rebooted HA that day.
  2. Create an input helper toggle, and turn it on when Motion or vibration are detected, and turn it off at midnight.
  3. Create a trigger-based template binary sensor that does the same thing as #2. But it would combine the automations and the toggle into a single entity, and it would also be read-only.
  4. Use your automation’s last_triggered attribute to your advantage: Create triggers for motion and vibration, and also a trigger for 8:00pm. In the conditions section, create a template condition to confirm the automation hasn’t already ran today: {{ this.attributes.last_triggered | default(0 | as_datetime, true) < today_at() }}. In your actions, use a choose block or if/then: if the trigger was the time trigger, then perform the notification.

Edit: since I was wrong about the conditions: your other option is:

  • Create an automation with a trigger at 8pm. Set the following entity state conditions:
    • Motion binary sensor has been OFF for 20 hours
    • Vibration binary sensor has been OFF for 20 hours

The downside of this last method is that if you restart or the sensor becomes unknown during those 20 hours, the automation won’t trigger.

1 Like

Thanks for the reply, I think the Template Binary Sensor looks good, so I would create the template with the entity and it would check if the State has changed? I can handle all the other conditions, but I haven’t really used a Template Binary Senor.

I checked the States in the Dev menu for this entity and it looks like its on \ off. I really just need to know if its been moved that day, or more likely a few hours before.

Does this look like anything?

{{ is_state_attr('binary_sensor.trash_can_vibration_vibration', 'value', on) }}

You need an entity that can retain its own state based on the previous state of your vibration sensor. Which also implies that it retains state across HA restarts.

A normal template sensor that you can create from the UI (technically called a state-based template sensor) will only calculate its state based off the current state of other sensors.

What you would need is a trigger-based template sensor, and it is currently only possible to create those in YAML. You can’t do it from the UI.

And thinking through it, option 3 will be more difficult than option 4 because option 4 only requires one template (which I already wrote out for you) and can be done completely from the UI.

Regarding the template you wrote, it will only switch between on/off if the binary_sensor.trash_can_vibration_vibration entity has an attribute called value which I suspect it does not. Go to developer tools → states, and find that entity. You will see in the right-hand column which attributes it has. What you probably wanted instead was is_state('binary_sensor.trash_can_vibration_vibration', 'on') however that would simply mirror the state of that entity. So you haven’t achieved anything helpful yet. That is where the trigger-based template sensor comes in.

If you want to continue down the path of option 3, here’s what I would use for a template configuration. This would turn off at midnight and turn on whenever either of the two entities turn on:

template:
  - trigger:
      - platform: state
        entity_id: 
          - binary_sensor.trash_can_vibration_vibration
          - binary_sensor.trash_can_vibration_motion
        to: 'on'
      - platform: time
        at: "00:00"
    binary_sensor:
      - name: Trash Can Activity Since Midnight
        unique_id: 72521dbe-879d-4f14-930c-dba458468145
        device_class: vibration
        state: "{{ trigger.platform == 'state' }}"

Once you have this sensor created and working, then you create your automation and in the conditions section (the section called “and if” in the UI) you add an entity state condition to verify this newly created sensor is OFF.

Also note that the newly-created template sensor will have a state of unknown until it is triggered (either at midnight, or if motion or vibration is detected).