How can I use math to generate a value for a notification to my phone?

I recently got the Aqara pet feeder and I want to send a notification to my phone whenever it goes off. As far as I can see the best way to do this is to monitor when cat_feeder_portions_per_day increases as the actual feeding event doesn’t publish anything to MQTT unfortunately.

alias: Notify cat was fed
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cat_feeder_portions_per_day
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state|int < trigger.to_state.state|int }}"
    alias: Only trigger if the value increased (don't notify when it resets at midnight)
action:
  - service: notify.mobile_app_pixel_7
    data:
      message: >-
        Meow! Cat was fed "{{ trigger.to_state.state|int - trigger.from_state.state|int }}"
        portions!
      title: Cat feeder
mode: single

As you can see I want the difference between the new portions_per_day and the old one to publish as part of the notification, but I’m getting a validation error.
What’s the right syntax?

You need to get rid of the " in the message. The >- header makes the " unnecessary, and including them can cause interpretation issues. You can also use the not_to/not_from keys to exclude “unknown” and “unavailable” which might trigger warnings from your templates.

alias: Notify cat was fed
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cat_feeder_portions_per_day
    not_from:
      - unavailable
      - unknown
    not_to:
      - unavailable
      - unknown
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state | int < trigger.to_state.state | int }}"
    alias: Only trigger if the value increased (don't notify when it resets at midnight)
action:
  - service: notify.mobile_app_pixel_7
    data:
      message: >-
        Meow! Cat was fed {{ trigger.to_state.state | int - trigger.from_state.state | int }}
        portions!
      title: Cat feeder
mode: single
2 Likes

Huge thanks my friend, this was the solution!