Help with an automation Action not working

I have the following code in An action but I can’t figure out what is wrong, I can’t get the damn automation to return the correct info, it always return the first message Moteur bloque

Any one can point me to my probably stupid mistake ?

service: input_text.set_value
target:
  entity_id: input_text.serrure_cabanon_last_status
data:
  value: >
   {% if "{{ trigger.event.data.event_label == Lock state: Lock jammed }}" %}
      Moteur bloque
    {% elif "{{ trigger.event.data.event_label == Keypad lock operation }}" %}
      Barree manuellement
    {% elif "{{ trigger.event.data.event_label == Keypad unlock operation }}" %}
      Code utilisateur entré
    {% elif "{{ trigger.event.data.event_label == Manual lock operation }}" %}
      Barree manuellement
    {% elif "{{ trigger.event.data.event_label == Manual unlock operation }}" %}
      Débarrée manuellement
    {% elif "{{ trigger.event.data.event_label == RF lock operation }}" %}
      Barrée par RF
    {% elif "{{ trigger.event.data.event_label == RF unlock operation }}" %}
      Débarrée par RF
    {% elif "{{ trigger.event.data.event_label == Auto lock locked operation }}" %}
      Barrée automatiquement
    {% elif "{{ trigger.event.data.event_label == Battery level status: Charge battery soon }}" %}
      Changer les piles bientôt
    {% elif "{{ trigger.event.data.event_label == Battery level status: Charge battery now }}" %}
      Changer les piles immédiatement
    {% else %}
      Message inconnu
    {% endif %}

Whats does the trace say?

I tried == ’ ’ and == " " both with either, there is nothing returned …

even if I have a return value “Moteur bloque”, the trace say “This step was not executed and so no further trace information is available.”

I know the value returned from trigger.event.data.event_label is “Keypad unlock operation” as i saved the info in a helper to make sure I have something returned from the automation

try:

{% if trigger.event.data.event_label == 'Lock state: Lock jammed'  %}
      Moteur bloque

?

2 Likes

Like armedad showed you are just stating a string in the first if statement.
So the line if "{{ trigger.event.data.event_label == Lock state: Lock jammed }}" will just read as: if "text" and with no compare it will result in True and this show the first message

Also might be cleaner to use an array?

1 Like

You rock… Thank you SOO much, I am just starting learning YAML code and I am an old guy… sometimes it takes a lot of practice and repeat to get something in this head.

As I mentionned to armedad, I am new to all this YAML, i come from Homeseer so a lot of difference.

Would you mind pointing me as to how to work with arrays in YAML if you have some time ? I like to learn and try putting good pratctices right at beginning

Thank you for your time, relly appreciate

no worries, we’re all just learning. there’s always someone more knowledgeable. i sometimes hesitate about answering here because my answers are rarely “the best”

but dujith is right that an array would be better. play around a lot with developer-tools->template it’s a god send for learning.

if you go there try this:

{% set my_test_json = {
  "temperature": 25,
  "unit": "°C"
} %}

{{ my_test_json["temperature"] }}

so in your case you could do something like this (warning, untested freehand pseudocode)

{% set your_array = {
  "keypad lock operation": 'barree manuellement',
  "keypad unlock operation": 'code utilisateur entre'
} %}

then your code simply does this instead:

{{ your_array[ trigger.event.data.event_label ] }}
2 Likes

Thanks a lot, i’ll play around that
have a good one

Ok I think I still need help, I can’t get the action being executed, I tried the following in developper tool and it retruns the correct string: “barree manuellement

{% set Lock_State_Array = {
    "Lock state: Lock jammed": 'Moteur bloqué',
    "keypad lock operation": 'barree manuellement',
    "keypad unlock operation": states('input_text.serrure_cabanon_last_user_name') ,
    "Manual lock operation": 'Barrée manuellement' ,
    "Manual unlock operation": ' Débarrée manuellement',
    "RF lock operation": 'Barrée par RF',
    "RF unlock operation": 'Débarrée par RF',
    "Auto lock locked operation": 'Barrée automatiquement',
    "Battery level status: Charge battery soon": 'Changer les piles bientôt',
    "Battery level status: Charge battery now": 'Changer les piles immédiatement'
   } %}
   
{{ Lock_State_Array[ "keypad lock operation" ] }}

if i try the exact same block of code in an automation where i try to set value to results of the array and look at the traces, the action never get executed:
(i replaced the device_id per xxxx for security)

alias: Serrure Cabanon Set Last Status
description: ""
trigger:
  - platform: event
    event_type: zwave_js_notification
condition:
  - condition: template
    value_template: |
      {{trigger.event.data.device_id == 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}}
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: input_text.set_value
    target:
      entity_id: input_text.serrure_cabanon_last_status
    data:
      value: |
        {% set Lock_State_Array = {
        "Lock state: Lock jammed": 'Moteur bloqué',
        "keypad lock operation": 'barree manuellement',
        "keypad unlock operation": states('input_text.serrure_cabanon_last_user_name') ,
        "Manual lock operation": 'Barrée manuellement' ,
        "Manual unlock operation": ' Débarrée manuellement',
        "RF lock operation": 'Barrée par RF',
        "RF unlock operation": 'Débarrée par RF',
        "Auto lock locked operation": 'Barrée automatiquement',
        "Battery level status: Charge battery soon": 'Changer les piles bientôt',
        "Battery level status: Charge battery now": 'Changer les piles immédiatement'
        } %} 
     {{ Lock_State_Array[ "Keypad lock operation" ]}}
mode: single

My final goal being trigger.event.data.event_label replaces the hard coded entry text “keypad lock operation” that I tried for testing purposes as I can’t find how to construct the request would it be:

1- {{ Lock_State_Array[ trigger.event.data.event_label ]}},
2- {{ Lock_State_Array[ {{trigger.event.data.event_label}} ]}}

or else …

Without testing it, but you are not using the the exact same block of code. Identation matter.

1 Like

I hope someday the indentation thing will get in my head, I always forget to look at these
thanks

good job sticking with it. Isn’t the resulting code more elegant? you get extra code beauty points :smiley:

Definitly, this is why I wanted to understand and get it to work !!!