Automation variable to keep a state of an entity

Hello everyone,

I’ve setup the hass to a very decent level and now started playing with TTS. All my media_players are not google or anything. 1 is KODI, 1 is the TV and 1 is MPD(mopidy) which snapcasts to some devices.
I only care about the MPD to use TTS, but the problem is that mpd is my main radio streamer and it’s used many hours throughout the day, so having a tts break the stream and having manually to restart it, is a nuissance.
What I want to do (if it’s doable) is to keep the stream before TTS if it was playing and restart it after the TTS. Something like this:

- id: tempwarning
  alias: Play TTS and keep previous stream
  trigger:
  - platform: sensor
    entity_id: sensor.0x00158d00036b744d_temperature
    above: 28
  action:
  - service: tts.google_say
    data_template: >
      {% if is_state('media_player.mpd', 'playing') %}
#somehow keep the stream as it has a state "media_content_id"
#also have another that stores like a 1 or 0 to check later if the mpd was playing
      entity_id: media_player.mpd
      message: "The temperature has risen! Turn on the A/C"
      {% else %}
      service: tts.google_say
      entity_id: media_player.mpd
      message: "The temperature has risen! Turn on the A/C"
      {% endif %}
  - service_template: >
      {% if PREVIOUS CHECK WAS 1, which means it was playing %}
      media_extractor.play_media
      {% endif %}
      data:
        entity_id: media_player.mpd
        media_content_id: "VARIABLE FROM PREVIOUS STATE"
        media_content_type: "music" #or we could get that too, don't care really as I only stream radio

Don’t mind the suntax errors, I will fix them if there’s a solution for that.
Anyone?

You can store media_content_id in an input_text. Alternately, you can install a custom component called hass-variables which provides the ability to store data in variables.

The following thread uses hass-variables to store the media_player’s volume level prior to playing an announcement and then restoring the volume level. Perhaps it can be adapted to meet your requirements:

1 Like