Template Sensor That Updates Every X Minutes or Hours

I’m trying to configure a binary template sensor that recomputes on/off (or true/false) based on a time interval, although the derived state would rely on other sensor inputs. I do this because want to rate-limit it, so that it doesn’t update too rapidly based the inputs on which it relies.

I created a template binary sensor through the UI, and added this YAML:

template:
  - trigger:
    - platform: time_pattern
      minutes: /1
  - sensor:
    state: "on"

Obviously it does not derive a state yet, I’m just trying to test the time_pattern for now.

However, I get this warning:

This template does not listen for any events and will not update automatically.

Can anyone help with how to correctly create a template that does this?

The template editor doesn’t check the YAML, only the Jinja templates.

Do you mean the Template Editor in Developer Tools or the Template Helper editor?

Advanced functions like trigger are not available through the UI Template helper. You must set up the sensor in your YAML configuration. Also, the State template field in the UI Template Helper editor is only for the state template, not YAML configuration.

What exactly do you mean when you say “though the UI”? Do you mean you created a Template Helper?

If that’s what you did then there’s no place in the form that’s designed to receive YAML. The form has a field named State Template and that’s where you put the Jinja template. In addition, a Template Helper cannot be used to create a Trigger-based Template Binary Sensor.

The YAML you posted can only be used in a text file (like configuration.yaml) and cannot be used to create a Trigger-based Template Binary Sensor via the UI.

That’s what I meant, yes.

Understood. I will work on just creating this in my config.yaml !

And for anyone doing this in the future, I found that this worked. I added this to my config.yaml:

template:
  trigger:
    - platform: time_pattern
      minutes: "/1"
  sensor:
    name: "Test binary sensor"
    unique_id: test_binary_sensor
    state: on

Again, you obviously need to add some derived logic to the state for this to be useful in any way.

If you ever want to add another Trigger-based Template entity, you should add a hyphen before trigger like this:

template:
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      name: "Test binary sensor"
      unique_id: test_binary_sensor
      state: 'on'

I also suggest you get in the habit of wrapping on and off in quotes so that the YAML processor handles them as strings. Otherwise, unquoted on and off are understood to be boolean true and false. In this particular case it doesn’t matter because a Template Binary Sensor accepts true/false, 1/0, 'on'/'off', etc. In other cases it will, like when comparing string values.

Since I don’t fully understand why, could you perhaps point me to a good explanation or tutorial on HA YAML?

Admittedly I just cobble things together from examples in the forums and documentation, but I find YAML to be difficult to read and write because it’s not at all intuitive!

Start here:

Also:

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/