Numeric automation doesn't work

- id: '1535792456242'
  alias: AQI_light
  trigger:
    platform: numeric_state
    entity_id: sensor.chinese_air_quality_index
    above: 1
    below: 100
  action:
    service: light.turn_on
    entity_id: light.gateway_light_7c49eb1a5d92
    data:
      brightness: 20
      color_name: green

now the sensor.chinese_air_quality_index is 52

but the light is not triggered.

How to fix it ?

Possibly your sensor doesn’t return the value as number but as text? In that case the numeric state trigger isn’t hit.
See Trigger numeric_state below value not firing

1 Like

From the docs:

Triggers when numeric value of an entity’s state crosses a given threshold

The automation won’t trigger while the value stays above 1 and below 100, but only if it goes from 1 or below to above 1, or from 100 or above to below 100.

2 Likes

The docs are wrong. It will trigger if it is in the defined range. But it has to change for it to trigger. I.e., the trigger doesn’t get evaluated until the entity causes a state_changed event. So if it’s value was 52 when HA started, the automation won’t trigger. It has to change to another value within the defined range (even if it was already within the defined range) for the automation to trigger.

All states are strings. This works because the trigger code converts the state string to a float before doing the comparison.

That can’t be right as my lights would be on and off like strobes.

The behaviour I see here is exactly as defined in the docs, and that’s exactly how it should behave.

Well I analyzed the code, and I observed an automation in my log that says otherwise. But I could be mistaken. I think I’ll test this theory some more. But in the meantime, are you sure? Are you sure that the actions just don’t have any effect (e.g., turning on a light that is already on, which does nothing)?

I’d be happy if the docs were right, because I agree that’s how it should work. But, …

Pretty confident, but now you’re making me doubt myself :stuck_out_tongue_winking_eye:

Hmm, now I’m not sure either. I just did a quick test that would seem to indicate the docs are right (and I’m wrong.) So for now, @redeemer & @Tinkerer, ignore what I said.

In fact, the more I think about it, I’m more confident I’m correct, because the lights I’m talking about are set to a scene when the threshold is crossed, so if I then dim them down farther, they’d come back up when the light level changed again, but they don’t as per the docs.

Yep, not sure how I came to the conclusion that the docs were wrong. But obviously I was confused. I have a few automations that trigger based on a light level sensor, and most of them use numeric_state, but one used a state trigger with template conditions. I guess I got confused by that one triggering and thinking it was one of the ones that used the numeric_state trigger. Anyway, like I said, just ignore what I said about it being wrong. :slight_smile:

1 Like

So I just did a more extensive test (for my own understanding, if nothing else. :slight_smile:) This is what I used:

input_number:
  test:
    min: 0
    max: 100
    step: 10

automation:
  - alias: Test numeric_state range
    trigger:
      platform: numeric_state
      entity_id: input_number.test
      above: 20
      below: 80
    action:
      service: persistent_notification.create
      data:
        message: numeric_state range triggered

  - alias: Test numeric_state above
    trigger:
      platform: numeric_state
      entity_id: input_number.test
      above: 80
    action:
      service: persistent_notification.create
      data:
        message: numeric_state above triggered

  - alias: Test numeric_state below
    trigger:
      platform: numeric_state
      entity_id: input_number.test
      below: 20
    action:
      service: persistent_notification.create
      data:
        message: numeric_state below triggered

Two conclusions. First, the docs are (mostly) right. These usually trigger only when a threshold is crossed.

Second, and @redeemer, I think this addresses your original question. Like (I think :sweat_smile: – another statement that might get me into trouble…) applies to any trigger, they can only “fire” when a state change occurs. So when HA starts, if the sensor’s state is 52, that’s not enough to trigger the automation. It has to change to something that meets the conditions - which in your case is a value above 1 and below 100. And this is where the docs are maybe a bit misleading, or at least vague. If the first state change of the entity satisfies the (above and/or below) condition(s) of the trigger, it will fire, even if it doesn’t cross a threshold. And I say this because I tried it. I had my input_number at a value of 0.0. Then I restarted HA. The automation didn’t trigger. Then I changed it to 10.0. That caused the “below” automation to trigger, even though it didn’t go from 20 or above to below 20. I also tried the same thing with the other automations. E.g., for the range trigger, if it was in the range when HA restarted, the automation didn’t trigger. But if it changed to another value, still in the range, it did fire. And I got a similar result with the above only trigger. I wouldn’t necessarily say this behavior is bad, but it’s good to be aware of.

2 Likes

Getting back to the OP, I think you can “fix” the issue of not triggering when HA starts and the numeric value of sensor.chinese_air_quality_index already being in the range defined by above and below by adding a trigger and condition:

- id: '1535792456242'
  alias: AQI_light
  trigger:
    - platform: numeric_state
      entity_id: sensor.chinese_air_quality_index
      above: 1
      below: 100
    - platform: homeassistant
      event: start
  condition:
    condition: numeric_state
    entity_id: sensor.chinese_air_quality_index
    above: 1
    below: 100
  action:
    service: light.turn_on
    entity_id: light.gateway_light_7c49eb1a5d92
    data:
      brightness: 20
      color_name: green

Note that, like I explained, this will trigger again if the value of the sensor changes but stays in the range. If the light is still on at a brightness of 20 and a color of green then it won’t matter. But if something else has already changed it, then that might not be what you want.

One other way to solve the problem is, when HA starts and the value is in the range, you can force the state of the sensor to change to another value in the range (by firing a state_changed event), which would trigger the original automation. Then the next time the sensor updates it will go back to the correct value. This would solve the problem, but only if you don’t care what the exact value is.

1 Like

Thank you very much! I’ll try your solution or find another way to trigger my light.

- id: '1535792456244'
  alias: AQI_light
  trigger:
    platform: time
    minutes: 00
    seconds: 01
  condition:
      condition: numeric_state
      entity_id: sensor.chinese_air_quality_index
      above: 1
  action:
    service: light.turn_on
    data_template:
      entity_id: light.gateway_light_7c49eb1a5d92
      brightness: 5
      color_name: >
        {%- if (states.sensor.chinese_air_quality_index.state | int <= 50) -%}
        green
        {%- elif (states.sensor.chinese_air_quality_index.state | int > 50) and (states.sensor.chinese_air_quality_index.state | int <= 100) -%}
        yellow
        {%- elif (states.sensor.chinese_air_quality_index.state | int > 100) and (states.sensor.chinese_air_quality_index.state | int <= 150) -%}
        orange
        {%- elif (states.sensor.chinese_air_quality_index.state | int > 150) and (states.sensor.chinese_air_quality_index.state | int <= 200) -%}
        red
        {%- elif (states.sensor.chinese_air_quality_index.state | int > 200) and (states.sensor.chinese_air_quality_index.state | int <= 300) -%}
        purple
        {%- elif (states.sensor.chinese_air_quality_index.state | int > 300) -%}
        purple
        {%- endif -%}

I tried like this use time to trigger , It’s works !

Wow, is that what you were trying to do all along? That was not clear from your original post. I don’t think that is a very efficient way to implement it. I would suggest something like this:

- id: '1535792456244'
  alias: AQI_light
  trigger:
    - platform: state
      entity_id: sensor.chinese_air_quality_index
    - platform: homeassistant
      event: start
  condition:
    condition: numeric_state
    entity_id: sensor.chinese_air_quality_index
    above: 1
  action:
    service: light.turn_on
    data_template:
      entity_id: light.gateway_light_7c49eb1a5d92
      brightness: 5
      color_name: >
        {% set idx = states('sensor.chinese_air_quality_index')|int %}
        {% if   idx <=  50 %} green
        {% elif idx <= 100 %} yellow
        {% elif idx <= 150 %} orange
        {% elif idx <= 200 %} red
        {% else %}            purple
        {% endif %}

This will turn the light on to the appropriate color when HA starts if the index is above 1. Then it will only change the light when the index changes (and stays above 1.) This is much more efficient and responsive than having the automation run once an hour (i.e., at XX:00:01, which is what your trigger does.)

2 Likes

Thank you! I’ll try your automation~