Simple "Tell me today's weather forecast"

Hi Everyone,
Thought I’d share my latest success with Rhasspy & HA. It is a simple “tell me today’s weather” that uses an intent script, and the Home Assistant National Weather Service (NWS) integration. I’m a beginner, so the really challenging part was getting the correct YAML to parse out the sensor data, but it seems to have been working well, and I wanted to share with the community to give something back.

First, install the NWS integration as per HA docs:


Second, make sure you have the “snips” integration included in your configuration.yaml:
snips:
Third, create your sentences in Rhasspy and train Rhasspy:
[GetWeather]
what’s the weather forecast
what is the weather forecast
weather forecast please
tell me the weather forecast
tell me today’s weather
what will the weather be like today
what will the weather be today
Fourth, add this intent to either your configuration.yaml:
intent:
  GetWeather:  # Intent type
    action:
    - service: mqtt.publish
      data_template:
        topic: hermes/tts/say
        payload_template: '{"text": "{{(states.weather.khaf_daynight.attributes.forecast[0].detailed_description)}}", "siteId": "default", "lang": null, "id": "", "sessionId": "", "volume": 1.0}'

Restart HA, and then ask Rhasspy, “what’s the weather” for the day’s forecast!

6 Likes

Not sure if you’re still around, but wanted to thank you for this.

Question, how does one get the payload_template from a service like NWS? I’m attempting to do something similar with openweathermap, but can’t figure out the template.

Hi underwater,
Give this a try:

payload_template: '{"text": "{{state_attr('weather.openweathermap', 'forecast')[1].condition }}", "siteId": "default", "lang": null, "id": "", "sessionId": "", "volume": 1.0}'

Not 100% warrantied, didn’t test it :slight_smile:

You can also “move ahead” in the forecast by changing the [1] which is today to [2] which is tomorrow, [3] the day after tomorrow (or overmorrow :slight_smile: )

You can see the other values by going to “Developer Tools”, then to “States” and using the dropdown arrow to select “weather.openweathermap”

Hope this helps!

1 Like

I’ll try tomorrow, thanks!

Can you help me understand how/why you formatted that? I feel like I’m stabbing in the dark with these configurations.

I didn’t see this part last night. Thank you so much, this will be really helpful!

I’ve tried and it errors out. Here’s the intents.yaml:

type or paste code her#
# intents.yaml - the actions to be performed by Rhasspy voice commands
#

GetTime:
  speech:
    text: The current time is {{ now().strftime("%H %M") }}

ChangeLightState:
    action:
      - service: mqtt.publish
        data:
          payload_template: '{"sessionId": "{{_intent.sessionId}}", "text" : "oh kay, the {{name}} is {{state}}"}'
          topic: hermes/dialogueManager/endSession
      - service_template: light.turn_{{state}}
        data:
          entity_id: light.{{name.replace('light','')}}

GetLightState:
  speech:
    text: Turning {{ name }} {{state }}
  action:
    - service: light.turn_{{ state }}
      target:
        entity_id: light.{{ name | replace(" ","_") }}

GetWeather:  # Intent type
  action:
    - service: mqtt.publish
      data_template:
        topic: hermes/tts/say
        payload_template: '{"text": "{{state_attr('weather.openweathermap', 'forecast')[1].condition }}", "siteId": "default", "lang": null, "id": "", "sessionId": "", "volume": 1.0}'
        topic: hermes/dialogueManager/endSessione

Here’s the error:

type or paste code he2023-08-25 11:37:29.039 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while parsing a block mapping
  in "/home/homeassistant/.homeassistant/intents.yaml", line 31, column 9
expected <block end>, but found '<scalar>'
  in "/home/homeassistant/.homeassistant/intents.yaml", line 32, column 52
2023-08-25 11:37:29.040 ERROR (MainThread) [homeassistant.bootstrap] Failed to parse configuration.yaml: while parsing a block mapping
  in "/home/homeassistant/.homeassistant/intents.yaml", line 31, column 9
expected <block end>, but found '<scalar>'
  in "/home/homeassistant/.homeassistant/intents.yaml", line 32, column 52. Activating safe modere

Okay, first you have “topic” twice in your intent, and you have a misspelling in one of the topics “endSessione”. But, you’re actually headed in the right direction!!! When I made the original post, I was really just starting out, and although it works, it really isn’t best practice (as was gently pointed out by folks who are many, many times more knowledgeable than me). If you’re just wanting to do a text to speech based on a condition (time of day, sensor status change, etc.) it is okay to the the topic “hermes/tts/say”. However, if you’re going to interact with HA via Rhasspy, it is best to use the topic: “hermes/dialogueManager/endSession”. However, if you use the “endSession” you need to give Rhasspy some more info to do the TTS. Here’s a GetTime automation doing this correctly:

  alias: Rhasspy-GetTime
  description: ''
  trigger:
  - platform: mqtt
    topic: hermes/intent/GetTime
  condition: []
  action:
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: hermes/dialogueManager/endSession
      payload_template: '{"sessionId": "{{ trigger.payload_json.sessionId }}", "text":
        "It is ({{states("sensor.simple_time")}})"}'
  mode: single

You’ll see there’s additional info in the payload template beyond just the text. Give me a little bit, I’ll see if I can test out the openweather for you (don’t use it myself, so I’ll have to set it up in my HA).

1 Like

Also, I’ll try to get you some links about YAML & JSON, which you need to get a familiarity with in order to understand how

{{state_attr('weather.openweathermap', 'forecast')[1].condition }}

turns into text.

Okay, botched the quotes, bitten me more than once, sorry. This works:

- id: '1691331852755'
  alias: Rhasspy-OpenWeather
  description: ''
  trigger:
  - platform: mqtt
    topic: hermes/intent/Weather
  condition: []
  action:
  - service: mqtt.publish
    data:
      qos: 0
      retain: false
      topic: hermes/dialogueManager/endSession
      payload_template: '{"sessionId": "{{ trigger.payload_json.sessionId }}", "text":"The forecast is ({{state_attr("weather.openweathermap", "forecast")[1].condition }})"}'
  mode: single

and used the correct method of interacting with Rhasspy.

1 Like

Okay, YAML & JSON at two data exchange formats that are used by Rhasspy & HA. Rhasspy is JSON, and HA is a little of both but mostly YAML.

When you’re trying to either do TTS or have an action taken by HA as a result of Rhasspy sending info over, you need to be able to “find” where that info is, either in the YAML, or the JSON. I use these two sites to figure out where the info I want/need is located.

Finally, you have to format the location info so that either Rhasspy or HA can act on it. That’s what is taking place in the payload template section of the automation. I messed up when I originally sent you the automation, there’s a formatting requirement for quote marks which requires using single quotes and double quotes.

And, after looking that up for you, I see that I really should be double quotes on the outside, and single quotes on the inside of my payload template. :worried:
So, do as the HA developers say, not as I do :slight_smile: Seriously, it is always best to follow the rules, because not doing so may cause you unforeseen problems, either now or in the future.
Hope all this helps, and have fun.

1 Like

Thank you so very much! Appreciate the hand holding and the pointer to the correct direction so I can learn on my own!

Glad to be of help! One request, pass the good will along in the future if you have the chance, and feel comfortable doing so. I really like Rhasspy (I can’t wait for 3.0 !) and have a good user community helps.

Always, and thanks again!

Using your code gave me some yaml errors I was able to correct with proper indents and time (probably web software formatting + yaml crazy pickiness).

I keep getting the following error in HA, though:

type o    raise UnknownIntent(f"Unknown intent {intent_type}")
homeassistant.helpers.intent.UnknownIntent: Unknown intent GetWeatherr paste code here

I attempted to add the GetWeather header, changed the Rhasspy-OpenWeather to Rhasspy-GetWeather, and even just GetWeather with no change in HA’s error.

Were you able to get rhasspy to read the forecast back to you?

Hi underwater,
take a close look at the error message:

Unknown intent GetWeatherr

do you have a typo in Rhasspy?

Here’s my sentences from Rhasspy:

[Weather]
whats the weather
what is the weather
whats the weather forecast
what is the weather forecast
whats the weather today
what is the weather today
do I need a coat
do I need an umbrella

So the bracketed “[Weather]” is what gets sent via MQTT to HA from Rhasspy, they have to match.
The “alias” in the automation is really just for us humans to keep track of things, since the “id” isn’t really user friendly, and neither would have any effect on the interaction.

I did test this before I provided it to you, (as I said, I had to install openweather since I use NWS (which by the way, has a much better text description of the weather than openweather)) so I do know it works!

Pretty sure changing the spelling in Rhasspy will fix this for you, let me know if it doesn’t.

1 Like

I have similar sentences, but I had a bracketed [GetWeather]. My assumption was that what’s in the bracket is the “identifier”, for the intent. So HA would need a block with the bracketed ID.

Changed it to [Weather], copied your code into my intents.yaml, loaded it in yamllint (checks yaml, i’m sure you know). Get the following:

type or paste code here 14:81     error    line too long (110 > 80 characters)  (line-length)
 28:1      error    syntax error: expected <block end>, but found '-' (syntax)
 36:3      error    wrong indentation: expected 4 but found 2  (indentation)
 41:81     error    line too long (174 > 80 characters)  (line-length)

That first error is from the - id: '1691331852755' line. The second is from the - service: mqtt.publishrmatted text line.

I attempted to load HA anyway despite the errors, but it started in safe mode with similar errors to yamllint.

You used GetWeather in your first post.

I think I moved from NWS because somehow it failed on me. I’ll try again.

Okay, let’s take an inventory. First, the code I gave you for HA is an Automation, not an Intent, sorry if I’ve caused some confusion when I cleaned things up for the correct way of interacting with Rhasspy using the end session.
You should be able to copy/paste the code block I provided (in the “botched the quotes” post) into your Automations.yaml (don’t trying using the automation editor gui) that’s a straight copy from mine.
Let me know if this helps.

1 Like

Wow! Thanks! I should have known as I had created an automation for lights when I was using events instead of intents. My brain was so focused on using the intents.yaml file, I didn’t even consider that I had seen this format before.

Anyway, I’m getting weather info back to rhasspy! Thanks! I’ll play around as I’d like something different than what I was getting.

I’m so glad its working for you!!! As I said, the OpenWeather text description leaves a bit to be desired "e.g. partly cloudy :slight_smile: ". If you installed the NWS integration, go to the “Developer Tools” “States” and use the drop down arrow to select your NWS weather entity. You’ll see in the forecast, detailed description yaml entry a good text description of your weather. I’ll leave it up to you, if you’d like to see if you can modify the automation I provided to work with the NWS by yourself (you can use the payload template from my original post) that might be fun, and I’ll help if you get stuck, or I can post mine.
Let me know.