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.
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.
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.
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.
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