Template multiple entities problem

hello there!
trying to add multiple enitities to template and i already know this not possible, but what is then solution for my problem? any help appreciated.

- service: climate.turn_off
  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')%}
        climate.elutuba,climate.trepp,climate.magala
      {% 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 %}

Create groups, turn the groups off.

Use service homeassistant.turn_off to cover whether the template returns a group or an entity.

im not sure i understand… smthg like this?

- service: homeassistant.turn_off
  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
      {% endif %}
- service: climate.turn_off
  data_template: 
    entity_id: >
      {% if is_state('binary_sensor.aken_telekas', 'on') %}
        climate.elutuba
      {% elif is_state('binary_sensor.aken_magala', 'on') %}
        climate.magala
      {% endif %}

Just the one service and template…

    - service: homeassistant.turn_off
      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 %}

kinda tried it last night… tried again.

Error executing service <ServiceCall homeassistant.turn_off: entity_id=>

Traceback (most recent call last):
File “/usr/lib/python3.6/site-packages/homeassistant/core.py”, line 1002, in _event_to_service_call
await service_handler.func(service_call)
File “/usr/lib/python3.6/site-packages/homeassistant/components/init.py”, line 117, in async_handle_turn_service
entity_ids = extract_entity_ids(hass, service)
File “/usr/lib/python3.6/site-packages/homeassistant/helpers/service.py”, line 103, in extract_entity_ids
return group.expand_entity_ids(hass, [service_ent_id])
File “/usr/lib/python3.6/site-packages/homeassistant/components/group/init.py”, line 204, in expand_entity_ids
domain, _ = ha.split_entity_id(entity_id)
ValueError: not enough values to unpack (expected 2, got 1)

I can usually do an educated guess from the error, but unfortunately I have no idea what that error message is trying to tell us, sorry.

@ReneTode may i ask your advise maybe?

i would love to give advice, but i never created a service in HA and templating is just what has driven me to Appdaemon.

i must say that i also am a bit puzzled about what you want to do.

so if you please want to describe what it is exactly what you try to do, i can think about a solution.

simple :slight_smile:
“aken” is in estonian window. so idea is to switchon/off climate based which window(s) open.

still not simple. :wink:

i dont know what you mean with climate based.

i do understand: “if the temperature in the livingroom is over 25 degrees, open a window.”

if i reread your code then i see:

if window is open do climate (which is an entity.)

i dont have any climate devices, but isnt that just another entity that just has a state?
and why a service? dont you just want to automate something?

:slight_smile: no.
actually yes, its part of automation. as i said… for example if i open window “binary_sensor.aken_arvuti” then it should switch off climate named “climate.elutuba” (no point to heat when window is open, right?). if i open all windows then it should switch off all climates. i have no problem write single edities to template and all works lika a charm, but problem is write 2 or 3 entitities…

so what you want is actually:

automation:
  trigger:
    window: open
  action:
    climate: off

you try to combine things that shouldnt be combined.
you cannot open several windows at the same time.

you actualy need a few automations.

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

the same for telekas > eletuba
a group for arvuti and telekas
and then the same for that group
and a group for arvuti, telekas and magala
and then an automation for that group.

so you end up with 4 simple automations and 2 small groups.

but i CAN open several windows at the same time…

Indeed, the template I posted earlier should work, or certainly something similar. Can you post the full automation and your groups please?

you probably cannot. the sensors would never be triggered at te same millisecond and even if they are, they will be seen in order by HA.

but even if you would be able to trigger them all at the same time, then the way i said, would work.

No, but they can all be physically open at the same time, just the most recent change triggers the automation, then the action is defined by which ones are open/closed.

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.