Automation: Numeric compare, then switch

Hi all,

I wanted to turn a fan on and off based on a comparison of humidity. It shall be off from 23:00 to 01:00.

A numeric_state automation does the job, but only when the threshold is crossed. Having another automation switch the fan off at 23:00 “kills” it.

What I tried now, was to trigger it every minute. Did not work.

Does any of you guys have a idea?

Kind regards

description: Fan_on_when_outside<inside
trigger:
  - platform: time_pattern
    hours: '*'
    minutes: '*'
condition:
  - condition: template
    value_template: '{{ (sensor.hum_abs_outside) < (sensor.hum_abs_inside) }}'
  - condition: time
    after: '01:00'
    before: '22:59'
action:
  - type: turn_on
    device_id: c563bf247abf1ee04e4c5cddf35d45ee
    entity_id: switch.sonoff_r3_1
    domain: switch
  - type: turn_on
    device_id: efe4e807c5619b73ae8d37003ba482cc
    entity_id: switch.on_off_plug_in_unit_2
    domain: switch
mode: restart

Try this:

alias: example
description: Fan_on_when_outside<inside
trigger:
  - platform: state
    entity_id:
      - sensor.hum_abs_outside
      - sensor.hum_abs_inside
condition:
  - "{{ 1 < now().hour < 23 }}"
  - "{{ states('sensor.hum_abs_outside')|float(0) < states('sensor.hum_abs_inside')|float(0) }}"
action:
  - service: switch.turn_on
    target:
      entity_id:
        - switch.sonoff_r3_1
        - switch.on_off_plug_in_unit_2
mode: restart

BTW, in your original version, this template is invalid:

{{ (sensor.hum_abs_outside) < (sensor.hum_abs_inside) }}

Refer to the corrected version shown in my example.

1 Like

Thanks!

I tried your suggestion, but the automation does not switch on the switch.
Would it mattter, if the two values are from virtual sensors?

- platform: template
  sensors:
     hum_abs_outside:
       value_template: >-
         {% set h, t = states('sensor.outside') | float, states('sensor.outside_2') %}
         {% if not h or t == 'unknown' -%}
           'unknown'
         {%- else %}
          {% set t = t | float %}
          {{ (h*6.112*2.1674*e**((t*17.67)/(t+243.5))/(t+273.15))|round(2) }}
         {% endif %}
       unit_of_measurement: 'g/m³'
       friendly_name: 'Outside abs. humidity'

- platform: template
  sensors:
     hum_abs_inside:
       value_template: >-
         {% set h, t = states('sensor.inside') | float, states('sensor.inside_2') %}
         {% if not h or t == 'unknown' -%}
           'unknown'
         {%- else %}
          {% set t = t | float %}
          {{ (h*6.112*2.1674*e**((t*17.67)/(t+243.5))/(t+273.15))|round(2) }}
         {% endif %}
       unit_of_measurement: 'g/m³'
       friendly_name: 'Inside abs. humidity'

Is the automation triggered but fails to turn on the switches or is it never triggered?

It will only trigger when at least one of the two humidity sensors changes state (i.e. the sensor’s state value changes).

It gets executed, but fails.

Executed: 7. Dezember 2021, 22:21:00
Result:
result: false
entities:

  • sensor.hum_abs_inside
  • sensor.hum_abs_outside
{
  "trace": {
    "last_step": "condition/1",
    "run_id": "eeb7ce53bfd92bb860efd26d34fe7d8b",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2021-12-07T21:21:00.757769+00:00",
      "finish": "2021-12-07T21:21:00.759168+00:00"
    },
    "domain": "automation",
    "item_id": "1628933114880",
    "trigger": "state of sensor.hum_abs_inside",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2021-12-07T21:21:00.757945+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.fan_keller_an",
              "state": "on",
              "attributes": {
                "last_triggered": "2021-12-04T15:47:35.167007+00:00",
                "mode": "restart",
                "current": 0,
                "id": "1628933114880",
                "friendly_name": "Fan_Keller_an"
              },
              "last_changed": "2021-12-07T21:06:16.840082+00:00",
              "last_updated": "2021-12-07T21:06:16.840082+00:00",
              "context": {
                "id": "952d623eb3a378adf29cce034fa85c0f",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "platform": "state",
              "entity_id": "sensor.hum_abs_inside",
              "from_state": {
                "entity_id": "sensor.hum_abs_inside",
                "state": "29.58",
                "attributes": {
                  "unit_of_measurement": "g/m³",
                  "friendly_name": "Inside abs. humidity"
                },
                "last_changed": "2021-12-07T21:16:00.704714+00:00",
                "last_updated": "2021-12-07T21:16:00.704714+00:00",
                "context": {
                  "id": "2e020665b5f34413f875fc3af5ca166c",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.hum_abs_inside",
                "state": "28.96",
                "attributes": {
                  "unit_of_measurement": "g/m³",
                  "friendly_name": "Inside abs. humidity"
                },
                "last_changed": "2021-12-07T21:21:00.753988+00:00",
                "last_updated": "2021-12-07T21:21:00.753988+00:00",
                "context": {
                  "id": "4fef810ee26e50f528d07cccd1fdb3fb",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of sensor.hum_abs_inside"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2021-12-07T21:21:00.758045+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "condition/1": [
        {
          "path": "condition/1",
          "timestamp": "2021-12-07T21:21:00.758694+00:00",
          "result": {
            "result": false,
            "entities": [
              "sensor.hum_abs_inside",
              "sensor.hum_abs_outside"
            ]
          }
        }
      ]
    },
    "config": {
      "id": "1628933114880",
      "alias": "Fan_Keller_an",
      "description": "Fan_on_when_outside<inside",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "sensor.hum_abs_outside",
            "sensor.hum_abs_inside"
          ]
        }
      ],
      "condition": [
        {
          "condition": "template",
          "value_template": "{{ 1 < now().hour < 23 }}"
        },
        {
          "condition": "template",
          "value_template": "{{ states('sensor.hum_abs_outside') < states('sensor.hum_abs_inside') }}"
        }
      ],
      "action": [
        {
          "service": "switch.turn_on",
          "target": {
            "area_id": "keller"
          }
        }
      ],
      "mode": "restart"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "c09b04ea42afad0cd7406602e526b6ee",
      "parent_id": "4fef810ee26e50f528d07cccd1fdb3fb",
      "user_id": null
    }
  },
  "logbookEntries": []
}

Are you sure the action does what it should do?
You can test it using development tools → services.

Next to that, have you checked the automations debug/trace feature to see what’s happening?

It fails because you didn’t use what I suggested.

Look at the template here shown here in the trace information:

That’s not the template I suggested you should use.

If you want my assistance, please use what I had suggested.

1 Like

@Taras: I am sorry, must have confused something during various copy&pastes.
Your idea works as intended.

Many thx for your assistance!!!

1 Like