Automation with variable and a template sensor

Hi there,

I want to use OpenAI conversation to provide additional song information for the music that I play on Spotify.
I tested in the Developer Tools and it works great. Now I want to build an automation that retrieves the information automatically and show it in a Markdown card on my dashboard. The challenge is that the amount of characters in the variable can exceed 255 so it can’t be stored in a input_text. I found out that an attribute can store more characters.
My question is how do I make an automation that stores a variable in an attribute of an entity.

Thanks in advance.

Sander

1 Like

You can’t use an automation.
You need to use a triggered template sensor.
It works pretty much like an automation but it’s yaml only in configuration.yaml

Thanks! To get a better understanding of how it works. First, an automation will do the OpenAI request and store the result in a variable. Second, a triggered template sensor will trigger if the variable gets an update and then stores the variable in the attribute?

I don’t think you can use the automation.
The automation needs to store the text somewhere but it can’t do that.
You need to use the triggered template sensor to do it all.

Here is one example:

template:
  - trigger:
      - platform: "event"
        event_type: "android.intent_received"
        event_data:
          android.bluetooth.device.extra.DEVICE: "00:02:5B:01:5F:D1"
          intent: "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"
    sensor:
      - name: "Vue Battery Level"
        unique_id: "Vue battery"
        device_class: "battery"
        unit_of_measurement: "%"
        state_class: "measurement"
        state: >- 
                {%- if trigger.event['data']['android.bluetooth.device.extra.BATTERY_LEVEL'] | int <= 0 -%}
                    {{ states('sensor.vue_battery_level') }}
                {%- else -%}
                    {{ trigger.event['data']['android.bluetooth.device.extra.BATTERY_LEVEL'] }}
                {%- endif -%}
                

I just found an example here on the forum that is a bit more complex and uses the attributes which I don’t.
You can probably use some parts of this.
How to list multiple calendar event in a template - Configuration - Home Assistant Community

What kind of additional song information are you looking for? And does it come from Spotify, or somewhere else?

If it’s from Spotify, then you might have a look at my SpotifyPlus Integration Services Provided List to see if it contains what you need. It exposes 99% of the services offered by the Spotify Web API, and they can all be called from HA automation scripts.

Also check out the UI Scripts page for an example of how to use / process the service response data that is returned.

Hope it helps,

Thanks for your advice, you have build a nice integration! I will definitely look at it. The idea with OpenAI was to generate some songfacts or pub quiz questions.

@Hellis81 thanks for the clarification. I discovered the manual event action in the automation. This way I can trigger the triggered-based template sensor and send the variable in the message of the event. But it seems that the amount of charaters is also limited.

1 Like

There is no character limit if you add it as an attribute, only the state is limited.

I ended up with the trigger-based template sensor suggested by Hellis81 and it works great. I had to limit the response to 600 characters, above this the text can get truncated.

  - trigger:
      - trigger: state
        entity_id:
          - media_player.spotify
        attribute: media_title
    condition:
      - condition: template
        value_template: "{{ state_attr('media_player.spotify', 'media_content_type') == 'music' }}"
    action:
      - action: conversation.process
        metadata: {}
        data:
          agent_id: conversation.chatgpt
          text: >-
            Vertel een anekdote over het nummer
            {{state_attr('media_player.spotify','media_title')}} van
            {{state_attr('media_player.spotify','media_artist')}}. 
            Geef het antwoord in maximaal 600 karakters.
        response_variable: factresponse
    sensor:
      - name: OpenAI spotify
        state: songfacts
        attributes:
          fact: "{{ factresponse.response.speech.plain.speech | string }}"
type or paste code here

The result on my dashboard

Thanks for your help!

Sander

1 Like