Trigger numeric_state below value not firing

I’m trying to get a notification when the temperature in a room is too low. The following automation is what I currently use:

###########################################
# Notify if rooms are too cold or too hot #
###########################################
- id: notify_rooms_too_cold_or_hot
  alias: Notify if rooms are too cold or too hot
  initial_state: True
  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_xxxxxxxxxxxxxx
      below: 17
    - platform: numeric_state
      entity_id: sensor.temperature_xxxxxxxxxxxxxx
      above: 23
  action:
    - service: notify.notify_phone
      data_template:
        message: >
          {% if states.sensor.temperature_xxxxxxxxxxxxxx.state|float < 17 -%}
            The room is too cold ({{states.sensor.temperature_xxxxxxxxxxxxxx.state_with_unit}})
          {% elif states.sensor.temperature_xxxxxxxxxxxxxx.state|float > 23 -%}
            The room is too hot ({{states.sensor.temperature_xxxxxxxxxxxxxx.state_with_unit}})
          {%- endif %}

The notification is sent to a notification group. Using a persistent notification doesn’t change anything.

The goal is to add multiple triggers for rooms being too hot or too cold and using the template to decide what rooms the notification should mention.

I’ve tried to manually lower to value of the sensor using the dev tools, but that does not trigger the notification to be sent. The sensor value is currently set at 16.65 °C.

Does anyone have an idea why this is not working?

I have a similar thing set up for my fridge. I have mine as two separate automation’s, one for too hot and one for too cold, I don’t know much about programming but perhaps the two triggers are cancelling each other out? Here’s my fridge automatons for reference just in case it helps.

- alias: 'Fridge Hot'
  trigger:
    platform: numeric_state
    entity_id: sensor.temperature_158d0001299416
    above: '5'
  action:
    service: notify.sms_notify
    data:
      message: 'Fridge is hot, {{ states.sensor.temperature_158d0001299416.state }} degrees.'

- alias: 'Fridge Cold'
  trigger:
    platform: numeric_state
    entity_id: sensor.temperature_158d0001299416
    below: '-1'
  action:
    service: notify.sms_notify
    data:
      message: 'Fridge is cold, {{ states.sensor.temperature_158d0001299416.state }} degrees.'

Your sensors probably aren’t numeric, they are string. Despite what the documentation says, in practice nom-numeric sensors do not work with numeric triggers. You have a couplemof options.

  1. switch to a template trigger
  2. Create a template sensor based off of the real sensor converted to float.
2 Likes

The solution provided by @treno put me into the right direction. Thank you.

Currently I have changed the automation to the following:

###########################################
# Notify if rooms are too cold or too hot #
###########################################
- id: notify_rooms_too_cold_or_hot
  alias: Notify if rooms are too cold or too hot
  initial_state: True
  trigger:
    - platform: template
      value_template: "{% if states.sensor.temperature_xxxxxxxxxxxxxx.state | float < 17.5 %} true {% endif %}"
    - platform: template
      value_template: "{% if states.sensor.temperature_xxxxxxxxxxxxxx.state | float > 23.5 %} true {% endif %}"
    - platform: template
      value_template: "{% if states.sensor.temperature_xxxxxxxxxxxxxx_2.state | float < 17.5 %} true {% endif %}"
    - platform: template
      value_template: "{% if states.sensor.temperature_xxxxxxxxxxxxxx_2.state | float > 23.5 %} true {% endif %}"
  action:
    - service: notify.notify_phone
      data_template:
        message: >
          {% if states.sensor.temperature_xxxxxxxxxxxxxx.state|float < 17.5 -%}
            The room is too cold ({{states.sensor.temperature_xxxxxxxxxxxxxx.state_with_unit}})
          {% elif states.sensor.temperature_xxxxxxxxxxxxxx.state|float > 23 -%}
            The room is too hot  ({{states.sensor.temperature_xxxxxxxxxxxxxx.state_with_unit}})
          {%- endif %}
          {% if states.sensor.temperature_xxxxxxxxxxxxxx_2.state|float < 17 -%}
            Another room is too cold ({{states.sensor.temperature_xxxxxxxxxxxxxx_2.state_with_unit}})
          {% elif states.sensor.temperature_xxxxxxxxxxxxxx_2.state|float > 23 -%}
           Another room is too hot  ({{states.sensor.temperature_xxxxxxxxxxxxxx_2.state_with_unit}})
          {%- endif %}

@BarryHampants perhaps your sensor does get stored numeric and your value '5'is casted to 5 in the background by HASS causing the trigger to fire.

Since @treno made me curious, I looked into the states table of HASS and saw that the event_data column contans the following JSON structure:

{
  "entity_id": "sensor.temperature_xxxxxxxxxxxxxx",
  "new_state": {
    "entity_id": "sensor.temperature_xxxxxxxxxxxxxx",
    "last_changed": "2017-10-07T13:00:32.658986+00:00",
    "attributes": {
      "homebridge_visible": true,
      "battery_level": 39.0,
      "friendly_name": "Bedroom Temperature",
      "unit_of_measurement": "\u00b0C"
    },
    "last_updated": "2017-10-07T13:00:32.658986+00:00",
    "state": "17.71"
  },
  "old_state": {
    "entity_id": "sensor.temperature_xxxxxxxxxxxxxx",
    "last_changed": "2017-10-07T12:03:30.003388+00:00",
    "attributes": {
      "homebridge_visible": true,
      "battery_level": 39.0,
      "friendly_name": "Bedroom Temperature",
      "unit_of_measurement": "\u00b0C"
    },
    "last_updated": "2017-10-07T12:03:30.003388+00:00",
    "state": "17.68"
  }
}

That does show that the data type of the state attribute is indeed a string value.

Thank you for all the help!

4 Likes

Thank you!

And yes my sensors (Xiaomi Aqara) do return a number but because of your question I learned a new way to do things and can condense two automation’s into a single one now.

Mine are Xiaomi’s as well @BarryHampants :blush:

Glad to see I could help you a bit out as well!

1 Like

There’s a really easy way to tell if a sensor is numeric.
Go over to the template editor in developer tools and type this:

{{states.sensor.SENSOR_NAME.state > 0}}

If sensor.SENSOR_NAME is numeric, this will return true or false in the result pane.
If sensor.SENSOR_NAME is not numeric, the result pane will be blank.

12 Likes