This falls somewhere between a configuration question and feature request.
I have a number of automations that monitor numeric states and trigger upon exceeding some threshold—exactly the sorts of tasks the numeric state trigger is intended to perform. However, in practice, it rarely works for me and I typically use a template trigger to accomplish the same thing.
As others have pointed out, the problem is many numeric states are returned as strings, which you can verify in the dev template pane:
# This fails
{{ states.sensor.ram_used.state > 0 }}
# This works
{{ states.sensor.ram_used.state | float > 0 }}
According to the numeric state trigger’s docs:
On state change of a specified entity, attempts to parse the state as a number
suggesting that some attempt is made to coerce strings to numbers but I haven’t seen any evidence of this and the following strategy doesn’t work:
# approach 1
- alias: 'CPU used - numeric state only'
trigger:
platform: numeric_state
entity_id: sensor.cpu_used
below: 100
action:
- service: notify.slack
data:
message: Docker numeric state CPU used.
which is a shame, because it’s the most straight forward approach and feels like the right tool for the right job.
I thought I could workaround this issue by using value_template
to perform the coercion:
# approach 2
- alias: 'CPU used - numeric state with value template'
trigger:
platform: numeric_state
entity_id: sensor.cpu_used
value_template: "{{ state.state | float }}"
below: 100
action:
- service: notify.slack
data:
message: Docker numeric state with value template CPU used.
but this too fails and I’m not sure why but would appreciate feedback if I’m missing something.
Ultimately, I end up using the follow approach almost everywhere:
# approach 3
- alias: 'CPU used - template'
trigger:
platform: template
value_template: "{{ states.sensor.cpu_used.state | float < 100 }}"
action:
- service: notify.slack
data:
message: Docker template CPU used.
which is fine, it works, but seems inelegant. I’ve also seen claims that there is a higher than usual performance cost associated with template triggers.
I’m posting this here in case anyone else has run into this issue and could benefit from my testing.