Simple question about automation / conditions

I’m trying to avoid too frequent notification when something a changes to a sensor occurs and found this example on the internet. I believe it used to work (but maybe it never did…) but I now get an error on the “Condition” line,

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data['condition'][0]['value_template']. Got None. (See ?, line ?). 

because of the way the Name is included ["…"]. Can someone confirm what the correct syntax is? For a condition to reference itself?

- id: '1609160142372'
  alias: Email When Water Softener Empty
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.hx711_value
    above: '26'
    below: '27.5'
    for: 10 minutes
  condition:
  - condition: template
    #Error on line below... 
    value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.["Email When Water Softener Empty"].attributes.last_triggered | default(0)) | int > 3600)}}'

The entity id of the automation will be based on the alias, but not the same. It will (likely) be automation.email_when_water_softener_empty.

You can always initially skip the condition and use the template “tester” in the developer tools to see if the template works (and returns what you expected).

1 Like

Thanks @robertklep - I still have this on my “must fix” list :slight_smile:

About the template tester, not sure how I can use that to work this out. Surely the template tester will not recognise the alias as I assume it will only accept the {{ … }}?

Yes, but your issue is with the template in your automation, so you can enter it into the template tester to see what happens:

{{ (as_timestamp(now()) - as_timestamp(states.automation.["Email When Water Softener Empty"].attributes.last_triggered | default(0)) | int > 3600)}}

And (hopefully) in its fixed form:

{{ (as_timestamp(now()) - as_timestamp(states.automation.["email_when_water_softener_empty"].attributes.last_triggered | default(0)) | int > 3600)}}

or:

{{ (as_timestamp(now()) - as_timestamp(states.automation.email_when_water_softener_empty.attributes.last_triggered | default(0)) | int > 3600)}}

Thanks @robertklep - I tried that and the only version which does not give an error in the template debugger is version 3 BUT when I update the automation accordingly, the home-assistant.log now includes the line below so I’m guessing it’s not recognising the entity?

2021-03-22 12:46:52 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: required key not provided @ data['trigger'][1]['platform']. Got None. (See ?, line ?).

I’m not sure if that’s related to this particular automation, it seems to refer to an automation that has (at least) two triggers, the second of which is missing a platform key.

Thanks @Robert - it was a syntax error in my automation file. I think it works now. Thanks for your help.