Automation notification on change state - include previous and current value?

I’m looking to include both the current and previous state value in a notification when a sensor value changes. I have mocked up a simple automation using speedtest. Then below does NOT give me the “from” value when I use states('trigger.from_state'), states('trigger.from_state.state').

Is the trigger value NOT available in the notification section, or do I have something incorrect here?

Thank you kindly,
CDL

service: persistent_notification.create
data_template:
  title: Network Speed Alert
  message: XFinity Download Speed Changed to {{states('sensor.speedtest_download')}}, from {{states('trigger.from_state')}}`

If the automation is using a State Trigger to monitor the state-changes of sensor.speedtest_download, then this is how to report the sensor’s current and previous values:

  message: 'XFinity Download Speed Changed to {{trigger.to_state.state}}, from {{trigger.from_state.state}}'

For more information about the trigger variable when used with a State Trigger, refer to Templating - State.

3 Likes

When I change to this, I get:

Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

I’ve tried all sorts of combinations…

  1. Post the entire automation.
  2. How are you testing it? (The trigger variable is undefined if you are manually triggering the automation)

Was totally how i was testing it, thank you! Is there any way to do a conditional in the messages so I can test in demand, e.g.: if(trigger is nothing) {sensor.state} else {trigger.state} type logic?

service: notify.persistent_notification
data:
  title: Network Speed Alert
  message: >
    XFinity Download Speed Changed to {% if trigger is defined and
    trigger.from_state is defined -%}
      {{ trigger.to_state.state }}, from {{ trigger.from_state.state }}
    {%- else -%}
      {{ states('sensor.speedtest_download') }}
    {% endif %}

NOTE

The use of the hyphen in -%} is for whitespace control (include/exclude spaces and newlines in the rendered text).

Be advised that you don’t normally test automations by manually triggering them. For more information, refer to Troubleshooting Automations.