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