Numeric_state - why no 'equal to'?

I’ve run into this issue as well.
I’m changing colors of lights based on my heart rate. I’m retrieving the bpm via API which returns a whole number (string?) 109, 142, 163, etc.

The ranges are as follows:
Aqua: <109 bpm
Olive: 109 to 125
Gold: 126 to 142
Orange: 143 to 159
red: >160

My automation fails or doesn’t act as expected at the border cases. 142 for instance fails when I had my automation ranges set the same as the bpm ranges. If I set one condition to above 141, I’ve created a situation where two conditions are true at the same time which doesn’t seem ideal. A Greater than or Equal to state would alleviate this issue without adding more complexity or thinking to this non-programmer’s automation.

You could trigger on any change in bpm, no conditions and then in the action part determine the color that the light should be set to with an IF-ELIF-ELSE statement or with the choose function.

Use templates to get the triggers/conditions you want, not numeric_state.

I’m sure there are a myriad of ways around this, and I’d love to understand them, but mainly I’m reiterating that from the interface, for a non-professional programmer, not being able to specify ‘equal to’ is a surprising lack of functionality.

5 Likes

It’s not really surprising. It’s a fairly simple thing to do with a template. This is most likely the reason it hasn’t been added.

If you want it added to numeric_state, this is not the place to do it. Configuration is for support. Head over to feature requests and make a formal request for this functionality.

OK. Could you point me in a good direction regarding how to use templates here?

Here’s a link to my current script. Peloton support

I really wouldn’t do this if I were you.
People will use it and it will all go wrong (pretty much as petro says)

    trigger:
      - platform: template
        value_template: "{{ states('sensor.s_heat_thermostat_temp') | float == 24 }}"

In this instance, the sensor could go from 23.9 to 24.7 (so is never equal to 24 and thus NEVER triggers

Just a bad idea
:man_shrugging:

The way you’re using it just use a state condition. It gets you exactly what you want:

instead of

      - condition: numeric_state
        entity_id: sensor.peloton_bpm
        above: '160'

use

      - condition: state
        entity_id: sensor.peloton_bpm
        state: '160'

But that would only trigger at 160. The API refreshes every minute so it could go from 154 to 162 and never hit 160. So I would be back to using templates similar to this?

conditions: "{{ (state_attr('sensor.peloton_username', 'Heart Rate Bpm') | int) > 160 }}"
and
conditions: "{{ (state_attr('sensor.peloton_username', 'Heart Rate Bpm') | int) >= 143 OR conditions: "{{ (state_attr('sensor.peloton_username', 'Heart Rate Bpm') | int) < 160 }}" }}"

I didn’t read your whole automation. I was simply pointing out that an “equals” numeric trigger in your case would the the same as a state trigger. Is this not what you want?

What’s the end goal of this automation? How about we start there

Alright, I read what you’re trying to do, try this automation

- alias: Change lights based on HR
  trigger:
  - platform: state
    entity_id: sensor.peloton_bpm
  condition:
  - condition: state
    entity_id: sensor.peloton_arghthor
    state: Active
  action:
  - service: light.turn_on
    data:
      rgb_color: >
        {%- set heartrate = trigger.to_state.state | int %}
        {% if heartrate >= 160 %}
          [ 255, 71, 89 ]
        {% elif 143 <= heartrate < 160 %}
          [ 252, 128, 15 ]
        {% elif 126 <= heartrate < 143 %}
          [ 250, 203, 62 ]
        {% elif 109 <= heartrate < 126 %}
          [ 182, 201, 92 ]
        {% else %}
          [ 80, 196, 170 ]
        {% endif %}

Disclaimer, this will only work in 0.118+

3 Likes

I think one solution is to include a NOT condition before the above or below numeric state.

So if the trigger state is below or equal 1, you can set trigger state as NOT above 1 instead.

1 Like

I have a slider to set a Timer for a light I have. After a given time it should turn off. If you slide the slider to “0” it also should turn off the light. That is my problem at the moment, where an “equal to” would help a lot.

that’s what template triggers are for

- platform: template
  value_template: "{{ states('sensor.xyz') == '0' }}"
1 Like

Another use-case for this.

I want a condition to check if my thermostat is set to 20. Yes I could use above 19.999 and below 20.001 but why? It would be much simpler to just have equals 20.

and you can do this, with a normal state condition

- condition: state
  entity_id: ...
  state: '20.0'

This will evaluate it as a string, so it will always return false.

- condition: state
  entity_id: ...
  attribute: ....
  state: 20.0

Without the apostrophes it will evaluate as a number.

The issue is, when setting a condition via the UI, it automatically adds the apostrophes. Combine that with the fact that numeric state doesn’t have an “equal to”, there is currently no way to do this via the UI.

Sure it’s not hard to switch to YAML mode and remove the apostrophes, but is the complete newbie to HA going to know that? Having an “equal to” in the numeric state, even if it converts to yaml as state: 20 would make things much more user friendly.

Even as an HA user for several years, I just made up an automation through the GUI and was scratching my head trying to figure out why it was evaluating as false when it was clearly true. Took me a few minutes to realize it was a string vs a number.

2 Likes

it is a string, and no, it will return true because all states are strings.

numerical_state conditions evaluate states as a number.

On my screen you’re ahead of me :). Must be ms apart. Anyways, the posts get the point across :thumbsup: