Template Condition last triggered doesn't work

I don’t know if is it a bug or feature?!
I have a script, that send me the names of open windows per telegram.
That script is not the issue, that works well.

I want to start the script when its beginning to rain.
Also this works fine.

But i wont to start the script maximun only one time per hour. If in the meantime the rain stops and start agian.

I would like to use a template condition to calculete when the script was last triggered.

{{ (as_timestamp(now()) - as_timestamp(state_attr('script.regen_benachrichtigung','last_triggered'))) | default(0)  | int > 3600}}

The Template Editor shows that the result ist false.

alias: Garten - Regen Benachrichtigung
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.regenmelder_regen
    from: 'off'
    to: 'on'
condition:
  - condition: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(state_attr('script.regen_benachrichtigung','last_triggered')))
      | default(0)  | int > 3600}}
action:
  - service: script.regen_benachrichtigung
    data: {}
mode: single

But if I use the template in an Automation the condition doesn’t work anymore?!

It doesn’t matter if the condition is true or false. Always the Action will be executed.

It looks like that you can’t test a templete condition, beause the result is always this message?!

template value should be a string for dictionary value @ data[‘value_template’]. Got None

The trace looks also very strange?

Is my template not correct? Or it’s not possible to use such kind of template as a condition?

Thanks in advance.

This is a known issue with the automation editor:

Ok, testing doesn’t work. It’s a cosmetic issue.
But what about the template condition itself.
Why is my automations not working?

What does this give in the template editor:

 {{ (as_timestamp(now()) - as_timestamp(state_attr('script.regen_benachrichtigung','last_triggered'))) | int(0) }}

btw, you’re probably better with this imho so you don’t have a hard coded name in there.

{{ as_timestamp(now()) - as_timestamp(state_attr(this.entity_id,'last_triggered')) | int(0) > 3600 }}

Except you should supply a default for the filter.

yep. :slight_smile: Too quick to type. haha

As I wrote. Depends on the last triggered time the result is True or False.

You could also cut out the timestamp conversion and use:


{{ now() - this.attributes.last_triggered | default(now(), true) > timedelta(hours=1) }}
1 Like

Yeah but that’s not what I asked you to test.

:man_facepalming:

3385

The same behavior by using you template.

{{ now() - state_attr('script.regen_benachrichtigung','last_triggered') > timedelta(hours=1) }}

The condition is not fulfill → result = false.

But the actions are executed anyway?!

Can you post the full automation trace?

Won’t this will fail if there is no last_triggered. timedelta is nice but need to ensure defaults and checks are in there.

What exactly? The step details?

There’s a “Download Trace” (arrow icon), top right of the trace page.

1 Like
{
  "trace": {
    "last_step": "action/0",
    "run_id": "9443f561d772691058dfe101f9920344",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2022-06-09T12:09:30.216274+00:00",
      "finish": "2022-06-09T12:09:30.276153+00:00"
    },
    "domain": "automation",
    "item_id": "1621165589883",
    "trigger": null,
    "trace": {
      "trigger": [
        {
          "path": "trigger",
          "timestamp": "2022-06-09T12:09:30.216718+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.garten_regen_benachrichtigung",
              "state": "on",
              "attributes": {
                "last_triggered": "2022-06-09T12:08:41.734737+00:00",
                "mode": "single",
                "current": 0,
                "id": "1621165589883",
                "friendly_name": "Garten - Regen Benachrichtigung"
              },
              "last_changed": "2022-06-09T12:08:29.100922+00:00",
              "last_updated": "2022-06-09T12:08:41.779885+00:00",
              "context": {
                "id": "01G545VS05HWG0EY3DJJTKQ7TW",
                "parent_id": "01G545VS006J809MQ378T08WVC",
                "user_id": null
              }
            },
            "trigger": {
              "platform": null
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2022-06-09T12:09:30.223985+00:00",
          "child_id": {
            "domain": "script",
            "item_id": "regen_benachrichtigung",
            "run_id": "28960080bbe5043475c67c4b863bd646"
          },
          "changed_variables": {
            "context": {
              "id": "01G545X8B8RG8NNCJ9XYZWXN8E",
              "parent_id": "01G545X8B2142G9NHGBQHPFQ2W",
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "script",
              "service": "regen_benachrichtigung",
              "service_data": {},
              "target": {}
            },
            "running_script": true,
            "limit": null
          }
        }
      ]
    },
    "config": {
      "id": "1621165589883",
      "alias": "Garten - Regen Benachrichtigung",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": "binary_sensor.regenmelder_regen",
          "from": "off",
          "to": "on"
        }
      ],
      "condition": [
        {
          "condition": "template",
          "value_template": "{{ now() - state_attr('script.regen_benachrichtigung','last_triggered') > timedelta(hours=1) }}"
        }
      ],
      "action": [
        {
          "service": "script.regen_benachrichtigung",
          "data": {}
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01G545X8B8RG8NNCJ9XYZWXN8E",
      "parent_id": "01G545X8B2142G9NHGBQHPFQ2W",
      "user_id": null
    }
  },
  "logbookEntries": []
}

If you run the automation manually it skips the triggers and conditions and just performs the actions.

If you use the trigger automation service in Developer Tools > Services you can choose not to skip the conditions,

1 Like

You’re absolutely right, I’ve updated the template in my original post

But by default now() means.
When the script was never triggered before. There is no value for last triggered.
now() - now() = 0
now() - now() > timedelta(hours=1) → FALSE
That means that the automation will never be executed?!