Adding formula results to message contents of an automation

Hi,

Can I include the result of a formula in the contents of a message I am sending to TTS from an automation?

something like this: xxx days left until vacation

where xxx is the formula: july 1st, 2021 - today

Thanks,
Rich F

Yes.

Thatā€™s not a ā€œformulaā€. The example you posted shows a string. Do you want to pass the string as a variable (to a script) or does it represent the state value of some other entity you have?

How you doing Taras?

July 1, 2021 is just arbitrary date I picked. It is stored nowhere.

I entered the following in Developer Tools -> Template

{{ (as_timestamp("2021-07-01 00:00:00") - as_timestamp(now())) / 86400 }}

and got result of 110.33374605195665 which is pretty close but is not an integer value.

What is the syntax to precede the message text " days left until vacation" with the 110.33374605195665 after formatting it to look like as an integer?   Is that possible?

In VBscript I think you would use Cstr(Cint()) & ā€™ days left until vacationā€™

Thanks,
Rich F

Try this (slightly extended)

{% set days = ((as_timestamp("2021-03-13 21:00:00") - as_timestamp(now())) / 86400) | int %}
{{ days }} {{'day' if days == 1 else 'days' }} left until vacation.

Taras,

That worked in under Developer Tools OK. But when I entered it into automations.yaml I get a ā€˜bad indentation errorā€™ for the attribute "message: " The script is to run weekly up until 7/1/2021.

- id: test01
  alias: test01
  description: 'test01'
  trigger:
  - platform: time
    at: '16:35:00'
  condition:
  - condition: template
    value_template: '{{ as_timestamp(now()) <= as_timestamp("2021-07-01 00:00:00") }}''
  - condition: and
    conditions:
    - condition: time
      weekday:
      - fri
  action:
  - service: notify.alexa_media
    data:
      target:
      - media_player.computer_room_dot
      data:
        type: tts
      message: {% set days = ((as_timestamp("2021-07-01 00:00:00") - as_timestamp(now())) / 86400) | int %} {{ days }} {{'day' if days == 1 else 'days' }} left until vacation.
  mode: single
  

Rich F

action:
  - service: notify.alexa_media
    data:
      target:
      - media_player.computer_room_dot
      data:
        type: tts
      message: > 
        {% set days = ((as_timestamp("2021-07-01 00:00:00") - as_timestamp(now())) / 86400) | int %}
        {{ days }} {{'day' if days == 1 else 'days' }} left until vacation.
  mode: single

And change your conditions, they are AND by default.

  condition:
  - condition: template
    value_template: '{{ as_timestamp(now()) <= as_timestamp("2021-07-01 00:00:00") }}'
  - condition: time
    weekday:
      - fri

Edit: By the way, Iā€™m not Taras :wink:

Sorry about that.

FWIW, hereā€™s an alternative way of calculating days using datetime objects.

      message: > 
        {% set d = (now().replace(month=7, day=1, hour=0, minute=0, second=0) - now()).days %}
        {{d}} day{{'' if d == 1 else 's'}} until vacation.

Taras,

I got it work using your code snippet. I just realized the GUI is just too finicky in creating automations and did it using File Editor.

Thanks,
Rich F

Glad to hear it worked.

As a separate issue, I believe you have misunderstood the purpose of the Solution tag. You marked your last post as the Solution but it contains no explanation or example.

The whole point of a Solution is to direct users to the one post in the entire thread that describes the cause of the problem and/or offers a way to resolve it. Your last post contains neither yet itā€™s the one that other users will be directed to.

1 Like