Syntax questions for automations

Hello everyone,
following a nice disccusion with great support by some of the community members, I think I will be working on my coding in Jinja skills.

I am currently trying to get an automation to work but I am a bit stuck.
I am checking status changes of a bunch of entities and I want to adjust the message sent to my telegram/signal account based on the origin.
However, it seems that my code is not yet acceptable because HA won’t even save it when I use the visual editor/yaml editor in “Configuration - Automations”.

....
condition: []
  action:
  msgorigin = {{ trigger.to_state.attributes.friendly_name }}
  msgcontent = {{ trigger.to_state.state }}
  {% if 'climate' in msgorigin %}
    msgcontent = {{ trigger.to_state.attributes.temperature }}
#  {% else %}
#    bye
  {% endif %} 
  - service: notify.signal
    data_template:
      message: msgorigin: msgcontent
  - service: notify.telegramgroup
    data_template:
      message: msgorigin: msgcontent

So, as you can see I wanted to first define a default value for message origin and message content. If I am not mistaken I have to use templating, so {{ }} for this.
Then, if the entity contains “climate”, so climate.livingroom or climate.diningroom etc., I would like to see the temperature value rather than the new state.
Once this is done, I would like to send a message to telegram and signal.

My inital code without the If statement and without defining any default values worked

  condition: []
  action:
  - service: notify.signal
    data_template:
      message: '{{ trigger.to_state.attributes.friendly_name }}: {{ trigger.to_state.state
        }}'
  - service: notify.telegramgroup
    data_template:
      message: '{{ trigger.to_state.attributes.friendly_name }}: {{ trigger.to_state.attributes.temperature
        }}'

Could someone tell me what I am doing wrong? I thought I had correctly combined information from Jinja — Jinja Documentation (2.11.x) and other sources.

Thank you once again for your great support for an eager noobie!
Alex

Maybe I need to set the variable rather than just directly defining it?
Syntax definitely still wrong, but maybe this is “closer” to being correct?

  condition: []
  action:
  {% set msgorigin = {{ trigger.to_state.attributes.friendly_name }} %}
  {% if 'climate' in msgorigin %}
    {%set msgcontent = {{ trigger.to_state.attributes.temperature }} %}
  {% else %}
      {% set msgcontent = {{ trigger.to_state.state }} %}
  {% endif %} 
  - service: notify.signal
    data_template:
      message: {{ msgorigin }}: {{ msgcontent }}
  - service: notify.telegramgroup
    data_template:
      message: {{ msgorigin }}: {{ msgcontent }}
  mode: single

You have defined the variables incorrectly. The documentation for scripts explains the correct way: Variables

In addition, you are also referring to the variables incorrectly. They must be within a template.

I didn’t understand what the if-else in your example was attempting to achieve so I left it out of the following version (which has correct syntax). If you explain how you want the if-else to work, I can help you implement it.

action:
  - variables:
      msgorigin: '{{ trigger.to_state.attributes.friendly_name }}'
      msgcontent: '{{ trigger.to_state.state }}' 
  - service: notify.signal
    data:
      message: '{{ msgorigin }}: {{ msgcontent }}'
  - service: notify.telegramgroup
    data:
      message: '{{ msgorigin }}: {{ msgcontent }}'
1 Like

@123 thatnk you :slight_smile:

The devil is in the details. I was not that far off, but it is interesting to me, that I do not need to use “set” for the variables. Is this an HA thing or was the information I found not correct?

The If statement is supposed to do the following:
Check if the friendly name contains a certain string, e.g. “climate”.
→ If it does, set msgcontent to trigger.to_state.attributes.temperature
→ Else set msgcontent to trigger.to_state.state

This is supposed to pass on the relevant information, because for a vacuum I need to know if it is docked or cleaning, a washing machine is washing or done. But a thermostat despite being on or off, is most intersting when it comes to the temperature. So I want a differnet piece of information from a thermostat than from most other entities.

If you define variables within an option then you must use Jinja2’s format.


  - service: notify.signal
    data:
      message: 
        {% set something = now().hour %}
        {{ msgorigin }}: {{ msgcontent }} {{ something }}
1 Like
action:
  - variables:
      msgorigin: '{{ trigger.to_state.attributes.friendly_name }}'
      msgcontent: >
        {% if 'climate' in trigger.to_state.attributes.friendly_name %}
          {{ trigger.to_state.attributes.temperature }}
        {% else %}
          {{ trigger.to_state.state }}
        {% endif %}

EDIT: sorry, I was writing while you were answering. Is the following code also possible or is this not valid here?

action:
  - variables:
      msgorigin: '{{ trigger.to_state.attributes.friendly_name }}'
      - choose:
          # IF climate
          - conditions:
              - condition: trigger.to_state.attributes.friendly_name
                value_trigger.to_state.attributes.friendly_name: "climate."
            sequence:
              - msgcontent: trigger.to_state.attributes.temperature
        default:
          -  msgcontent: trigger.to_state.state
  - service: notify.signal
    data:
      message: '{{ msgorigin }}: {{ msgcontent }}'
  - service: notify.telegramgroup
    data:
      message: '{{ msgorigin }}: {{ msgcontent }}'

Or is this also wrong?

It’s wrong.

choose is for controlling the action’s execution flow, not for determining a variable’s value. Templates are used for that purpose.

1 Like

Is it possible to have multiple arguments in one If statement?

{% if 'climate' in trigger.entity_id and trigger.to_state.attributes.temperature > '4.5' %}

If the temperature is 4.5 and above, then send the temperature and if it is 4.5 and lower, then send the state (in this case “off”).

Yes, it’s permitted.

Okay, thank you. Can I somehow see if an attribute is a string or a value?
Because my example

{% if 'climate' in trigger.entity_id and trigger.to_state.attributes.temperature > 4.5 %}

does not work. So I am thinking that maybe the temperature is not a value but rather a string?

To give @123 Taras a little relief. :wink: :smiley: Thanks, you’re doing a fantastic job here. And even after two years with HA I’m still learning from your examples. :+1: :+1: :+1:

So, here we go:
To check if it is a number, use the developer tools and the entity_id (trigger won’t work here, so you have to check the actual entity):

{{ state_attr('sensor.mysensor', 'temperature') is number }}

If it’s not, one possibility is to use a filter (like int or float) to typecast the value to a number.
Like this:

and trigger.to_state.attributes.temperature | float > 4.5
1 Like

Hmmm, according to HA it is in fact a number. So my syntax has to be wrong.
I also tried

{% if ('climate' in trigger.entity_id) and (trigger.to_state.attributes.temperature | float > 4.5) %}

and

{% if ('climate' in trigger.entity_id) and (trigger.to_state.attributes.temperature > 4.5) %}

but that also does not work.

What exactly is “not working”? Means, how do see, it isn’t working? Do you try to run it in developer tools, because that isn’t working with trigger. .)

Hi,
no, I meant the automation was not working. So it did not correctly identify the condition and change the message sent.
I know found that the problem was that the automation was not being reloaded properly. I had restarted the server before making the final change as suggested and then the automation did not reload properly.
It seems to be working now!

Thank you @paddy0174 and @123 !

1 Like

I wanted to add a NOT condition according to the website but I am told that “Property conditions is not allowed”.

After the different triggers I added:

.
.
  - platform: state
    entity_id: climate.dining_room
    attribute: temperature
  - platform: state
    entity_id: climate.master_bedroom
    attribute: temperature
  condition: not
  conditions:
    - condition: state
      entity_id: vacuum.roborock_vacuum_s5e
      state: "Unavailable"
  action:
  - variables:
.
.

I wanted to have this condition “globally”, that’s why I did not write it directly below the trigger.
Is this not possible or did I make another mistake? The instructions Conditions - Home Assistant seem to suggest that this should be working.

Additional question: is it possible to use placeholders e.g. for entitiy_ids?
If e.g. I wanted a condition to apply to all entities with a certain string in them, could I write

entitiy_id: climate.*

or something similar?

You’re missing a condition: line. Look at the docs again:

1 Like

:man_facepalming:

1. Is it possible to
a) use asteriks or similar
b) combine entity_ids with and/or (not)?
e.g.

  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: sensor.* and not climate.*
        state: "Unavailable"

So in this example, it would basically be an “except” statement. So “do not notifiy me if a device becomes unavailable except if it is a climate control device”.

2. Is it also possible to use “For” as in triggers? So check if the condition applies for e.g. more than 1 minute?

3. If I state a “To” and “From”, is it an AND statement or an OR statement?
Example:
From: a
To: b
So will it fire only if the state changes from a to b or also when it changes from a to c or from d to b?

4. In addition, if e.g. I do not care if a device goes offline for 1 minute (my roborock goes offline/unavailable every night at 3:09) and I want to use the “for” condition, I want this to apply to both “To” and “From”, because after a minute it returns to “Docked” but I do not need that information since it was only down due to routine updating or something.
But the documentation states that

Please note, that when using from, to and for, only the value of the to option is considered for the time specified.

which suggests that once the device becomes available again and goes from “Unavailable” to “Docked”, a notification will be triggered.

5.
If a device becomes unavailable, is this even a state of the device or is it a system message? So is it even controllable via monitoring the state? Even if it is not, the questions above stand because I will use them to make all kinds of additional conditions :slight_smile:

6. I just found that my vacuum keeps notifying me that it is cleaning. So I get a new message every 30 sec.
I looked at the different trigger options and condition options, but I cannot seem to combine them to filter out these repeat messages.
I was thinking something along the lines of

  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: vacuum.roborock_vacuum_s5e
        state: "Unavailable"
      - condition: state
        entity_id: vacuum.roborock_vacuum_s5e
        from: "cleaning"    
        to: "cleaning"

But this is not allowed it seems.
Maybe something like

  condition:
    condition: not
    conditions:
      - condition: state
        entity_id: vacuum.roborock_vacuum_s5e
        state: "Unavailable"
      - condition: template
        value_template: trigger.to_state != trigger.from_state

?

Thank you all!!!

What is the generally preferred way of asking a larger number of questions in this forum? Given they are all linked to the same topic, should I keep it as one thread (this one) or split the questions into different topics?

  1. No, although you can build template triggers to do that.

  2. No. Conditions are “instant” checks. You need to build the “for” logic into your trigger.

  3. Yes, to: and from: are ANDed if both specified.

  4. I don’t understand: please provide code example of what you’re trying to do that doesn’t work.

  5. It depends. Have a look in Developer Tools / States, and play around in the template editor.

  6. Again, not entirely clear what you’re trying to do. Your value_template line is not correctly formatted: should be:

value_template: "{{ trigger.to_state != trigger.from_state }}"
1 Like