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
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.
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.
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.
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.
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.