Trigger attributes as template condition - Help

Hi All

I have created an automation the works correctly, however when restarting MQTT or HA the automation triggers. I am using the entity’s timestamp attribute change as the trigger and notice when it falsely triggers the trigger.from_state does not actually have a timestamp attribute at all, its not there. But obviously the trigger.to_state does - hence the automation triggers.

Can someone help or advise what is the correct syntax for a template condition to prevent the automation from triggering if the trigger attribute of timestamp is not present.

I have used this (which I know is not right):

{{ trigger.from_state.attributes.timestamp | float > 1  }}

When there is a timestamp it passes correctly, but when there is no timestamp it does actually prevent the automation from running but generates an error.

So what the correct way of templating this condition.

Thanks in advance.

I’m not 100% sure this will work with an attribute… But, I would approach this by altering the trigger using the not_from key. Using a more specific trigger has the benefit over using a condition, in that, it will reduce the number of mostly-pointless traces that are recorded.

trigger:
  - platform: state
    attribute: timestamp
    entity_id: sensor.YOUR_SENSOR
    not_from:
      - unknown
      - unavailable
      - none

Thanks Drew. Unfortunately it does not appear to, if I use the timestamp attribute alone the trigger works, if I attempt to add any form of ‘to’, ‘from’, ‘not_from’ etc it does not trigger.

Well… dang it. Have you tried either of the following for your condition?

{{ trigger.from_state.attributes.timestamp is defined  }}

or

{{ trigger.from_state.attributes.timestamp is not none }}
1 Like

No, I haven’t tried either of those to be fair as my knowledge on templates and all things related is pretty minimum. But both sound promising and I will try them tomorrow and report back.

Thanks for your input.

PS: Is there a way of using developer tools template section to define the trigger for testing these types of scenarios i wonder?

The first thing to do is just to look at the automation trace for one of the instances of the trigger you are trying to avoid. If it’s not already selected, select the trigger in the chart. Then at the bottom select Step Details > Changed Variables. If you scroll down you can see all the details from the trigger.

If you really want to use the Template editor tool, the easiest way is to get the trigger data in javascript format. You can do that by downloading the automation trace. Copy the trigger portion (everything after "trigger": and before "condition":), then paste it into the Template editor tool like:

{% set trigger = namespace( {
.... TRIGGER PORTION...
} ) %}

That will allow you to access the trigger variable in the editor the same way you would in templates in an automation.

1 Like

Perfect, thanks Drew, that’s great to know.

I will test out your initial 2 template condition ideas and feedback :+1:t2:

This worked perfectly - many thanks :+1:

1 Like