Al little help with sensor template syntax

Anybody could tell me what’s wrong here with this sensor template? I’m getting a syntax error…

  - platform: template
    sensors:
      lock_last_state:
        friendly_name: Dernière activité sur le lock
        entity_id:
          - sensor.lock_status
        value_template: >-
          {% if is_state('sensor.lock_status', 'Locked by RF') %}
            Verrouillé par Z-Wave
          {% if is_state('sensor.lock_status', 'Unlocked by RF') %}
            Déverrouillé par Z-Wave            
          {% if is_state('sensor.lock_status', 'Locked with Keypad by user 1') %}
            Verrouillé par Sylvain  
          {% if is_state('sensor.lock_status', 'Unlocked with Keypad by user 1') %}
            Déverrouillé par Sylvain   
          {% if is_state('sensor.lock_status', 'Locked with Keypad by user 2') %}
            Verrouillé par Josée   
          {% if is_state('sensor.lock_status', 'Unlocked with Keypad by user 2') %}
            Déverrouillé par Josée                
          {% if is_state('sensor.lock_status', 'Locked with Keypad by user 3') %}
            Verrouillé par invité  
          {% if is_state('sensor.lock_status', 'Unlocked with Keypad by user 3') %}
            Déverrouillé par invité             
          {% else %}
            NA   
          {% endif %}   

Error:

Configuration invalid
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘elif’ or ‘else’ or ‘endif’. The innermost block that needs to be closed is ‘if’.) for dictionary value @ data[‘sensors’][‘lock_last_state’][‘value_template’]. Got "{% if is_state(‘sensor.lock_status’, ‘Locked by RF’) %}\n Verrouillé par Z-Wave\n{% if is_state(‘sensor.lock_status’, ‘Unlocked by RF’) %}\n Déverrouillé par Z-Wave \n{% if is_state(‘sensor.lock_status’, ‘Locked with Keypad by user 1’) %}\n Verrouillé par Sylvain \n{% if is_state(‘sensor.lock_status’, ‘Unlocked with Keypad by user 1’) %}\n Déverrouillé par Sylvain \n{% if is_state(‘sensor.lock_status’, ‘Locked with Keypad by user 2’) %}\n Verrouillé par Josée \n{% if is_… (See ?, line ?). Please check the docs at Template - Home Assistant

The second, third, fourth, fifth, sixth and seventh if’s should be elif’s.

Awesome thanks, don,t know why I missed that lol

Maybe you could help doing what I want for the last one “{% else %}”
Is there a way to just show the actual state of “sensor.lock_status” if none of the previous conditions are shown? (instead of having to specify something)
I really just needed to change the “by user x” with the actual name of the user but there are other status useful like lock jammed, etc… I don’t want to specify all possible outcome…
Does it make sense?

{% else %}
  {{ states('sensor.lock_status') }} 
{% endif %}
1 Like

Perfect! simple enough lol… sorry, still learning…

Since you’ve been awesome, I’ll risk myself pushing this a little further…
My lock has the entity “lock.schlage_be469_touchscreen_deadbolt_locked”
in its states, the status is what I’m using:
2018-12-07%2011_09_13-Home%20Assistant

So what I did (and I think this is too complicated for nothing), I created a sensor to show the “lock_state” status:

  - platform: template
    sensors:
      lock_status:
        friendly_name: "Lock status"
        value_template: "{{ states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status }}" 

Then, I use this one for my template sensor:

  - platform: template
    sensors:
      lock_last_state:
        friendly_name: Dernière activité sur le lock
        entity_id:
          - sensor.lock_status
        value_template: >-
          {% if is_state('sensor.lock_status', 'Locked by RF') %}
            Verrouillé par Z-Wave
          {%  elif is_state('sensor.lock_status', 'Unlocked by RF') %}
            Déverrouillé par Z-Wave            
          {%  elif is_state('sensor.lock_status', 'Locked with Keypad by user 1') %}
            Verrouillé par Sylvain  
          {%  elif is_state('sensor.lock_status', 'Unlocked with Keypad by user 1') %}
            Déverrouillé par Sylvain   
          {%  elif is_state('sensor.lock_status', 'Locked with Keypad by user 2') %}
            Verrouillé par Josée   
          {%  elif is_state('sensor.lock_status', 'Unlocked with Keypad by user 2') %}
            Déverrouillé par Josée                
          {%  elif is_state('sensor.lock_status', 'Locked with Keypad by user 3') %}
            Verrouillé par invité  
          {%  elif is_state('sensor.lock_status', 'Unlocked with Keypad by user 3') %}
            Déverrouillé par invité             
          {% else %}
            {{ states('sensor.lock_status') }} 
          {% endif %}    

Ideally, I feel I could just create the last one using the original “lock.schlage_be469_touchscreen_deadbolt_locked” but I’m having a hard time templating using the sub state “lock_state”

Tried this but it doesn’t work:

  - platform: template
    sensors:
      lock_last_status:
        friendly_name: Status serrure
        entity_id:
          - lock.schlage_be469_touchscreen_deadbolt_locked
        value_template: >-
          {% if is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Locked by RF') %}
            Verrouillé par Z-Wave
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Unlocked by RF') %}
            Déverrouillé par Z-Wave            
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Locked with Keypad by user 1') %}
            Verrouillé par Sylvain  
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Unlocked with Keypad by user 1') %}
            Déverrouillé par Sylvain   
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Locked with Keypad by user 2') %}
            Verrouillé par Josée   
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Unlocked with Keypad by user 2') %}
            Déverrouillé par Josée                
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Locked with Keypad by user 3') %}
            Verrouillé par invité  
          {%  elif is_state('states.lock.schlage_be469_touchscreen_deadbolt_locked.attributes.lock_status', 'Unlocked with Keypad by user 3') %}
            Déverrouillé par invité             
          {% else %}
            NA   
          {% endif %}  

If possible, this would be the ideal setup instead of relying on a second sensor to get the sub state…
Thanks (again) in advance :slight_smile:

try:

sensors:
  lock_last_status:
    friendly_name: Status serrure
    value_template: >
      {% set lock_state = state_attr('lock.schlage_be469_touchscreen_deadbolt_locked','lock_status') %}
      {% set mapper = { 'Locked by RF': 'Verrouillé par Z-Wave',
                        'Unlocked by RF': 'Déverrouillé par Z-Wave',
                        'Locked with Keypad by user 1': 'Verrouillé par Sylvain',
                        'Unlocked with Keypad by user 1': 'Déverrouillé par Sylvain',
                        'Locked with Keypad by user 2': 'Verrouillé par Josée',
                        'Unlocked with Keypad by user 2':'Déverrouillé par Josée',
                        'Locked with Keypad by user 3': 'Verrouillé par invité',
                        'Unlocked with Keypad by user 3': 'Déverrouillé par invité' } %}
       {{ mapper[lock_state] if lock_state in mapper else 'N/A' }}
2 Likes

I get:

Configuration invalid
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘par’) for dictionary value @ data[‘sensors’][‘lock_last_status’][‘value_template’]. Got "{% set lock_state = state_attr(‘lock.schlage_be469_touchscreen_deadbolt_locked’,‘lock_status’) %} {% set mapper = { ‘Locked by RF’: ‘Verrouillé par Z-Wave’,\n ‘Unlocked by RF’: Déverrouillé par Z-Wave,\n ‘Locked with Keypad by user 1’: Verrouillé par Sylvain’,\n ‘Unlocked with Keypad by user 1’: ‘Déverrouillé par Sylvain’,\n ‘Locked with Keypad by user 2’: ‘Verrouillé par Josée’,\n 'Unlocked with Keypad by u… (See ?, line ?). Please check the docs at Template - Home Assistant

missed a few quotes, sorry, edited original answer, try again, works here:

1 Like

PERFECT!!! thank you so much.
And how would I display the actual state instead of “NA” in the line about “else”?
{{ mapper[lock_state] if lock_state in mapper else 'N/A' }}

actual state of the lock would be states('lock.schlage_be469_touchscreen_deadbolt_locked')

which might be possible by simply stating:

{{ mapper[lock_state] if lock_state in mapper else state }} and if not (think it won’t work since it refers to the state of the sensor, which is made by the mapper…) do

{{ mapper[lock_state] if lock_state in mapper else states('lock.schlage_be469_touchscreen_deadbolt_locked') }}

First one didn’t work.
Second one works but gives me the main status of the lock (locked or unlocked), not the sub status “lock_status”

Sorry, I realized I might not have been clear…
Basically, if any other state of the sub status “lock_status” then the ones I listed, I want it to display the content of this sub state.
2018-12-07%2016_16_09-Home%20Assistant
So that I don’t need to list every possible option that might be like “lock jammed”, etc… but still know what’s going on.

Original purpose for the template was to replace “user 1” by the actual name.

then use:

{{ mapper[lock_state] if lock_state in mapper else lock_state }}