How to use different words in a TTS message for attribute

So I have this message working (mostly)

  - service: script.audio_notify
    data_template:
      tts_msg: "The {{ trigger.to_state.attributes.friendly_name }} is: {{ trigger.to_state.state }}"
      mplayer: "main"

But it says, the Front door is ON, or Window is OFF, etc.

Makes sense since that is the state. Without creating another sensor, how do I get it to say Open ,instead of On, and closed instead of off?

  - service: script.audio_notify
    data_template:
      tts_msg: >-
        The {{ trigger.to_state.name }} is:
          {%- if trigger.to_state.state in ['on', 'off'] -%}
            {{ {'on': ' open', 'off': ' closed'}[trigger.to_state.state] }}
          {%- else -%}
            {{ ' unknown' }}
          {%- endif %}
      mplayer: "main"

Note that trigger.to_state.name is a shortcut for trigger.to_state.attributes.friendly_name.

1 Like

Thank you very much, I’m going to use this for my water leak automation as well.

- alias: Doors or windows left opened
  trigger:
    platform: state
    entity_id: binary_sensor.open_close_sensor_back_door,binary_sensor.open_close_sensor_front_door,binary_sensor.open_close_sensor_garage_door,binary_sensor.living_room_window_open_close,binary_sensor.office_window_open_close
    to: 'on'
    for:
      minutes: 2
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  - condition: state
    entity_id: group.primary_presence
    state: 'on'
  action:
  - service: script.audio_notify
    data_template:
      tts_msg: >-
        The {{ trigger.to_state.name }} is:
          {%- if trigger.to_state.state in ['on', 'off'] -%}
            {{ {'on': ' open', 'off': ' closed'}[trigger.to_state.state] }}
          {%- else -%}
            {{ ' unknown' }}
          {%- endif %}
      mplayer: "main"

- alias: Water Leak Detected
  trigger:
  - platform: state
    entity_id: binary_sensor.water_sensor_attic,binary_sensor.water_sensor_kitchen
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  action:
  - service: script.audio_notify
    data_template:
      tts_msg: >-
        The {{ trigger.to_state.name }} is:
          {%- if trigger.to_state.state in ['on', 'off'] -%}
            {{ {'on': ' Detected a leak.', 'off': ' leak has cleared.'}[trigger.to_state.state] }}
          {%- else -%}
            {{ ' unknown' }}
          {%- endif %}
      mplayer: "all"

Hmm, for the first automation this seems a bit unnecessary. I.e., the automation only triggers when one of the entities is β€˜on’ for two minutes, so trigger.to_state.state is only ever going to be β€˜on’. So this should do:

- alias: Doors or windows left opened
  trigger:
    platform: state
    entity_id: binary_sensor.open_close_sensor_back_door,binary_sensor.open_close_sensor_front_door,binary_sensor.open_close_sensor_garage_door,binary_sensor.living_room_window_open_close,binary_sensor.office_window_open_close
    to: 'on'
    for:
      minutes: 2
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  - condition: state
    entity_id: group.primary_presence
    state: 'on'
  action:
  - service: script.audio_notify
    data_template:
      tts_msg: "The {{ trigger.to_state.name }} is open."
      mplayer: "main"

Good call, I forgot i had changed the trigger. Initially I had it set to any state change, but it got annoying pretty quick. haha.

1 Like