Automation trigger on Rest sensor attribute values

I’m having trouble triggering the automation based on attribute values from my REST sensor.

I’m getting the waste removal info via the API which returns dates for next waste removal (per waste type).

I created template trigger for the automation which set the ‘true’ value correctly but it just wont trigger automatically:

alias: Telegram - Biological
description: ""
trigger:
 - platform: template
   value_template: >-
     {% if state_attr('sensor.waste_info','biological') ==
     now().strftime('%Y-%m-%dT00:00:00') %}true{% endif %}
condition: []
action:
 - service: notify.telegram
   data:
     message: Waste removal
     title: Biological!
mode: single

This is rest sensor configuration:

  - platform: rest
    resource_template: ...url... 
    method: POST
    name: waste_info
    payload: '{..somedata...}'
    headers:
      Content-Type: application/json
      Accept-Encoding: gzip, deflate, br
    value_template: "{{ value_json.status }}"
    json_attributes:
      - glass
      - paper
      - biological
      - packaging
      - mixed

Any idea how can I make it work? Thanks!

This is a better way to format the trigger template:

   value_template: >
     {{ state_attr('sensor.waste_info','biological') ==
     now().strftime('%Y-%m-%dT00:00:00') }}

It will return true/false.

Yours only returns nothing (which will generate an error) or true.

I can’t vouch for the correctness of the template as you have not shown us what the attribute value is.

1 Like

Thanks. I fixed the template. It still wont trigger though.

When does a rest sensor actually getting the new values? Per time period or per manual request (when the sensor is needed).

These are the current values of the attributes:

glass: null
paper: '2023-07-21T00:00:00'
biological: '2023-07-18T00:00:00'
packaging: null
mixed: '2023-07-21T00:00:00'
friendly_name: waste_info

It’ll only trigger when the template changes from false to true. As the attribute is already on the current day, it starts off being true so won’t trigger this cycle.

Personally, I’d write it thus (although this still might not trigger for today, depending on how that sensor operates):

trigger:
  - platform: state
    entity_id: sensor.waste_info
    attribute: biological
  - platform: time
    at: "00:01:00"
condition:
  - "{{ state_attr('sensor.waste_info','biological')[:10] == now().strftime('%Y-%m-%d') }}"
action:
  - service: notify.telegram
    data:
      message: Waste removal
      title: Biological!

Triggers when either the attribute or the current date changes (one minute into the day to avoid potential race conditions; or you could set it to when you want the notification); then checks if the attribute matches the date before continuing.

If you desperately want it to trigger today, add another trigger to my version above:

  - platform: event
    event_type: automation_reloaded
1 Like

That’s make sense. I tried changing the attributes via developer tools and I got the notification! Thank you!

I also set the update interval for the rest sensor to 100000s and then created another time automation to update the sensor manually every day at a specific time.

Bit fiddly, but I think this will work. You might get a double notification on Friday when you have paper and mixed due: if that is a problem we can sort that out.

trigger:
  - platform: state
    entity_id: sensor.waste_info
  - platform: time
    at: "00:01:00"
condition:
  - or:
    - "{{ state_attr('sensor.waste_info','glass')[:10] == now().strftime('%Y-%m-%d') }}"
    - "{{ state_attr('sensor.waste_info','paper')[:10] == now().strftime('%Y-%m-%d') }}"
    - "{{ state_attr('sensor.waste_info','biological')[:10] == now().strftime('%Y-%m-%d') }}"
    - "{{ state_attr('sensor.waste_info','packaging')[:10] == now().strftime('%Y-%m-%d') }}"
    - "{{ state_attr('sensor.waste_info','mixed')[:10] == now().strftime('%Y-%m-%d') }}"
action:
  - service: notify.telegram
    data:
      message: Waste removal
      title: "{{ states.sensor.waste_info.attributes|dictsort|selectattr(1,'defined')|selectattr(1,'eq',now().strftime('%Y-%m-%dT00:00:00'))|map(attribute=0)|map('title')|join(', ') }}!"

Last line gets a list of attribute keys whose value is the current date, converted to Title Case and joined with commas if there is more than one that matches.