Template binary sensor with time_pattern trigger only works once

Never thought about that. Do trigger-based template sensors operate similar to what would be considered restart mode for an automation?

That is pretty much exactly what I am doing! Theirs is more polished and configurable than mine of course. I am definitely going to check that out.

The only additional functionality that I have in my solution is simulating on/off states for switches that have no recent history (for instance there is a light which is rarely used but I do want that light to turn on/off randomly while I am away) This random sensor that is the subject of this thread is what I am using to do that.

Thanks for that
DM

Just had a quick look at the HA ‘automation modes’ page.

Did I get you right here, that this happens because ‘triggered sensors/binary_sensors’ sort of default to something similar as an automation defined to
- mode: restart
whereas the ordinary automations default to
-mode: single

I am not sure that is true in my case. Following @Didgeridrew 's example. I changed the delay_on and auto_off values to static values where their sum is less than the interval. The trigger still fires exactly once and never again.

- triggers:
    - trigger: time_pattern
      minutes: "/10"
      seconds: 0
  binary_sensor:
    - name: random_break
      state: "on"
      delay_on: "00:02:00"
      auto_off: "00:04:00"
      attributes:
        updated: "{{ now() }}"

I just tested this with a short interval and it looks very promising. I have updated all the associated sensors in my yaml with this approach and I am going to let it run for a while to see what happens. I’ll report back tomorrow as some sensors only trigger once a day and I want to test with all the real values.

Thanks
DM

Since you said you have a few of the “random” sensors, I would probably just break this whole thing up into two template sensors that provide start and stop times.

template:
  - triggers:
      - trigger: time_pattern
        minutes: "0"
        seconds: "0"
    actions:
      - variables:
          hour: "{{ now().hour }}" 
          on_min: "{{ range(1,60) | random | int }}"
          off_min: |
            {% set end = range(on_min, 61) | random | int %}          
            {{ 0 if end == 60 else end }}
    sensor:
      - name: random_break_delay_start
        state: "{{ today_at( hour|string~':'~on_min|string ) }}"
        device_class: timestamp
      - name: random_break_delay_stop
        state:  |
          {% set offset = 1 if off_min == 0 else 0 %}
          {{ today_at( (offset + hour)|string~':'~off_min|string ) }} 
        device_class: timestamp

You could then use those in Time triggers of a trigger-based binary sensor or directly in your “presence simulation” automations. To get multiple “random” sequences out of just two sensors, utilize the offset feature of Time triggers.

Templates are just rendered when a trigger occurs, nothing more. The delays, if different, will reset.

Sure that part makes sense. But if you have a bunch of actions (which would include long delays) that are still executing when the trigger occurs again, what happens?

All scripts for template entities are set to the default, which is single.

1 Like

The trigger is firing constantly. There is a bug with delay on.

Y’all are welcome to confirm this if you want.

template:
- triggers:
    - trigger: time_pattern
      seconds: "/5"
  binary_sensor:
    - name: Time Pattern Delay On Auto Off
      state: "on"
      delay_on: "00:00:02"
      auto_off: "00:00:01"
      attributes:
        updated: "{{ now() }}"
    - name: Time Pattern Delay On
      state: "on"
      delay_on: "00:00:02"
      attributes:
        updated: "{{ now() }}"
    - name: Time Pattern Auto Off
      state: "on"
      auto_off: "00:00:01"
      attributes:
        updated: "{{ now() }}"
  sensor:
    - name: Time Pattern Trigger
      device_class: timestamp
      state: "{{ now() }}"

I marked @Didgeridrew 's post as the solution because it solved the problem presented in my original post. However other members have pointed out improvements to my approach and an existing addon and also a blueprint that implement what I am trying to do and I’ll certainly be looking at those.

Thanks to all!
DM

Bug found and fixed

4 Likes

Excellent. Thank you!

DM