How to store long text in MQTT sensor attribute?

I run into classic problem of having to store long (way above 255 characters) text for use in UI (it is ChatGPT generated album description). For obvious reasons it cannot be stored as state. As ChatGPT integration provides response as content of call back event, it cannot be used as attribute in template (or I did not found any trace of how to make it due to lack of set_attribute service). So in many places people are pointing to publishing long strings to MQTT… and here the problem starts, as I’m total noob here. I have broker configured and working properly, but this is copy/paste configuration required for some other integrations.
So here is what I have so far:
Automation to calll ChatGPT whenever media_album_name for selected medai player changes:

  - id: 'update_media_info_2'
    alias: Update Media Info 2
    initial_state: true
    variables:
      player: >-
        {% set result=states('input_select.media_players') %}
        {{ result }}
      artist: >-
        {% set string=states('input_select.media_players') %}
        {% set result=state_attr(string, 'media_artist') %}
        {{ result }}
      album: >-
        {% set string=states('input_select.media_players') %}
        {% set result=state_attr(string, 'media_album_name') %}
        {{ result }}
      output: >-
        {% set string=states('input_select.media_players') %}
        {% set string=string.split('.') %}
        {% set result="input_text." + string[1] + "_media_info" %}
        {{ result }}
    trigger:
    - platform: state
      entity_id:
        - media_player.audiocast_dlna
        - 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 250 characters
          callback_event: >-
            {{ player }}_response

Another automation triggered by ChatGPT response and publishing response to MQTT topic:

  - id: 'volumio_response'
    alias: Volumio Response
    trigger:
      - platform: event
        event_type: media_player.volumio_2_response
    action:
      - service: mqtt.publish
        data:
          topic: "txt/volumio_album"
          retain: true
          payload: "{{trigger.event.data.content | trim | replace('\"','')}}"

Finally MQTT sensor, that provides this reponse back, so it can easily be used.

mqtt:
  sensor:
    - name: "volumio_album"
      state_topic: "txt/volumio_album"

Above at least prooves that logic is OK, as it works as expected.
Now the problem is that with this sensor also response is limited to 255 characters… So I’d like to publish/read the response from ChatGPT to sensor attribute… and here I’m lost, whatever I tried it fails woth some errors and sensor is not created/updated. Any advise how to make it? Is it something to be changed on publishing side or sensor or (probavly) both?

1 Like

I wouldn’t try to do it with MQTT, just use a trigger based template sensor, where you set the attributes, as they can hold >255 chars.

See here the documentation:

Not tested, and I’m not an expert, but to show a direction:

template:
  - trigger:
      - platform: state
        entity_id:
          - media_player.audiocast_dlna
          - media_player.denon_heos_s750h
          - media_player.marantz_sacd30n
          - media_player.volumio_2
        attribute: media_album_name
    sensor:
      - name: media_info
        state: #whatever you want to use here as a state, templating is allowed
        attributes: 
          player: # templating is allowed here as well
          artist: ...

I hope that gives you an idea, I’m sorry I can’t be of further help, maybe someone else jumps in… :slight_smile: :wink:

1 Like

Thanks a lot Patrick! You indeed pointed me into th eright direction, I was not aware about templating trigger sensors and this allowed to put required text into attribute, as needed! The first automation could stay as is, the second could be eliminated and the sensor looks like that:

  - trigger:
      - platform: event
        event_type: media_player.volumio_2_response
    sensor:
      - name: volumio_album_description
        state: 'on'
        attributes:
          album_name: "{{trigger.event.data.content | trim | replace('\"','')}}"

Now I just need to finetune this for multiple media players I have.

1 Like