Template multiple entities problem

right.

so 4 windows, 4 triggers
if 1 triggers then the others are conditions.

with the 4 simple automations and 2 groups i proposed, probably a climate would be turned of twice at “the same time” but if it is off ha wont turn it off, so 1 off the actions is to much, but doing nothing.

That will be 4 automations, each with 1 trigger, 3 conditions and 1 action, so around 15 lines of code each. Call it 60 lines minimum for the bunch.

1 automation with a template trigger would be fewer than 10 lines.

Kinda the point in service_templates. Just need to get the template right.

no not 3 conditions.
no conditions at all in his case, because he just want to turn something off when a window opens.

less lines, doesnt make it better in all cases. readability (so you still know what you did there after 6 months) is always also important.
and if its just about lines, then this is 1 line,

          {% if is_state('binary_sensor.aken_arvuti', 'on') and is_state('binary_sensor.aken_telekas', 'on') and is_state('binary_sensor.aken_magala', 'on')%}

but i could also put more stuff on 1 line

automation:
  - alias: magala window open
  trigger: {platform: state, entity_id: binary_sensor.aken_magala, to: on}
  action: [{service: climate.turn_off},{data:{entity_id: climate_magala}}]

then the automation is also just 3 lines. 4 times, so in total 12 lines.
and my lines are shorter.

so the actual decision isnt how many lines, but how complicated you want to write stuff.

Nice to have a neat configuration though, and this is basically exactly the situation that templates are designed for.

but its all depending of the view of the user.
combining 2 languages to 1 piece of code doesnt make it neat for me, and for certain not when in the end you have more characters.

for me it is more neat to have 4 lines like this:

  trigger: {platform: state, entity_id: binary_sensor.aken_magala, to: on}

then a block like this:

      data_template: 
        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')%}
            group.kyte_2korrus
          {% elif is_state('binary_sensor.aken_arvuti', 'on') and is_state('binary_sensor.aken_telekas', 'on') %}
            group.kyte_2korrus1
          {% elif is_state('binary_sensor.aken_telekas', 'on') %}
            climate.elutuba
          {% elif is_state('binary_sensor.aken_magala', 'on') %}
            climate.magala
          {% endif %}

btw… i got it working :slight_smile: problem was climate.turn_off

- alias: KÜTE / Kinni aken on avatud
  trigger:
    - platform: state
      entity_id: binary_sensor.aken_arvuti
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.aken_telekas
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.aken_magala
      from: 'off'
      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 %}
2 Likes

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 %}