Notification message subtracting trigger.from_state from trigger.to_state

I thought this was pretty straightforward, but templating is something I really struggle with.

I’m trying to use notifications service to tell me the number of coronavirus deaths in the last 24h (yeah, I know, I like being anxious I guess).

This is what I have right now:

  - data_template:
      message: Tivemos {{ trigger.to_state - trigger.from_state }} mortes em 24h.
      title: Atualização COVID-19!
    service: notify.mobile_app_mi_9

I tried a few other formats, but none worked. I even tried setting the state through dev tools, since I realised clicking EXECUTE in automations page would skip trigger, so no from_state and to_state would exist.

The error I got:

Covid: Error executing script. Unexpected error for call_service at pos 1: unsupported operand type(s) for -: 'State' and 'State'

Any help is appreciated.

{{ trigger.to_state.state | int - trigger.from_state.state | int }}

All states are strings, so you have to convert the string to an int or float in order to do math. You also missed .state at the end

Ok, I believe I got It. I tried with the .state in the end, but without integer/float.

I would Aldo like to thank you for clarifying exactly what the | does. I used before, but always empirically, based on other templates.

I’ll update my automation and test it as soon as get back home.

Thank you.

It worked.Thank you so much.