Combining 2 Automations with Alexa TTS and variables

Try as I might, I can’t seem to get the magic to work. Using a variable open/closed in the spoken phrase is eluding me. It should be simple and obvious, but nothing I’ve tried works.


Tts_open ="Front Gate is Opening"
Tts_closed="Front Gate is Completely Closed"

Any insight would be great, or I just live with 2 automations, though I’d like to know how to do this in the future.

  alias: TTS When Gate Starts to Open
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.gate_open_sw
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: notify.alexa_media_everywhere
    data:
      data:
        type: announce
      message: Front Gate is Opening
  mode: single
- id: '1600813852279'
  alias: TTS When Gate Closed
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.gate_open_sw
    from: 'on'
    to: 'off'
  condition: []
  action:
  - service: notify.alexa_media_everywhere
    data:
      data:
        type: announce
      message: Front Gate is Completely Closed
  mode: single
  alias: TTS When Gate is Opened or Closed
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.gate_open_sw
  condition: []
  action:
  - service: notify.alexa_media_everywhere
    data:
      data:
        type: announce
      message: Front Gate is {{'Opening' if trigger.to_state.state == 'on' else 'Completely Closed'}}

Just keep in mind that if you intend to test this automation by calling it with the automation.trigger service (using Developer Tools > Services), it will fail to work.

That’s because the Trigger State Object only exists if the automation’s trigger is activated. In other words, trigger.to_state.state will be undefined if you trigger the automation “manually”.

Fascinating. Trying now.

Edit: worked first time. Learned some new! Awesome!

1 Like

Glad you like it.

Please consider marking my (previous) post with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. This helps other users find answers to similar questions.

try this