Template multiple entities problem

Good work :+1:

I would (with my definition of neatness) reduce the trigger…

- alias: KÜTE / Kinni aken on avatud
  trigger:
    platform: state
    entity_id:
      - binary_sensor.aken_arvuti
      - binary_sensor.aken_telekas
      - binary_sensor.aken_magala
    to: 'on'
  action:
    service: climate.set_operation_mode
    data_template: 
      operation_mode: "off"
      entity_id: >
        {% if is_state('binary_sensor.aken_arvuti', 'on') and is_state('binary_sensor.aken_telekas', 'on') and is_state('binary_sensor.aken_magala', 'on')%}
          climate.elutuba, climate.magala, climate.trepp
        {% elif is_state('binary_sensor.aken_arvuti', 'on') and is_state('binary_sensor.aken_telekas', 'on') %}
          climate.elutuba, climate.trepp
        {% elif is_state('binary_sensor.aken_telekas', 'on') %}
          climate.elutuba
        {% elif is_state('binary_sensor.aken_magala', 'on') %}
          climate.magala
        {% endif %}

thats an improvement i would chose too. :wink:

but combining 2 automations like:
if a==b then do c
if d==e then do f

to 1 automation like:

if a==b or d==e then if a==b do c elif d==e do f

isnt more neat but overcomplicating. :wink:

1 Like

That hurts my brain :smile:

right, mine too :wink:
it was even hard to write it down correct, but still that is what is done with the climate situation :wink:
and that is why a lot off people struggle so much :wink:

Yeah, I think it’s because we all see the problem from a different perspective, which is probably why you’re good with python and I really really really struggle with it.

Like you saw

if a==b or d==e then if a==b do c elif d==e do f

But I saw

Do z to a, b or c if they == x

Horses for courses :smile:

in general i see a whole lot of people trying to combine things to a big problem.
and in my experience it is always the best way to splitt things up.

in science they splitt everything up to the smallest possible building bricks.
and use a few bricks together to create new bricks.

here you see people who have 10 automations and they want to create 1 from it.
just the other way around.

to built a wall you create 1 stone after another and then glue them together with cement.
if you take all the clay you need for the bricks and the cement together and trow it on 1 pile and try to build a wall from it you will never succeed.

but i am glad that @typeonegative figured it out, so its not that big of a problem that i am talking a bit phylosifical here :wink:

(still puzzles me why @typeonegative did ask my advise here, because i havent shown a lot off knowledge about this kind of things and other do) :wink:

1 Like

you simply looks so smart :slight_smile:

1 Like

then i only can say: thank you :wink:
but like all other people i can only be smart if i know how to use the tools :wink:
and there are others here that use this kind of tools way better then me.

I know i’m late to the party but it looks like you are just trying to turn off what is on.

This chunk of code should work better for expansion in the future if you add more of these devices. Your current solution would be a pain to manage if you add 1 more switch/climate device. So to clarify, It’s no better than the solutions you already have but it would be better for expansion because you just add to the sensor list and climate list.

entity_id: >
  {% set sensors = [ 'binary_sensor.aken_arvuti', 'binary_sensor.aken_telekas', 'binary_sensor.aken_magala' ] %}
  {% set climates = ['climate.elutuba', 'climate.trepp', 'climate.magala' ] %}
  {%- for sensor in sensors if is_state(sensor, 'on') %}
    {{- climates[sensors.index(sesnor)] }},
  {% endfor %}
1 Like

wow. thanx man! :slight_smile:

if you’re feeling bold to try it, let me know if it works. May have to play with this line: {{- climates[sensors.index(sesnor)] }}, to get it to work.

Including spelling sensor correctly in the bracket :yum:

1 Like

Haha, oops.:joy:

{{- climates[sensors.index(sensor)] }},

1 Like

Hi @petro
I know this is not related to the question, but I was wondering if you can help with my query:

 - service: homeassistant.turn_off
  data_template:
    entity_id: >
      {% if is_state('switch.livingroom_candle_control', 'on') %}
        switch.livingroom_candle_control
      {% endif %}
- delay: '00:00:01'
- service: homeassistant.turn_off
  data_template:
    entity_id: >
      {% if is_state('light.living_room', 'on') %}
        light.living_room
      {% endif %}
- delay: '00:00:01'
- service: homeassistant.turn_off
  data_template:
    entity_id: >
      {% if is_state('switch.livingroom_hisense_control', 'on') %}
        switch.livingroom_hisense_control
      {% endif %}
- delay: '00:00:01'
- service: homeassistant.turn_off
  data_template:
    entity_id: >
      {% if is_state('switch.livingroom_etisalat', 'on') %}
        switch.livingroom_etisalat
      {% endif %}
- delay: '00:01:10'
- service: homeassistant.turn_off
  data_template:
    entity_id: >
      {% if is_state('switch.livingroom_ac_control', 'on') and is_state('binary_sensor.neo_coolcam_battery_powered_pir_sensor_sensor_2', 'off') %}
        switch.livingroom_ac_control
      {% endif %}

whats the problem?

I am trying to create something similar to what you did above, as I am planning to add more things it is becoming complicated and I want to simplify things.

Ah, You actually have to list out what you have, you cannot create a loop. Jinja always returns a string. Jinja always returns 1 value. With those 2 limitations, you cannot create a for loop to turn on/off multiple entities in a single service.

So I am basically stuck with the same method I used. Is there any way to simplify the code by any other mean?

Your best option would be a python script. Otherwise, what you have is the best way to approach this with jinja.

thanks for the help.