Automation template: empty "else" part

In an automation, I have the following action:

action:
  - service_template: >
        {% if is_state('group.lookingtv','on') %}
           scene.turn_on
        {% else %}
           ?? do nothing ??
        {% endif %}
     entity_id: scene.looking_tv

What can I put in the else part that does nothing and accepts an entity_id?

Donā€™t do it that way.

Use a state condition instead.

trigger:
[ your trigger here ]
condition:
  condition: state
  entity_id: group.lookingtv
  state: 'on'
action:
  service: scene.turn_on
  entity_id: scene.looking_tv
1 Like

You could also insert a script that is empty (ie does nothing)
Ot call homeassistant.update_entity: input_boolean.random_entity

1 Like

Why use a cludgey workaround when there is a simple way to do it?

1 Like

This works fine, thanks mate!

Yep I (almost) agree, just giving him alternatives.
This way he can test for himself and will learn the options /benefits

My action consists of multiple service calls. Your simple way will not work.

Move that action to the last in the list of actions and put the condition before it. You can put conditions in the action block. Donā€™t pollute your config with cludges.

action:
- service: ...

- service: ...

- service: ...

- condition: state
  entity_id: group.lookingtv
  state: 'on'
- service: scene.turn_on
  entity_id: scene.looking_tv
1 Like

I didnā€™t know that I can put conditions between services. Does this work:

  action:
# if the lights are on, turn them off
  - condition: state
    entity_id: group.tvschauen
    state: 'on'
  - service: homeassistant.turn_off
    entity_id: group.tvschauen

# if the lights are off, turn them on and apply the scene
  - condition: state
    entity_id: group.tvschauen
    state: 'off'
  - service: homeassistant.turn_on
    entity_id: group.tvschauen
  - service: scene.turn_on
    entity_id: scene.tv_schauen

You can use choose for that:

    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: group.tvschauen
                state: 'on'
            sequence:
              - service: homeassistant.turn_off
                entity_id: group.tvschauen
          - conditions:
              - condition: state
                entity_id: group.tvschauen
                state: 'off'
            sequence:
               - service: homeassistant.turn_on
                 entity_id: group.tvschauen
               - service: scene.turn_on
                 entity_id: scene.tv_schauen
4 Likes

Thank you, I did not know about ā€œchooseā€.

Iā€™m struggling with using homeassistant.update_entity when I test the template in Developer Tools. Basically, I want the else statement in this template to do nothing:

{% if not ('AMZN' or 'online' or 'GOOGLE' or 'PAYPAL')  in state_attr('sensor.sm_g977u_last_notification', 'android.text') %} 
          {% set amount_spent = state_attr('sensor.sm_g977u_last_notification', 'android.text')%}
          {{ amount_spent.split(' ')[2] }}
        {% else %}
          {% homeassistant.update_entity: sensor.amount_spent %}
        {% endif %}

However, I get the following feedback in Developer Tool template testing:

TemplateSyntaxError: Encountered unknown tag 'homeassistant'. Jinja was looking for the following tags: 'endif'. The innermost block that needs to be closed is 'if'.

Sorry, I am new to Jinja and this may just be a simple syntax error. Can someone please enlighten me with whatā€™s wrong with my code?

You are nearly there
A couple of things to note : -

The following is a print statement
{{ 'Hello World' }}
This prints something back to homeassistant such that it can be evaluated in 
another context e.g. a service, an entity_id or a value (other things 
are also possible)
You can also print plain text such as 
      - service: switch.turn_{{trigger.to_state.state}}
This determines if the triggering entity was turned on or off and converts it to
switch.turn_on or switch.turn_off accordingly
So the "switch.turn_" passes through to be resolved
But you will note that 'generally' the print part starts with '{{' and then 
ends with '}}'

You can also have straight evaluation steps, calculate this, assign that etc.
These are denoted by {% at the beggining and %} at the end
So 
{% set ex1 = 'Hello ' %}
{% set ex2 = 'World' %}
Note that the variables must start with a letter
{{ ex1 ~ ex2 }} prints - Hello World
Note that '=' assigns the value and ==, >, <, >=, <= are used to test statements
A final point is 
{# This is a comment and is ignored by the code #}
So,
Your else does not print anything and has nothing to evauate (as this doesn't 
mean anything to the jinja2 interpreter, just to homeassistant
Remove your outer brackets (and the '%' symbols) and see what you get

Thank you for your explanation. So when I remove curly brackets and % symbols like so:

        {% if not ('AMZN' or 'online' or 'GOOGLE' or 'PAYPAL')  in state_attr('sensor.sm_g977u_last_notification', 'android.text') %} 
          {% set amount_spent = state_attr('sensor.sm_g977u_last_notification', 'android.text')%}
          {{ amount_spent.split(' ')[2] }}
        {% else %}
          homeassistant.update_entity: sensor.amount_spent
        {% endif %}

I get the following string result:

homeassistant.update_entity: sensor.amount_spent

However, what Iā€™m expecting is that the else will be the previous result being unchanged (i.e. do nothing). Is that not the correct understanding for this use case?

Your template attempts to do two very different things:

  1. If true it reports a value:
    {{ amount_spent.split(' ')[2] }}
  2. If false it reports something that appears to be a service call (but is incorrect):
    homeassistant.update_entity: sensor.amount_spent

Where is this template being used?

The code is in my configuration.yaml and I use sensor.amount_spent (Friendly Name: Amount Spent) in a lovelace dashboard.

Basically, I want to see the Amount Spent and Merchant in brick and mortar stores. Hence, I obtain the desired string from my Discover Card notification if the notification does not contain ā€˜onlineā€™, ā€˜Paypalā€™, ā€˜Googleā€™, or ā€˜AMZNā€™. The amount spent is the correct dollar value when tested and passes the if condition. However, if it fails the if condition it clears out the Amount Spent instead of just stopping/doing nothing.

OK, but thatā€™s not particularly specific; itā€™s like saying ā€œItā€™s in Home Assistantā€.

Is it part of a:

  1. Template Sensor
  2. Script
  3. Automation
  4. Something else

Post the YAML of wherever that template is employed.

Itā€™s a template sensor. The code I pasted earlier was my testing in Developer tools. Here is currently what I have in my configuration.yaml:

#Discover Card Amount Spent
  - platform: template
    sensors:
      amount_spent:
       friendly_name: "Amount Spent"
       value_template: >
          {% if not ('AMZN' or 'online' or 'GOOGLE' or 'PAYPAL')  in state_attr('sensor.sm_g977u_last_notification', 'android.text') %} 
          {% set amount_spent = state_attr('sensor.sm_g977u_last_notification', 'android.text')%}
          {{ amount_spent.split(' ')[2] }}
          {% endif %}
  - platform: template
    sensors:
      amount_spent:
        friendly_name: "Amount Spent"
        value_template: >
          {% set amount = state_attr('sensor.sm_g977u_last_notification', 'android.text') %}
          {{ iif(amount is not search('AMZN|online|GOOGLE|PAYPAL', false), amount.split()[2], '') }}
  • If amount does not contain one of the listed text strings (case-sensitive), it converts amount to a list and reports index item 2.
  • If amount does contain one of the listed text strings, it reports an empty string.

Is that what you want?

Yes, thatā€™s correct. It reports the dollar value extracted from the notification. This is the expected result and it works correctly.

No, that is the current result but the desired result is that an empty string is NOT displayed. Instead, I want the result to be unchanged from the last time that the index item 2 was reported (i.e last value when if condition was true).

Hope that makes sense.