Create a "choose" (or if) using trigger.platform

Hi,

I am trying to make an alarm automation, triggered by sensors and by a timer.

I want the behavior to be different if it’s the timer or a sensor.
I have tried to do it using “trigger.platform”, both with if and choose, but either it gives me an error, or it does not recognize the states.

With this code, always select the last option.
The strange thing is that if I give input_select the value {{ trigger.platform }} and if value is correct, but it has not executed the conditionals.

automation:
  - alias: Camaras Record Telegram
    mode: queued
    trigger:
      - platform: time
        at: "13:00:00"
      - entity_id: binary_sensor.comedor_puerta
        platform: state
        to: 'on'
      - entity_id: binary_sensor.comedor_movimiento1
        platform: state
        to: 'on'
      - entity_id: binary_sensor.comedor_movimiento2
        platform: state
        to: 'on'
      - entity_id: binary_sensor.pasillo_movimiento
        platform: state
        to: 'on'
      - entity_id: binary_sensor.cocina_puerta
        platform: state
        to: 'on'
      - entity_id: binary_sensor.cocina_movimiento
        platform: state
        to: 'on'
      - entity_id: binary_sensor.dormitorio_simple_movimiento
        platform: state
        to: 'on'
      - entity_id: binary_sensor.ania_movimiento
        platform: state
        to: 'on'
    action:
      - service: input_text.set_value
        data:
          entity_id: input_text.telegram_disparador_alarma
          value: >
            {% if is_state('trigger.platform', 'time') %}
              TEMPORIZADOR
            {% elif is_state('trigger.platform', 'state') %}
              S_{{ state_attr(trigger.entity_id, 'friendly_name') }}
            {% elif is_state('trigger.platform', 'device') %}
              D_{{ state_attr(trigger.entity_id, 'friendly_name') }}
            {% else %}
              LAST_{{ trigger.platform }}
            {% endif %}

Searching for information I have found the choose method, but in this case, it gives me an error when verifying the code (I do not find any condition that includes trigger.platform

    action:
      - choose:
          - conditions: {{ is_state('trigger.platform', 'time') }}
            sequence:
              - service: input_text.set_value
                data:
                  entity_id: input_text.telegram_disparador_alarma
                  value:  {{ trigger.platform }}
          - conditions: {{ is_state('input_text.trigger_platform', 'state') }}
            sequence:
              - service: input_text.set_value
                data:
                  entity_id: input_text.telegram_disparador_alarma
                  value:  {{ trigger.platform }}

Do I have to put an id yes or yes to each trigger and not use trigger.platform ?
Am I trying to do something impossible or am I just doing it wrong?

Let’s see if someone can help me

I do my automations like this. This should provide a template to modify your automation.

- id: 'Bar Lights On and Off based on motion and switch'
  alias: Bar Lights - Turn on and off based on stairwell light.
  description: Turn on and off all the lights in the bar area
  trigger:
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.shellymotionsensor_60a42395fc6a_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_ne_corner_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: switch.stairwell_light
      from: 'off'
      to: 'on'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_ne_corner_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off'
      platform: state
      entity_id: switch.stairwell_light
      from: 'on'
      to: 'off'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.shellymotionsensor_60a42395fc6a_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'     
  condition: []
  action:
  - choose:
    - conditions:
        - "{{ trigger.id == 'bar_on' }}"
      sequence:
        - service: switch.turn_on
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_on
          target:
            entity_id:
            - light.bar_pendant_light
            - light.bar_back_bar_cans
          data:
            brightness_pct: 50
    - conditions:
        - "{{ trigger.id == 'bar_off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    - conditions:
        - "{{ trigger.id == 'bar_off1' }}"
        - "{{ states('binary_sensor.bar_main_motion_sensor_motion') == 'off' }}"
        - "{{ states('binary_sensor.shellymotionsensor_60a42395fc6a_motion') == 'off' }}"
        - "{{ states('binary_sensor.bar_ne_corner_motion') == 'off' }}" 
        - "{{ states('binary_sensor.bar_stairwell_motion_sensor_motion ') == 'off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    default: []
  mode: queued
1 Like

You’re just setting it up incorrectly:

First, your actions need to be specified with an action: key.

Second, if you put quotes around the trigger variable it is treated like a string, not like the variable that it is. Also the trigger variable already represents a state object, so you do not need to use the state_attr() function on it.

automation:
  - alias: Camaras Record Telegram
    mode: queued
    trigger:
      - platform: time
        at: "13:00:00"
      - entity_id: binary_sensor.comedor_puerta
        platform: state
        to: 'on'
      - entity_id: binary_sensor.comedor_movimiento1
        platform: state
        to: 'on'
      - entity_id: binary_sensor.comedor_movimiento2
        platform: state
        to: 'on'
      - entity_id: binary_sensor.pasillo_movimiento
        platform: state
        to: 'on'
      - entity_id: binary_sensor.cocina_puerta
        platform: state
        to: 'on'
      - entity_id: binary_sensor.cocina_movimiento
        platform: state
        to: 'on'
      - entity_id: binary_sensor.dormitorio_simple_movimiento
        platform: state
        to: 'on'
      - entity_id: binary_sensor.ania_movimiento
        platform: state
        to: 'on'
    condition: []
    action:
      - service: input_text.set_value
        data:
          entity_id: input_text.telegram_disparador_alarma
          value: >
            {% if trigger.platform == 'time' %}
              TEMPORIZADOR
            {% elif trigger.platform == 'state' %}
              S_{{ trigger.attributes.friendly_name }}
            {% elif trigger == 'device' %}
              D_{{ trigger.attributes.friendly_name }}
            {% else %}
              LAST_{{ trigger.platform }}
            {% endif %}
1 Like

It is true, as I have been doing so many tests, I have been copying the code in pieces, and I have left the action.
In the code I use, if it is present.

I am convinced that I have tried as you indicate, but I am going to try it again.
I see it much easier to use IF than CHOOSE

Perfect, it has worked as you have indicated.
I’m going to look better what mistake I’ve made, so I learn for another time.

And thank you very much for your help.

The If/Then action will not work as well for your automation as Choose because you have more than 2 possible end points… If/Then does not support elif only then and else. The following analogous automation using the Choose action would be significantly longer than the template-based version but it would work just as well.

action:
  - choose:
      - conditions:
          - "{{ trigger.platform == 'time' }}"
        sequence:
          - service: input_text.set_value
            data:
              entity_id: input_text.telegram_disparador_alarma
            value: "TEMPORIZADOR"
      - conditions:
          - "{{ trigger.platform == 'state' }}"
        sequence:
          - service: input_text.set_value
            data:
              entity_id: input_text.telegram_disparador_alarma
            value: "S_{{ trigger.to_state.attributes.friendly_name }}"
      - conditions:
          - "{{ trigger.platform == 'device' }}"
        sequence:
          - service: input_text.set_value
            data:
              entity_id: input_text.telegram_disparador_alarma
            value: "D_{{ trigger.to_state.attributes.friendly_name }}"
    default:
      - service: input_text.set_value
        data:
          entity_id: input_text.telegram_disparador_alarma
        value: "LAST_{{ trigger.platform }}"

Although, you haven’t specified any triggers that aren’t State or Time, so half of those options will go unused and are surplus to necessity.

1 Like

You can simplify the service call’s template if you assign a trigger variable to your Time Trigger and State Trigger.

automation:
  - alias: Camaras Record Telegram
    mode: queued
    trigger:
      - platform: time
        at: "13:00:00"
        variables:
          msg: 'TEMPORIZADOR'
      - platform: state
        entity_id:
          - binary_sensor.comedor_puerta
          - binary_sensor.comedor_movimiento1
          - binary_sensor.comedor_movimiento2
          - binary_sensor.pasillo_movimiento
          - binary_sensor.cocina_puerta
          - binary_sensor.cocina_movimiento
          - binary_sensor.dormitorio_simple_movimiento
          - binary_sensor.ania_movimiento
        to: 'on'
        variables:
          msg: 'S_{{ trigger.to_state.name }}'
    action:
      - service: input_text.set_value
        target:
          entity_id: input_text.telegram_disparador_alarma
        data:
          value: '{{ msg }}'

NOTE

I agree with Didgeridrew that your original example contains superfluous code. It lacks a Device Trigger, so the value of trigger.platform cannot be “device” therefore there’s no need to test for it in the service call. Similarly, the automation uses only two types of trigger platforms, Time and State, so there’s no need for the service call’s template to have a catchall {% else %} section because it’s already limited to handling just two kinds of trigger platforms.

1 Like

My goodness, all I have left to learn (and know how to use), because having so many entity_id in the trigger was nonsense, the good method is that of @123

It’s already late for me, and I’m going to keep them busy for the next two days, but I look at everything very carefully.

But on a first read, I haven’t understood the problem with if/then/else.
I have tried to launch the automation with the timer and with a manual launch from the “developer tools” and it has worked for me.
I can’t test with the sensors yet, because I’m not home.

My final code, based on what you have suggested, is as follows:

automation:
  - alias: Camaras Record Telegram
    mode: queued
    trigger:
      - platform: time
        at: "13:00:00"
      - platform: state
        entity_id:
          - binary_sensor.comedor_puerta
          - binary_sensor.comedor_movimiento1
          - binary_sensor.comedor_movimiento2
          - binary_sensor.pasillo_movimiento
          - binary_sensor.cocina_puerta
          - binary_sensor.cocina_movimiento
          - binary_sensor.dormitorio_simple_movimiento
          - binary_sensor.ania_movimiento
          - binary_sensor.timbre
        from: 'off'
        to: 'on'
    condition:
      - condition: state
        entity_id: alarm_control_panel.xiami_gateway_alarm
        state: armed_away
    action:
      - service: input_text.set_value
        data:
          entity_id: input_text.telegram_disparador_alarma
          value: >
            {% if trigger.platform == 'time' %}
              TEMPORIZADOR
            {% elif trigger.platform == 'state' %}
              {{ trigger.attributes.friendly_name }}
            {% else %}
              MANUAL
            {% endif %}
      - service: script.camaras_record
      - delay: "00:00:30"
      - service: script.camaras_telegram
      - delay: "00:00:10"
      - service: input_text.set_value
        data:
          entity_id: input_text.telegram_disparador_alarma
          value: ""

The reason for the ELSE is that when I launched the automation from the development panel, it gave me errors when the conditions were not met (with trigger).
It helps me to have a different final message (already done, it is better to cover all the options), and to put a value in it that will show me the results of the variables.

And I’m not tired, thank you very much.

Your original example’s template uses an if statement.

          value: >
            {% if is_state('trigger.platform', 'time') %}
              TEMPORIZADOR
            {% elif is_state('trigger.platform', 'state') %}
              S_{{ state_attr(trigger.entity_id, 'friendly_name') }}
            {% elif is_state('trigger.platform', 'device') %}
              D_{{ state_attr(trigger.entity_id, 'friendly_name') }}
            {% else %}
              LAST_{{ trigger.platform }}
            {% endif %}

It contains the following errors:

  • trigger.platform is a variable that contains the platform name of whichever trigger was responsible for triggering the automation. However you put quotes around it like this 'trigger.platform' which makes it a literal string and no longer a variable.

  • You can’t use is_state to compare the value of trigger.platform to a value. It’s exclusively for comparing the state value of an entity_id to a value.

Here’s a corrected version:

          value: >
            {% if trigger.platform == 'time' %}
              TEMPORIZADOR
            {% elif trigger.platform == 'state' %}
              S_{{ state_attr(trigger.entity_id, 'friendly_name') }}
            {% elif trigger.platform == 'device' %}
              D_{{ state_attr(trigger.entity_id, 'friendly_name') }}
            {% else %}
              LAST_{{ trigger.platform }}
            {% endif %}

The automation contains only a State Trigger and a Time Trigger. There’s no Device Trigger or other kind so the template can be reduced to this:

          value: >
            {% if trigger.platform == 'time' %}
              TEMPORIZADOR
            {% else %}
              S_{{ trigger.to_state.name }}
            {% endif %}
1 Like

Hello,
Indeed, I have used the quotation marks incorrectly, there was already a point where you do not see what you have in front of you.
Regarding the “Device”, as the ELSE status always skipped me, I added the DEVICE as a test.

In the end I have left the code as indicated above.

Regards,

Hi,

I have finally been able to try to make the sensors missing, and I find that with {{ trigger.attributes.friendly_name }} it gives me an error:

Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘attributes’

With the variable {{ state_attr(trigger.entity_id, ‘friendly_name’) }} it has not given me an error.

The trigger variable doesn’t have an attributes property.

When using a State Trigger, the trigger variable has the following properties. They’re all properties of the State Object.

You could use trigger.to_state.attributes.friendly_name or simply trigger.to_state.name.

1 Like