Comparing sensor temperature with input number not working

Hello,

I’m new to this so any help is more than welcome and I apologize in advanced if I make anything wrong.

What I want to do:
I have 2 temperature sensors (Shelly HT) in different areas of the house. I want to turn on a plug if one of them falls below a given temperature defined in input_number.termometro_test, but only during a period of the day specified by input_datetime.horario_inicio & input_datetime.horario_fim.

Since I have no YAML experience, I’m working with the UI as much as possible (Variables where created in the UI and also the automation). I then tried several different solution in the UI and by editing the automation.yaml.

My issue is:
Everything works if in the condition I use a value such as “18”, everything I tried with the “input_number.termometro_test” does not work.

I tried:

  • below: 18 => Works
  • below: input_number.termometro_test => Does not work (edited in “automations.yaml”)
  • below: "{{ states('input_number.termometro_test') | int }}" => Does not work (edited in “automations.yaml”)
  • below: "{{ states('input_number.termometro_test') | int(0) }}" => Does not work (edited in “automations.yaml”)

What am I doing wrong?

Code:

- id: '1608481773854'
  alias: Test Tempo
  description: ''
  trigger:
  - platform: time
    at: input_datetime.horario_inicio
  condition:
  - condition: and
    conditions:
    - condition: time
      before: input_datetime.horario_fim
    - condition: or
      conditions:
      - type: is_temperature
        condition: device
        device_id: 9f1a67cb9ed20ad0ad9112e2f612a1a3
        entity_id: sensor.shelly_h_t_1_temperature
        domain: sensor
        below: "{{ states('input_number.termometro_test') | int(0) }}"
      - type: is_temperature
        condition: device
        device_id: ebb3fcc754b974136e9b6399a4d23169
        entity_id: sensor.shelly_h_t_2_temperature
        domain: sensor
        below: "{{ states('input_number.termometro_test') | int(0) }}"
  action:
  - type: toggle
    device_id: d3928893ec73429d80ad3d6f94f0633e
    entity_id: switch.shellyplug1
    domain: switch
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.tester
  mode: single

Thanks for the help

Maybe try numeric_state as condition instead device.
Try this:

condition: or
conditions:
  - condition: numeric_state
    entity_id: sensor.shelly_h_t_1_temperature
    below: '{{ states("input_number.termometro_test") | int}}'
  - condition: numeric_state
    entity_id: sensor.shelly_h_t_2_temperature
    below: '{{ states("input_number.termometro_test") | int}}'

Or you could try template:

condition: or
conditions:
  - condition: template
    value_template: >-
      {{ (states("sensor.shelly_h_t_1_temperature") | int) <
      (states("input_number.termometro_test") | int) }}
  - condition: template
    value_template: >-
      {{ (states("sensor.shelly_h_t_2_temperature") | int) <
      (states("input_number.termometro_test") | int) }}

I think you can do this also via the UI like this:
image

1 Like

Theoretically, the first one should have worked because a Numeric State Condition does support assigning a Helper (such as an input_number) to its above and below options. Here’s the last example from the documentation:

condition:
  condition: numeric_state
  entity_id: climate.living_room_thermostat
  attribute: temperature
  above: input_number.temperature_threshold_low
  below: input_number.temperature_threshold_high

The other two are invalid because it does not allow its below and above options to have templates.


EDIT

I see the problem. Your example is using a Device Condition, not a Numeric State Condition. Device Conditions aren’t even documented; they are created via the UI and it’s best not to edit them afterwards. Use a Numeric State Condition and assign it the input_number but don’t try to create a template because it’s not supported for below.

1 Like

First of all, thx for the help.

I tried several things in the UI, and it seems that whenever you put something in Above or Below that is different of an actual number, you get error messages and nothing is saved to automation.yaml (this happened both in device and in numeric_state)

The Template solution worked straight out of the UI. It behaves a bit strange because it does not fire when the trigger happens but about 20-30sec later. This is not a problem in this case, but I can imagine that this could be an issue in some aplications.

Big thx for your help and support,

Thx for the help,

Might be a bug but the UI does not allow you to insert a helper in above or below. When you try to save it, the UI gives out an error and nothing is saved.

Anyway, template suggestion of Dynamike worked.

Thx for the help

1 Like

The Automation Editor has its limitations. :man_shrugging:

BTW, given that you are using a Template Condition, a single template can perform the task of comparing both temperatures.

condition:
  - condition: template
    value_template: >-
      {% set test = states("input_number.termometro_test") | int %}
      {{ (states("sensor.shelly_h_t_1_temperature") | int) < test or
         (states("sensor.shelly_h_t_2_temperature") | int) < test }}
3 Likes

What does the error say when you use numeric_state and put input_number.termometro_test in below over the UI?

I made some additional tests on another automation I had (didn’t wanted to mess with the currently working one) and there I had no issue in using “below: input_number.termometro_test” directly in the UI.

This however only works when using a “numeric_state” instead of “Device”. If I do this in “Device” I get the following error: “Message malformed: expected float for dictionary value @ data[‘below’]”.

The issue I had of taking 20-30seconds to trigger also disappeared and is now instantly.

thx

1 Like