*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…
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.
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.
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
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).