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.
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
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?
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.
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).