Template trigger input_number unable to set value

Hi there

I have been playing with template triggers and have had great success - but now I have run my head against a wall.

I made this template where I wanted to increase a number every time the sensor turned to on - now I have just hardcoded it to static 1 for testing

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.contact3_contact
        to: "on"
    number:
      - name: times_opened
        state: "0"
        set_value:
          data:
            value: 1
        step: "1"

Whenever I check the configuration I get this error:

Invalid config for [template]: Unable to determine action @ data[‘number’][0][‘set_value’][0]. Got None. (See /config/configuration.yaml, line 96).

What am I doing wrong?

Did you ever figure this out?

Just use a trigger-based Template sensor or a Counter with an automation… Trigger-based Template Number is mostly nonsensical.

*would have liked the max and min of trigger:number, but couldn’t figure out set_value. Why wouldn’t state be sufficient? No need to answer, just wondering.

set_value requires an action sequence, just like in an automation or script. The sequence is performed whenever the value of the number entity is changed either through the UI or via the number.set_value service. The sequence does not run if changes to entities in the state template cause the number to change.

set_value doesn’t actually have to do anything productive, so it’s perfectly fine to just have a Stop action or 1 second delay.

This is the closest I’ve gotten to a functional Trigger-based template number, but it has issues…

  1. It’s basically unstable over restart or reloading template entities… if you click on the entity in a dashboard card immediately after restart it will update to the next value, but triggers will cause it to reset to 1.
  2. Max and min are respected for UI change when using the slider or increment/decrement button, but not if you manually fill the field or the trigger causes the number to change min/max are disregarded.

  1. Because the triggering is what allows the state to be updated, you will have to include every service that can effect number entities in the trigger block and incorporate how to handle them into the template.

… and it’s already more complicated than an equivalent automation and input number or state-based template number would be…

  - trigger:
      - platform: state
        entity_id: input_boolean.test_bool_1
        to: "on"
      - platform: event
        event_type: call_service
        event_data:
          domain: number
          service: set_value
          service_data:
            entity_id: number.times_opened
    number:
      - name: times_opened
        state: |
          {% set current = this.state | float(0) | default(0, 1) %}
          {% if trigger.platform == 'event' %}
            {{ trigger.event.data.service_data.value|int }}
          {% else %}          
            {{ current + state_attr('number.times_opened', 'step') }}
          {% endif %}
        set_value:
          - stop: Just stop
        step: 1
        max: 2000

Interesting. I notice that you have a float filter with a default value, and a default filter. Is there a reason for having both?

No… I think I was just experimenting and never bothered cleaning everything up once I got to the point where I figured out it wasn’t worth going any further. It should be sufficient to use just float(0).