How to trigger a notification based on template

Hi all,

I have a value template that results in a true or false.
is there a way to trigger a service call based on that template?

this is the template:

{{ state_attr('sensor.afvalinfo_papier','is_collection_date_today') }}

is there a simpel way to trigger on this?

im not sure if the template like this is enough? do I need to set “true” some where, so it knows when to trigger?
or is this always only trigger when its true?

should this fire when it is true?

- alias: NOTIFY papier buiten
  trigger:
    platform: time 
    at: '08:00:00'
  condition:
    condition: template
    value_template: "{{ state_attr('sensor.afvalinfo_papier','is_collection_date_today') }}"
  action:
    - service: notify.all_phones
      data:
        title: Afval Info
        message: "Het papier moet buiten gezet worden"

I’ve had some times where even tho the template rendered as “true” I still needed to make it explicitly return a boolean ‘true’ in the template.

I can’t explain why it happens intermittently I just know that it does.

So if the above doesn’t work for some reason you could use this:

value_template: >
  {% if state_attr('sensor.afvalinfo_papier','is_collection_date_today') == 'true' %}
    true
  {% else %}
    false
  {% endif %}

Make sure what the attribute says matches exactly including capitalization.

Sorry you’re correct, I found that I need a notify around 8 o clock, so the I need a trigger at time.
and a condition that it only fire’s when its true

so something like this then:

- alias: NOTIFY papier buiten
  trigger:
    platform: time 
    at: '07:30:00'
  condition:
    condition: template
    value_template: >
      {% if state_attr('sensor.afvalinfo_papier','is_collection_date_today') == 'true' %}
        true
      {% else %}
        false
      {% endif %}
  action:
    - service: notify.all_phones
      data:
        title: Afval Info
        message: "Het papier moet buiten gezet worden"

If those all match the real values in the states page for your entities then it looks OK.

Thank you, I will try this.

The problem here is whether the attribute is returning a boolean or the strings ‘true’/‘false’

- alias: NOTIFY papier buiten
  trigger:
    platform: time 
    at: '07:30:00'
  condition:
    - "{{ is_state_attr('sensor.afvalinfo_papier','is_collection_date_today', true) }}"
  action:
    service: notify.all_phones
    data:
      title: Afval Info
      message: "Het papier moet buiten gezet worden"

Try that, and if that’s no good try a single quote ' around the word true towards the end of the template.

I did make both, with 1 min interval. So I can see wichten works.

- alias: NOTIFY papier buiten 1
  trigger:
    platform: time 
    at: '07:30:00'
  condition:
    condition: template
    value_template: >
      {% if state_attr('sensor.afvalinfo_papier','is_collection_date_today') == 'true' %}
        true
      {% else %}
        false
      {% endif %}
  action:
    - service: notify.all_phones
      data:
        title: Afval Info 1
        message: "Het papier moet buiten gezet worden"
- alias: NOTIFY papier buiten 2
  trigger:
    platform: time 
    at: '07:29:00'
  condition:
    - "{{ is_state_attr('sensor.afvalinfo_papier','is_collection_date_today', true) }}"
  action:
    service: notify.mobile_app_cph2025
    data:
      title: Afval Info 2
      message: "Het papier moet buiten gezet worden"

It is my understanding that all attributes used in a template will always return the type that it thinks that it is.

So a string value ‘17’ will automatically be returned as an integer because it looks like an integer. And a string ‘True’ or ‘true’ should return a boolean true because it looks like a boolean.

It was changed a few releases back to work that way (to some small amount of confusion) but it doesn’t seem to be foolproof.

Not doubting you for a second, but if that’s the case why are all states strings?

My understanding was that states are ‘normalised’ to strings, but attributes remain their native types - therefore 17 could be an int or a string dependant on the component that’s generating them.

But that said, I have no idea where I got that idea from, so I’ll likewise happily believe your understanding too :slightly_smiling_face:

Yes, all states are strings. and all attributes retain their types.

but the template could produce different results and could end up with different return types.

Here is some further reading if you are interested:

the change was in v117 (beta) and default in v118.

it says that native types of attributes are retained.

and here is the PR that the change was based on: https://github.com/home-assistant/core/pull/41227

but here is another discussion which shows that it doesn’t necessarily depend on the actual type in the attribute (or even the state) but it depends on the conversion of the template into whichever type the renderer thinks that the type should be (in that example it converted the state string ‘None’ into the native type None so the state of the new entity was unknown as a result of the template).

So yes, attributes keep their types but the ultimate returned type could end up being a different type depending on the way the template renders it.

That said, the returned type of that template should be a boolean even if the attribute was a string (because it looks like a boolean) but it hasn’t always worked out like that.

I understand everything you said, but that is to do with the output of templates and the template I did returns a boolean regardless, that wasn’t the point/question I was making/asking, and therefore not what I thought you were replying to, and therefore not what I was replying to etc.

The point I was making was that the attribute is_collection_date_today might be a boolean, but it might be a string, so I guessed it was boolean but it might need quotes around it to make the boolean output of the template correct.

OK, then nevermind… :slightly_smiling_face:

1 Like

Number 2 works!
Maby there is a typo in number 1, but it didn’t work.

1 Like