Automation: action when no conditions are met

Hi,

I’m attempting to setup an automation for my humidifier.
I’ve set some triggers and conditions when the automation should run.
Works great when the conditions are met, but doesn’t achieve what I’m aiming for when the conditions are not met.

I use an input_boolean called ‘awake’ that indicates if I’m awake or not (humidifier is in my room, I don’t want it to run when I’m asleep).
If I put this as a condition, if the humidifier is running when I go to sleep, then it will keep running because conditions are not met…

Can we have a remaining catch action that we could optionally trigger when conditions are not met ?

Details of what I want to achieve (focus on triggers/conditions):

  • Trigger if humidity reaches threshold
  • Run only during daytime
  • Run only when I’m awake
  • If I’m not awake, or after the schedule, humidifier should turn off

Only way I can think of now is to put my conditions within the actions section as service_template. But I seem to be unable to use the conditions section itself.

Current automation:

- id: '1608571242117'
  alias: Manage Bedroom humidity
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.bedroom_humidity
  - platform: state
    entity_id: input_number.desired_humidity_bedroom
  - platform: state
    entity_id: input_boolean.awake
  condition: []
  action:
  - service_template: >
  {% set target = states('input_number.desired_humidity_bedroom') | float %}
  {% set actual = states('sensor.bedroom_humidity') | float %}
  {% set delta = states('input_number.delta_humidity') | float %} 
  {% set hi = target + delta %}
  {% set lo = target - delta %}

  {% if is_state('input_boolean.awake', 'on') %}
    {% if actual > hi %}
      switch.turn_on
    {% elif actual < lo %}
      switch.turn_off
    {% else %}
      switch.turn_{{ states('switch.humidifier') | lower }}
    {% endif %}
   {% else %}
     switch.turn_off
   {% endif %}
    entity_id: switch.humidifier
  mode: single

For use cases like this I tend to use the chooser with the default where conditions are not met.

Also, providing your install is up to date (can’t remember which release introduced this change) it is no longer necessary to use service_template / data_template etc - templates can now be used with the usual service and data elements.

1 Like

What you have seems to work when I tested it in the developer template, but I get the idea you had a condition: section that didn’t work. If you can share that, we can try to figure out what went wrong. It would get a little complicated, and you might be better off sticking with the template.

The chooser option that @TazUk mentions looks like a viable method as well - I missed that one, so haven’t tried it yet.

Does what you have within the template give the desired result?

Is that a copy-paste error or is there really no additional indentation of all the lines in service_template?

This indentation fails to pass Check Config:

  - service_template: >
  {% set target = states('input_number.desired_humidity_bedroom') | float %}
  {% set actual = states('sensor.bedroom_humidity') | float %}

This indentation passes Check Config:

  - service_template: >
      {% set target = states('input_number.desired_humidity_bedroom') | float %}
      {% set actual = states('sensor.bedroom_humidity') | float %}

Copy/paste error.
The code works well, lthough it contains the additional if statement to check if I’m awake.

I’ve replaced now with the suggested Chooser, mentioned by TazUk.
It facilitates the management of all these conditions and avoid garbage easy code in the service section, so thanks! :wink:

The option is called choose not chooser and it’s unnecessary for this application where the entity_id is fixed and only the service changes. It’s more of a stylistic choice as opposed to a necessity to resolve this problem.

Please consider double-checking your work when submitting it, otherwise it misleads the people trying to help you.

Please consider posting the revised version you have created. Otherwise, users who may have a similar question don’t have any example of how the automation was actually converted to use choose.

1 Like

Full automation YAML (working like a charm).

I only have the automation running when I’m home and awake.
The presence and awake input values are managed through scripts that I call via Google Assistant routines.

- id: '1608571242117'
  alias: Manage Bedroom humidity
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.bedroom_humidity
  - platform: state
    entity_id: input_number.desired_humidity_bedroom
  - platform: state
    entity_id: input_boolean.awake
  - platform: state
    entity_id: input_select.presence
  condition: []
  action:
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: input_select.presence
          state: Home
        - condition: state
          entity_id: input_boolean.awake
          state: 'on'
      sequence:
      - service: "{% set target = states('input_number.desired_humidity_bedroom')\
          \ | float %} {% set actual = states('sensor.bedroom_humidity') | float %}\
          \ {% set delta = states('input_number.delta_humidity') | float %}  {% set\
          \ hi = target + delta %} {% set lo = target - delta %} {% if actual > hi\
          \ %}\n     switch.turn_on\n{% elif actual < lo %}\n     switch.turn_off\n\
          {% else %}\n     switch.turn_{{ states('switch.humidifier') | lower }}\n\
          {% endif %}\n"
        entity_id: switch.humidifier
    default:
    - type: turn_off
      device_id: 45cbc5ea50c3400f2ac1243be2518344
      entity_id: switch.humidifier
      domain: switch
  mode: single

Easily achieved without the use of choose (and reduced in size):

- id: '1608571242117'
  alias: Manage Bedroom humidity
  trigger:
  - platform: state
    entity_id:
    - sensor.bedroom_humidity
    - input_number.desired_humidity_bedroom
    - input_boolean.awake
    - input_select.presence
  action:
  - service: >
      {% if is_state('input_select.presence', 'Home') and is_state('input_boolean.awake', 'on') %}
        {% set target = states('input_number.desired_humidity_bedroom') | float %}
        {% set actual = states('sensor.bedroom_humidity') | float %}
        {% set delta = states('input_number.delta_humidity') | float %}
        {% if actual > target + delta %} switch.turn_on
        {% elif actual < target - delta %} switch.turn_off
        {% else %} switch.turn_{{ states('switch.humidifier') | lower }}
        {% endif %}
      {% else %} switch.turn_off
      {% endif %}
    entity_id: switch.humidifier
  mode: single

Still has a copy/paste error as you have included the id: of the next automation