Trigger based helper does not work

I tried to implement an example for a template that I found, but I can’t get it to work. I created a helper template for a motion sensor:

template:

template:
- trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_occupancy 
    to: 'on'
  binary_sensor:
    name: Woonkamer motion detected with occupancy timeout    
    state: 'on'
    auto_off: '00:15:00'

The trigger does work in an automatiion, but there is no way to get this template respond to the trigger.
The editor I also get the warning “This template does not listen for any events and will not update automatically.”

What am I doing wrong?

You’ve called this a “helper”. Does that mean you’ve created it in the UI, and pasted your code above into the state template box?

If so, that will not work. At the current time, the only way to create a trigger-based template sensor is via YAML — that code above needs to go into configuration.yaml.

There are no events. There isn’t actually a template in your YAML sensor configuration.

Templates are Jinja code like:

{{ states('binary_sensor.motion_sensor_occupancy') }}

You have defined a template binary sensor, but rather than using a template to set the state, you are hard-coding it to a static 'on' value, so there are no events to listen to.

YAML trigger configurations are not templates.

Thanks for your reply.
I formatted the code according to your instructions.
I created the template in the UI using the Template Helper. The code is as shown in the state template box.
I copied part of the template code, without the trigger part, to the configuration.yaml and added template code in Ninja. That seems to work.
The usage of the trigger part is still obscure to me.

You cannot create trigger-based sensors in the UI.

The UI template sensor helper state template box is for Jinja templates like:

{{ states('binary_sensor.motion_sensor_occupancy') }}

and not for YAML like:

template:
  - trigger:
      platform: state

So if you want a trigger-based sensor that only updates when the trigger fires, you must create that in YAML and not in the UI.

If you want a general template sensor that updates when any of the inputs change, you can use the UI.

As an example, I have two outside temperature sensors, one facing east, one facing west. I take the colder of the two to eliminate any sun heat issues.

I do this with a normal template sensor which can be set up via the UI:

{{ (states('sensor.outside_temperature_1')|float(0),
    states('sensor.outside_temperature_2')|float(0))|min }}

That updates whenever a new sensor reading comes in from either sensor:

For a triggered example, I want a sensor that shows how many hours the heating system has run during the day. To do this, I use a trigger-based sensor that triggers at the end of the day and ignores any other changes:

template:
  - trigger:
      - platform: time
        at: "23:59:00"
    sensor:
      - name: "Heating daily hours"
        unit_of_measurement: 'hours'
        state: "{{ states('sensor.heating_hours_today')|float(0) }}"

Without the trigger, that would just be a copy of the heating_hours_today sensor that gradually increases through the day.

That is in my config files, as it cannot (yet) be set up through the UI.

1 Like

The original code is in the configuration.yaml and it is working now!
I also found out that this template does not work in the Template editor in the Developer Tools. I think that that editor has some limitations.
Thank you for your support!

Your original code is not a template

I can’t say it any more clearly…