Multiple if statements

I am trying to turn off my different lights depending on the selected input_booleans

service: homeassistant.turn_off
target:
  entity_id: >
    {% if is_state('input_boolean.kitchen_lights', 'on') %}
      switch.kitchen
    {% endif %} 
    {% if is_state('input_boolean.dining_table_lights_checkbox', 'on') %}
      switch.smart_plug_2
    {% endif %}

If I make a single input_boolean ‘on’, it does the job. However, when both input_booleans are ‘on’, this does not work. I know I can somehow make it work calling home_assistant.turn_off 2 times or put some endif conditions but I wonder why this does not work.

The if operator checks the validity of the statement one by one and as soon as a statement is found valid, it exits the operation, that is the reason why it wont work with these two conditions. Try something like this

  entity_id: >
    {% if is_state('input_boolean.kitchen_lights', 'on') and is_state('input_boolean.dining_table_lights_checkbox', 'on')%}
      switch.kitchen,switch.smart_plug_2
    {% elif is_state('input_boolean.kitchen_lights', 'on') %}
      switch.kitchen
    {% elif is_state('input_boolean.dining_table_lights_checkbox', 'on') %}
      switch.smart_plug_2
    {% endif %}

There are 2 input_booleans, each having 2 possible states, so there are 22 combinations.


service: switch.turn_off
target:
  entity_id: >
    {% set k = is_state('input_boolean.kitchen_lights', 'on') %}
    {% set d = is_state('input_boolean.dining_table_lights_checkbox', 'on') %}
    {% if k and d %} switch.kitchen, switch.smart_plug_2
    {% elif k %} switch.kitchen
    {% elif d %} switch.smart_plug_2
    {% else %} none
    {% endif %}

EDIT

Correction. Replaced service call homeassistant.turn_off with switch.turn_off because it doesn’t support the concept of none.

2 Likes

Maybe try adding dashes, so it becomes a list? (haven’t tried this, just a guess)

service: homeassistant.turn_off
target:
  entity_id: >
    {% if is_state('input_boolean.kitchen_lights', 'on') %}
      - switch.kitchen
    {% endif %} 
    {% if is_state('input_boolean.dining_table_lights_checkbox', 'on') %}
      - switch.smart_plug_2
    {% endif %}

No. You can’t generate a YAML list like that and even if it worked it only handles 2 of the 4 possible combinations.

service: homeassistant.turn_off
target:
  entity_id: >-
    {% set list = [] %}
    {% if is_state('input_boolean.kitchen_lights', 'on') %}
      {% set list = list + ['switch.kitchen'] %}
    {% endif %} 
    {% if is_state('input_boolean.dining_table_lights_checkbox', 'on') %}
      {% set list = list + ['switch.smart_plug_2'] %}
    {% endif %}
    {{ list|join(', ') }}

Although it will break when non of the statements are true.

1 Like

I have exactly the same solution as workaround but I thought I could make it “prettier”. And that is correct; it exits the operation when one of the statements is realized so I will stick to this solution.

Thanks!

I have tried every single combination, but as mentioned, if any of the conditions is realized, it exits the operation so this construcion does not work as it looks.

Be advised that the post you marked as the Solution only covers three of the four possible combinations. It doesn’t handle the case where both input_booleans are off. The result will be a blank entity_id which is an invalid value for the service call (and will cause an error message).

It is usable only if your automation includes a condition that excludes the case when the two input_booleans are off.

Why would it exit? It just runs the complete template before passing it to the call.

Following works fine in dev tools +> services

service: homeassistant.turn_off
data:
  entity_id: >-
    {% set list = [] %}
    {% if True %}
      {% set list = list + ['light.livingroom_dimmer_l1'] %}
    {% endif %} 
    {% if True %}
      {% set list = list + ['light.livingroom_dining_table'] %}
    {% endif %}
    {{ list|join(', ') }}

Turns them both off. Only if both are false it will trow an error.

Right, there must be

{% else %}
  none

added in between. Thanks

I haven’t tried this in my automation but this works in templates as you mentioned. In my case where the code was

    {% if is_state('input_boolean.kitchen_lights', 'on') %}
      switch.kitchen
    {% endif %} 
    {% if is_state('input_boolean.dining_table_lights_checkbox', 'on') %}
      switch.smart_plug_2
    {% endif %}

so you are right probably; it does not exit but outputs something like switch.kitchenswitch.smart_plug_2 which ends up no action.

Yeah, your version does not make a (comma separated) list. But you can use my version but replace the entities and of course the “True” with your statement. Then it should generate a processable list to the call.

It can also be done like this but it’s not necessarily superior to a simple chain of if-elif statements.

service: homeassistant.turn_off
target:
  entity_id: >-
    {% set x = expand('input_boolean.kitchen_lights', 'input_boolean.dining_table_lights_checkbox') | selectattr('state','eq','on') | map(attribute='object_id') | list %}
    {{ 'none' if x | count == 0 else x | join(', ') | replace('kitchen_lights', 'switch.kitchen') | replace('dining_table_lights_checkbox', 'switch.smart_plug_2') }}
2 Likes

setting entity_id none does nothing as expected but it triggers this error:

  • Error executing script. Invalid data for call_service at pos 2: Entity ID none is an invalid entity ID for dictionary value @ data[‘entity_id’]

Change the service call to switch.turn_off.

For some reason, possibly due to an oversight, homeassistant.turn_on (and turn_off) don’t understand none like switch.turn_on and light.turn_on.

1 Like