Various triggers to update various attributes in trigger template sensor?

Is it possible to use different triggers to update different attributes of triger template sensor? E.g. someting like to configure sensor twice with the same name and different set of attributes, each time having different trigger?
Problem I have is that I can use one, more frequent trigger to do all of this inside one sensor, but it will abuse ~10-fold paid per call external API, which I want to avoid. Obvious solution is to have 2 different template trigger sensors and combine their outputs in third one, regular template sesnor, but this is multiplying entities unnecessary…

If you are accessing an API use the rest integration. You can create as many sensors as you want from the one call to the resource. No template sensors required (use the value_template of each sensor).

@tom_l I was not fully clear… this specific case is for using of ChatGPT integrqation, so there is a payment associated with usage of OpenAI API, but I have no direct control over sensor creation. ChatGPT integration returns response via content of call back event, so I need to use template trigger sensors to populate attributes with answers. I use it for gathering music album description. So every time media_player album_name changes I call ChatGPT, but I’d like to have complete set of data about currently played media within one snsor, so also about current song. Unfortunatelly song (extracted from media_player) does not change in such sensor without trigger. That’s why I was thinking about different triggers for different attributes. Otherwise one more template sensor to consolidate all data from different sources might be better oprion.

I ass-u-me from the documentation that you could use trigger.id in the template sensor and update state or attributes conditionally based on that (e.g. attribute foo gets updated if the response contains bar, otherwise retain current value this.state.attributes['foo']).

Without more details of the sensor structure and the response you get from our favourite tool, we’re not going to be able to suggest any more than that.

So here is what I come to so far:
Automation that requests album info via ChatGPT integration when album name of one of listed media_players is changed:

  - id: 'update_media_info'
    alias: Update Media Info
    initial_state: true
    variables:
      player: >-
        {{ trigger.entity_id }}
      artist: >-
        {% set string=trigger.entity_id %}
        {% set result=state_attr(string, 'media_artist') %}
        {{ result }}
      album: >-
        {% set string=trigger.entity_id %}
        {% set result=state_attr(string, 'media_album_name') %}
        {{ result }}
      output: >-
        {% set string=trigger.entity_id %}
        {% set string=string.split('.') %}
        {% set result="input_text." + string[1] + "_media_info" %}
        {{ result }}
    trigger:
    - platform: state
      entity_id:
        - media_player.audiocast
        - media_player.denon_heos_s750h
        - media_player.marantz_sacd30n
        - media_player.volumio_2
      attribute: media_album_name
    condition: []
    action:
      - service: chatgpt.chat
        data:
          messages:
            - role: user
              content: >-
                Tell me about {{ artist }}'s album {{ album }} in less than 120 words
          callback_event: >-
            {{ trigger.entity_id }}_response

As you can see the response is provided as callback event named media_player.name_response
Here is sample event data received from intergation:

event_type: media_player.volumio_2_response
data:
  role: assistant
  content: >-
    Cantoma's self-titled debut album is a dreamy and eclectic mix of Balearic
    beats, exotica, and disco. Produced by Phil Mison, the album features a
    stellar line-up of collaborators, including respected musicians like Hush
    Forever, Bing Ji Ling, and Pathaan. The music is a soothing and tropical
    escape, blending disparate genres and sounds to create a unique sonic
    experience. Tracks like "Essarai" and "Maja" are sensual and sun-kissed,
    while "Pandajero" and "Early Till Late" have a more bohemian, free-spirited
    vibe. Overall, Cantoma is a must-listen for fans of chillout, world music,
    and electronic genres.
origin: LOCAL
time_fired: "2023-06-12T16:21:31.317041+00:00"
context:
  id: 01H2R6K71NGV01X0S1Q97Z4KCN
  parent_id: null
  user_id: null

So here (one sample of) template trigger sensor, that reads this response and put it into sensor attribute (album_description). Other attributes are taken directly from corresponding media_player entity attributes:

template:
  - trigger:
      - platform: event
        event_type: media_player.volumio_2_response
    sensor:
      - name: volumio_2_album_description
        state: 'on'
        attributes:
          album_description: "{{trigger.event.data.content | trim | replace('\"','')}}"
          album_title: "{{ state_attr('media_player.volumio_2', 'media_album_name') }}"
          artist_name: "{{ state_attr('media_player.volumio_2', 'media_artist') }}"
          song_title: "{{ state_attr('media_player.volumio_2', 'media_title') }}"
          album_art: "{{ state_attr('media_player.volumio_2', 'entity_picture') }}"

The problem is that if ‘media_album _name’ changes and in consequence new response is received the whole sensor is updated. Unlike normal template sensor this one does not update when attributes of media_player changes, so remaining attributes are updated only once per album, whis fine for all, but `song_title’ attribute, that changes more frequently. I could use it for triggering automation, but this would cause refreshing almum infomration on every song (which might be interesting experiment, as ChatGPT each time gives different rresponse), but is waste of tokens…
So final sensor looks like:

Try this version. It’s untested so it may require further adjustments.

template:
  - trigger:
      - platform: event
        event_type: media_player.volumio_2_response
      - platform: state
        entity_id:
          - media_player.volumio_2
      attribute: media_title
    sensor:
      - name: volumio_2_album_description
        state: 'on'
        attributes:
          album_description: >
            {{ trigger.event.data.content | trim | replace('\"','')
               if trigger.platform == 'event' else
               this.attributes.album_description | default('') }}
          album_title: "{{ state_attr('media_player.volumio_2', 'media_album_name') }}"
          artist_name: "{{ state_attr('media_player.volumio_2', 'media_artist') }}"
          song_title: "{{ state_attr('media_player.volumio_2', 'media_title') }}"
          album_art: "{{ state_attr('media_player.volumio_2', 'entity_picture') }}"

@taras, thanks for pushing into right direction! Finally I ended up with following code for sensor:

template:
  - trigger:
      - platform: event
        event_type: media_player.volumio_2_response
      - platform: state
        entity_id:
          - media_player.volumio_2
        attribute: media_title
    sensor:
      - name: volumio_2_album_description
        state: 'on'
        attributes:
          album_description: >
            {% if trigger.platform == 'event' %}
              {{ trigger.event.data.content | trim | replace('\"','') }}
            {% else %}
              {{ this.attributes.album_description | default('') }}
            {% endif %}   
          album_title: "{{ state_attr('media_player.volumio_2', 'media_album_name') }}"
          artist_name: "{{ state_attr('media_player.volumio_2', 'media_artist') }}"
          song_title: "{{ state_attr('media_player.volumio_2', 'media_title') }}"
          album_art: "{{ state_attr('media_player.volumio_2', 'entity_picture') }}"

So only slight change in album_description, but overal logic wokrd fine. Thanks a lot!

1 Like

You’re welcome!

BTW, ChatGPT’s evocative description of Cantoma’s music led me to listen to a few tracks. Overall I enjoyed the debut album; I found a few tunes to be a bit repetitive but, as the saying goes, they can’t all be gems. Thanks for the musical tip.

1 Like