Hi community friends,
I’ve taken myself up for a challenge.
I’ve put a door sensor in the mailbox and now would like the sensor to trigger an automation triggering a variable and funny TTS announcement (going through OpenAI) on my home group speakers when the mailman delivers mail.
I’ve been trying with the help of ChatGPT but ain’t getting there. Need some real experts advice here there I’m not very familiar yet with coding and YAML.
Below is my automation YAML-code
alias: Brievenbus Klep Humoristisch Bericht
description: Grappig TTS-bericht bij brievenbusgebeurtenis.
triggers:
- entity_id: binary_sensor.brievenbus_klep_deur_2
to: "on"
trigger: state
actions:
- data:
entity_id: input_text.openai_prompt
value: >-
De brievenbus klep ging net open. Maak een grappige opmerking over de
postbode die iets heeft afgeleverd.
action: input_text.set_value
- target:
entity_id: sensor.openai_text_response
action: homeassistant.update_entity
data: {}
- data:
level: debug
message: >
Prompt sent to OpenAI: De brievenbus klep ging net open. Maak een
grappige opmerking over de postbode die iets heeft afgeleverd.
action: system_log.write
- data:
prompt: >
De brievenbus klep ging net open. Maak een grappige opmerking over de
postbode die iets heeft afgeleverd.
action: rest_command.openai_generate_text
- delay:
hours: 0
minutes: 0
seconds: 3
- data:
level: debug
message: >
OpenAI Response: {{ states('sensor.openai_text_response') |
default('Geen antwoord ontvangen.') }}
action: system_log.write
- data:
entity_id: media_player.home_group
cache: false
language: nl
message: >
{{ states('sensor.openai_text_response') | default('Er ging iets mis met
de grap!') }}
action: tts.google_translate_say
mode: single
Below is the code from configuration.yaml
rest_command:
openai_generate_text:
url: "https://api.openai.com/v1/chat/completions"
method: POST
headers:
Authorization: "Bearer [REMOVEDMYKEYBUTISVALIDANDWORKINGBOUGHTCREDITS]"
Content-Type: "application/json"
payload: >
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "Je bent een grappige assistent voor slimme huisgebeurtenissen. Je reageert altijd met geestige opmerkingen die specifiek zijn voor de gebeurtenis. Antwoord altijd in het Nederlands."
},
{
"role": "user",
"content": "{{ prompt | default('Er is geen gebeurtenis meegegeven!') }}"
}
],
"max_tokens": 50,
"temperature": 1.0,
"top_p": 0.9,
"frequency_penalty": 0.5,
"presence_penalty": 0.5
}
content_type: "application/json"
sensor:
- platform: rest
name: OpenAI Text Response
resource: https://api.openai.com/v1/chat/completions
method: POST
headers:
Authorization: "Bearer [REMOVEDMYKEYBUTISVALIDANDWORKINGBOUGHTCREDITS]"
Content-Type: "application/json"
payload: >
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "Je bent een grappige assistent voor slimme huisgebeurtenissen. Je reageert altijd met geestige opmerkingen die specifiek zijn voor de gebeurtenis. Antwoord altijd in het Nederlands."
},
{
"role": "user",
"content": "{{ states('input_text.openai_prompt') | default('') }}"
}
],
"max_tokens": 50,
"temperature": 1.0,
"top_p": 0.9,
"frequency_penalty": 0.5,
"presence_penalty": 0.5
}
value_template: "{{ value_json.choices[0].message.content | default('Geen antwoord ontvangen.') }}"
scan_interval: 3600
tts:
- platform: google_translate
language: "nl"
input_text:
openai_response:
name: OpenAI Response
max: 255
So I’m getting back a creative response from OpenAI but it says it’s missing specific instructions (about the mailman delivering mail.)
Respons I’m getting below freely translated:
It looks like you didn't specify a specific event. I'm all set to make you laugh, but I do need a little context. Try
When testing, I get these log errors now:
Logger: homeassistant.helpers.service
Source: helpers/service.py:303
First occurred: 5:42:43 PM (2 occurrences)
Last logged: 5:59:00 PM
Referenced entities input_text.openai_prompt are missing or not currently available
and also
Logger: homeassistant.components.rest_command
Source: components/rest_command/__init__.py:154
integration: RESTful Command (documentation, issues)
First occurred: 5:42:48 PM (2 occurrences)
Last logged: 5:59:03 PM
Error. Url: https://api.openai.com/v1/chat/completions. Status code 400. Payload: b'{\n "model": "gpt-4",\n "messages": [\n {\n "role": "system",\n "content": "Je bent een grappige assistent voor slimme huisgebeurtenissen. Je reageert altijd met geestige opmerkingen die specifiek zijn voor de gebeurtenis. Antwoord altijd in het Nederlands."\n },\n {\n "role": "user",\n "content": "De brievenbus klep ging net open. Maak een grappige opmerking over de postbode die iets heeft afgeleverd.\n"\n }\n ],\n "max_tokens": 50,\n "temperature": 1.0,\n "top_p": 0.9,\n "frequency_penalty": 0.5,\n "presence_penalty": 0.5\n}'
Thanks in advance for your (home) assistance