Execute action with multiple identities?

Hello, everyone, and greetings. I’m a newbie trying to understand how HA works and your comments are helping me a lot, but it’s not easy. I took advantage of a post by Phil Hawthorne, to whom I am very grateful for his blog, to try to create an automation to turn on a lamp connected to a switch-wifi. The automation and the states of an entity work perfectly, but it gets complicated when I do the whole process for my and my wife’s mobile. HA marks the two entities correctly, but the problem is that i don’t know to integrate the states of two entities in the trigger. I explain:

This is a part as an example:

    - alias: David acaba de llegar
      trigger: 
        - platform: state
          entity_id: device_tracker.david
          from: 'not_home'
          to: 'home'
        - platform: state
          entity_id: device_tracker.nuria
          from: 'not_home'
          to: 'home'
      action:
        - service: input_select.select_option
          data_template:
            entity_id: >
              {% if trigger.entity_id == 'device_tracker.david' %}
                input_select.david_status_dropdown
              {% else %}
                input_select.nuria_status_dropdown
              {% endif %}
            option: >
              {% if trigger.entity_id == 'device_tracker.david' %}
                {% if states.input_select.david_status_dropdown.state == 'Acaba llegar' %}
                  Casa
                {% else %}
                  Acaba llegar
                {% endif %}
              {% else %}
                {% if states.input_select.nuria_status_dropdown.state == 'Acaba llegar' %}
                  Casa
                {% else %}
                  Acaba llegar
                {% endif %}
              {% endif %}

If I use this, it works perfectly:

# Encender lampara al llegar a casa
- alias: Encender lampara
  trigger:
      platform: state
      entity_id: input_select.david_status_dropdown
      to: "Acaba llegar"
  condition:
    - condition: sun
      after: sunset
      before_offset: -01:00:00
  action:
      service: switch.turn_on
      entity_id: switch.xxxxxxxxxxxxxx

If I create a group and use it obviously does not work because the group will only appear as “home” or “not_home”

# Encender lampara al llegar a casa
- alias: Encender lampara
  trigger:
      platform: state
      entity_id: group.lampara
      to: "Acaba llegar"

Is it possible to indicate that an action will be executed when a group member changes their status to “Just arrived”?

You don’t need to use a group in the trigger.

You can make it so that either input_select can trigger the automation:

- alias: Encender lampara
  trigger:
    - platform: state
      entity_id: input_select.david_status_dropdown
      to: "Acaba llegar"
     - platform: state
      entity_id: input_select.nuria_status_dropdown
      to: "Acaba llegar"
  condition:
    - condition: sun
      after: sunset
      before_offset: -01:00:00
  action:
    service: switch.turn_on
    entity_id: switch.xxxxxxxxxxxxxx

It was an option but I thought that with your way both conditions have to be met and with one entity going to the required state it would be enough. Am I wrong?

triggers are always an ‘or’. If either (or any…) trigger goes true then the automation gets triggered. Then it checks the conditions to see if they are true. If yes, then the action is taken.

Tried and tested. So that a launcher does not work as “or” and works as “and”, for example when the two entities are marked as “away” must use some condition?

Thank you very much

If I understand you correctly…

you will never be able to make the triggers an “and”. they will always be “or”.

But, yes, you can put in conditions to control if the action is taken based on specified conditions. And you can use the same entity in the conditions as are in the trigger.

But be aware that the trigger only looks at a state change (from ‘off’ to ‘on’) at that moment in time. It then compares the states of the entities in the conditions to see if they are in the correct state at that moment. If the trigger occurs and the conditions aren’t met at that moment then the automation will never run until a trigger becomes true again.

First of all, I’m sorry for my level of English. I think I understand you, more or less.
I’ll explain myself better. In the first case, if only one status_dropdown is as away, the action is executed.
The second case is that we want that both status_dropdown have the same state to execute the action.

trigger
- platform: state
entity_id: input_select.david_status_dropdown
to: “Just arrived”
condition: and
conditions:
- condition: state
entity_id: input_select.nuria_status_dropdown
to: “Just arrived”

But we need the same with the second entity

Close…

trigger:
  - platform: state
    entity_id: input_select.david_status_dropdown
    to: "Just arrived"
condition: and
  conditions:
    - condition: state
      entity_id: input_select.nuria_status_dropdown
      state: "Just arrived"
action:
  - some action...

As I said, the trigger looks at the change in state so it needs a “to:”. the conditions look at the current state so it needs a “state:”

And to add in the second entity:

trigger:
  - platform: state
    entity_id: input_select.david_status_dropdown
    to: "Just arrived"
- platform: state
    entity_id: input_select.nuria_status_dropdown
    to: "Just arrived"
condition: and
  conditions:
    - condition: state
      entity_id: input_select.nuria_status_dropdown
      state: "Just arrived"
    - condition: state
      entity_id: input_select.david_status_dropdown
      state: "Just arrived"
action:
  - some action...

That way it will look at the change in state to “just arrived” for either entity and if it sees that state change it will look to see if the other entity is already in the “just arrived” state. If yes then the action is taken.

And I hope I don’t confuse things further but…

by default all conditions are “and” so unless you want to specify “or” in the conditions, you don’t need the “condition: and” line:

    trigger:
      - platform: state
        entity_id: input_select.david_status_dropdown
        to: "Just arrived"
    - platform: state
        entity_id: input_select.nuria_status_dropdown
        to: "Just arrived"
    condition:
      - condition: state
        entity_id: input_select.nuria_status_dropdown
        state: "Just arrived"
      - condition: state
        entity_id: input_select.david_status_dropdown
        state: "Just arrived"
    action:
      - some action...
1 Like

I get it now. Thank you for your patience. Greetings!

1 Like