Help with an automation with if - then

Hello everyone, I have a problem that I don’t understand with the following automation:

alias: Telegram - Mensaje Backups
description: ""
trigger:
  - platform: state
    entity_id:
      - var.estado_backup_docker
      - var.estado_backup_mayoral
      - var.estado_backup_local
      - var.estado_backup_rpi
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - service: notify.telegram
    data:
      message: 💾 {{ trigger.to_state.name }} - {{ trigger.to_state.state }}
  - if:
      - condition: template
        value_template: "{{ trigger.to_state.state != 'Success' }}"
    then:
      - service: notify.persistent_notification
        data:
          message: Backup fallido.
          title: "ATENCIÓN"
mode: parallel
max: 10

I want to receive a persistent notification when the state of the trigger is different from Success, but I’m receiving this persistent notification always, even when the state is Success. Which part is incorrect? Regards

Hi @DendelX

Could you use Developer Tools > States to check that the state returned by your sensors is Success and not success

Also, what text does the template {{ trigger.to_state.name }} - {{ trigger.to_state.state }} generate as it could be helpful. Does this need to be wrapped in quotes?

Quote your single line templates:

message: "💾 {{ trigger.to_state.name }} - {{ trigger.to_state.state }}"

I’ll check that but this is not the problem of the automation. This service works fine and I receive the message. The problem is the “if - then” that don’t works and it enter in the “then” when the status is Success. I don’t understand why because the automation traze shows that the condition doesn’t match but I have the persistent notification every day.

The trace you show above has the action skipping the then: block.

I’d try working with choose and not with if-then. It makes reading easier, and is around way longer.

Just try it, it’s not that big of a change:

action:
  - service: notify.telegram
    data:
      message: 💾 {{ trigger.to_state.name }} - {{ trigger.to_state.state }}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state != 'Success' }}"
        sequence:
          - service: notify.persistent_notification
            data:
              message: Backup fallido.
              title: "ATENCIÓN"

Are you certain that the value of trigger.to_state.state does not have a space character before or after the word Success?

Try this version:

  - if:
      - condition: template
        value_template: "{{ 'Success' not in trigger.to_state.state }}"

I’m sorry, but I have to ask! :smiley: How does your example account for a space in front or after? I didn’t know that “!=” is different to “not in”, or is it? :slight_smile:

Copy-paste the following into the Template Editor and experiment with it:

{% set a = 'Success' %}
(==) {{ a == 'Success' }}
(!=) {{ a != 'Success' }}
(in) {{ 'Success' in a }}
(not in) {{ 'Success' not in a }}


{% set a = 'Success ' %}
(==) {{ a == 'Success' }}
(!=) {{ a != 'Success' }}
(in) {{ 'Success' in a }}
(not in) {{ 'Success' not in a }}

To really see the difference, change the value of a to Success is not guaranteed.

1 Like

Thanks, that answers the question! :slight_smile: :+1: And good example btw. :smiley:

And after knowing it is a difference, I read up the Jinja docs again, and look at that:

in
Perform a sequence / mapping containment test

Thanks, learned something important! :slight_smile: :wave:

1 Like

Exact! the trace shows that the action has been skipped but I have the persistent notification!

I know that there are a lot of ways to do this but I like to understand why this option is not working

Not from that triggered event you don’t.

It was from another one.

ok ok but again… if you see the trace, you can see that the action has been skipped! why I receive the persistant notification?? The code could be wrong but the automation shouldn’t execute this part of code.

I have tried as you tell me, but I keep getting the persistent notification!

alias: Telegram - Mensaje Backups
description: ""
trigger:
  - platform: state
    entity_id:
      - var.estado_backup_docker
      - var.estado_backup_mayoral
      - var.estado_backup_local
      - var.estado_backup_rpi
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
  - service: notify.telegram
    data:
      message: 💾 {{ trigger.to_state.name }} - {{ trigger.to_state.state }}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state != 'Success' }}"
        sequence:
          - service: notify.persistent_notification
            data:
              message: Backup fallido.
              title: "ATENCIÓN"
mode: parallel
max: 10


b

I just realized that while in the yaml editor of the automation I have double quotes in some places and single quotes in others, in the trace they are shown interchanged. This is normal?

Post the automation’s trace file. It contains the details we need to see to understand its behavior.

Screenshot_20230828-144435~2

I think @tom_l is on to something here. :wink:

How many traces of that automation do you have? Maybe it’s not one automation… :slight_smile:

Fair enough, I can see why. :laughing:

Here is the trace, I can see that the if action returns false but I still have the persistent notification.

{

  "trace": {

    "last_step": "action/1/if/condition/0",

    "run_id": "0d59b683279dacb11ff6002b296e53af",

    "state": "stopped",

    "script_execution": "finished",

    "timestamp": {

      "start": "2023-08-29T06:14:19.739457+00:00",

      "finish": "2023-08-29T06:14:19.770190+00:00"

    },

    "domain": "automation",

    "item_id": "1686833960430",

    "trigger": "state of var.estado_backup_local",

    "trace": {

      "trigger/0": [

        {

          "path": "trigger/0",

          "timestamp": "2023-08-29T06:14:19.739685+00:00",

          "changed_variables": {

            "this": {

              "entity_id": "automation.telegram_mensaje_backup_mayoral",

              "state": "on",

              "attributes": {

                "id": "1686833960430",

                "last_triggered": "2023-08-29T06:14:10.728292+00:00",

                "mode": "parallel",

                "current": 0,

                "max": 10,

                "friendly_name": "Telegram - Mensaje Backups"

              },

              "last_changed": "2023-08-29T06:11:49.938289+00:00",

              "last_updated": "2023-08-29T06:14:10.758414+00:00",

              "context": {

                "id": "01H8ZYV6H7XPEDN5ME9HJAW0DT",

                "parent_id": "01H8ZYV6H3KVG8CYC3W5RH67CD",

                "user_id": null

              }

            },

            "trigger": {

              "id": "0",

              "idx": "0",

              "alias": null,

              "platform": "state",

              "entity_id": "var.estado_backup_local",

              "from_state": {

                "entity_id": "var.estado_backup_local",

                "state": "En proceso",

                "attributes": {

                  "event_name": "BEFORE",

                  "result_file": "",

                  "datetime": "2023-08-29T06:14:10.707057241+00:00",

                  "icon": "mdi:cloud-upload",

                  "friendly_name": "Estado Backup Local"

                },

                "last_changed": "2023-08-29T06:14:10.725097+00:00",

                "last_updated": "2023-08-29T06:14:10.725097+00:00",

                "context": {

                  "id": "01H8ZYV6H3KVG8CYC3W5RH67CD",

                  "parent_id": null,

                  "user_id": "1d9d4eaa60a24966aa8732687ec608e2"

                }

              },

              "to_state": {

                "entity_id": "var.estado_backup_local",

                "state": "Success",

                "attributes": {

                  "event_name": "AFTER",

                  "result_file": "/tmp/dup-bee3f0f1-e151-4be3-ad96-b66c9a6e1488",

                  "datetime": "2023-08-29T06:14:19.720219254+00:00",

                  "icon": "mdi:cloud-upload",

                  "friendly_name": "Estado Backup Local"

                },

                "last_changed": "2023-08-29T06:14:19.736486+00:00",

                "last_updated": "2023-08-29T06:14:19.736486+00:00",

                "context": {

                  "id": "01H8ZYVFAPGXRH9Z56S851QQR6",

                  "parent_id": null,

                  "user_id": "1d9d4eaa60a24966aa8732687ec608e2"

                }

              },

              "for": null,

              "attribute": null,

              "description": "state of var.estado_backup_local"

            }

          }

        }

      ],

      "condition/0": [

        {

          "path": "condition/0",

          "timestamp": "2023-08-29T06:14:19.739768+00:00",

          "result": {

            "result": true,

            "entities": []

          }

        }

      ],

      "action/0": [

        {

          "path": "action/0",

          "timestamp": "2023-08-29T06:14:19.744284+00:00",

          "changed_variables": {

            "context": {

              "id": "01H8ZYVFAV3DZCZPPZBSEPNY2P",

              "parent_id": "01H8ZYVFAPGXRH9Z56S851QQR6",

              "user_id": null

            }

          },

          "result": {

            "params": {

              "domain": "notify",

              "service": "telegram",

              "service_data": {

                "message": "💾 Estado Backup Local - Success"

              },

              "target": {}

            },

            "running_script": false

          }

        }

      ],

      "action/1": [

        {

          "path": "action/1",

          "timestamp": "2023-08-29T06:14:19.766827+00:00"

        }

      ],

      "action/1/if": [

        {

          "path": "action/1/if",

          "timestamp": "2023-08-29T06:14:19.766956+00:00",

          "result": {

            "result": false

          }

        }

      ],

      "action/1/if/condition/0": [

        {

          "path": "action/1/if/condition/0",

          "timestamp": "2023-08-29T06:14:19.767008+00:00",

          "result": {

            "result": false,

            "entities": []

          }

        }

      ]

    },

    "config": {

      "id": "1686833960430",

      "alias": "Telegram - Mensaje Backups",

      "description": "",

      "trigger": [

        {

          "platform": "state",

          "entity_id": [

            "var.estado_backup_docker",

            "var.estado_backup_mayoral",

            "var.estado_backup_local",

            "var.estado_backup_rpi"

          ]

        }

      ],

      "condition": [

        {

          "condition": "template",

          "value_template": "{{ trigger.from_state.state != trigger.to_state.state }}"

        }

      ],

      "action": [

        {

          "service": "notify.telegram",

          "data": {

            "message": "💾 {{ trigger.to_state.name }} - {{ trigger.to_state.state }}"

          }

        },

        {

          "if": [

            {

              "condition": "template",

              "value_template": "{{ trigger.to_state.state != 'Success' }}"

            }

          ],

          "then": [

            {

              "service": "notify.persistent_notification",

              "data": {

                "message": "Backup fallido.",

                "title": "ATENCIÓN"

              }

            }

          ]

        }

      ],

      "mode": "parallel",

      "max": 10

    },

    "blueprint_inputs": null,

    "context": {

      "id": "01H8ZYVFAV3DZCZPPZBSEPNY2P",

      "parent_id": "01H8ZYVFAPGXRH9Z56S851QQR6",

      "user_id": null

    }

  },

  "logbookEntries": []

}

That trace shows the following:

  • The automation was triggered by the value of var.estado_backup_local changing from En proceso to Success.

  • Its condition evaluated to true.

  • It executed its first action and sent a message via the Telegram integration.

  • It evaluated the second action’s if condition and the result was false.

  • The automation ended.

The trace shows no record of sending a “Backup fallido.” persistent notification.

If you are certain this is the trace file from the time when the automation did send a persistent notification, I suggest you post it as an Issue in Home Assistant’s GitHub Core repository.