How to pass time to an automation?

I inserted this sensor in configuration.yaml:

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

I want to pass the time to an action as a payload. payload={time} and payload"{time}" don’t work. Can this be done in an automation or does a script have to be used. Thanks for any help.

I have an automation that is triggered by rhasspy; that part works. I want to use RESTful Command: rhasspy_speak to have rhasspy say the time. the command is this in the config yaml:
``
rest_command:
rhasspy_speak:
url: ‘http://192.168.0.114:12101/api/text-to-speech
method: ‘POST’
content_type: ‘text/plain’
payload: “{{ payload }}”

I installed the Time and Date sensors and they appear in the overview dashboard. I want to send a formatted time to the payload in the action so that rhasspy will say the time. I suspect I will need a script to reference and format the time so my first question is how do I call a script from an automation? I don't see that option in the dropdown. Thanks for help.`

Sorry, I missed the ``` after the code.

If your command is triggered by Rhasspy, you don’t need a rest_command. The automation can simply answer to your request from Rhasspy.

automation:
  - id: Rhasspy GetTime
    alias: Rhasspy GetTime
    mode: single
    trigger: 
      - platform: event
        event_data: {}
        event_type: rhasspy_GetTime
    action:
      - service: mqtt.publish
        data:
          topic: hermes/dialogueManager/endSession
          payload_template: '{"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "Es ist {{ now().hour }} Uhr {{ now().minute }}"}'

This is one way, as this just takes the time from now(). The other would be like this:

automation:
  - id: Rhasspy GetTime
    alias: Rhasspy GetTime
    mode: single
    trigger: 
      - platform: event
        event_data: {}
        event_type: rhasspy_GetTime
    action:
      - service: mqtt.publish
        data:
          topic: hermes/dialogueManager/endSession
          payload_template: '{"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "Es ist {{ states('sensor.time') }} Uhr"}'

The problem is, your sensor.time must be first processed to a “real” time, and that is easier if you just take now().

Why should you do this? Because Rhasspy opens a session with every command you send to HA. Thsi session should be ended by your automation, that’s why just send a text together with the “endSession” command. Otherwise you will get unfinished sessions, and these can “block” your Rhasspy or slow it down.

I don’t understand. Rhasspy doesn’t respond to mqtt messages. I need the rest_command to make rhasspy speak.

I made a script called Say Time that will use the rest command to pass a string to rhasspy to say. Now I need a way to put the time as a string into the payload of the command as the final action of that script.

If this were written in python or C# I would declare a string variable, use a function to set that variable as the current time, then format as needed and send it to print. I’m not yet familiar with yaml, but doesn’t it have similar statements?

The “normal” way to communicate for Rhasspy <-> HA is via intents or events, both of which are sent via MQTT. You configure this in Rhasspy under “Intent Handling”.

I personally use “events”, because I couldn’t get “intents” to work reliable. So in my case the way is as follows:

  • Sentence.ini like this
    [GetTime]
    What is the time
    
  • In HA the above posted automation (the first one)
  • Rhasspy recognizes the sentence, starts a new session and sends an event via MQTT to HA
  • HA reacts with the above automation and sends a text together with the endSession command
  • Rhasspy speaks the text

The rest_command you’re using is valid as well, but is normally used in another case, if you want to let Rhasspy say something without a command to Rhasspy. An example would be, if you have an alert in HA, where you want to let Rhasspy speak a warning, without getting “asked”. Let’s say your main door is open for longer than 10 minutes, than Rhasspy should tell you something. That is a use case for rest_command.

I tried your first example. From automation.yaml:

- id: '1654308484528'
  alias: Rhasspy Get Time
  description: ''
  trigger:
  - platform: event
    event_type: Rhasspy_GetTime
    event_data: {}
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: hermes/dialogueManager/endSession
      payload_template: '{"sessionId": "{{ trigger.event.data._intent.sessionId }}",
        "text": "Es ist {{ now().hour }} Uhr {{ now().minute }}"}'
  mode: single

But when I say What is the time (which is in sentences.ini) rhasspy doesn’t respond. What did I do wrong? I have config:default so input datetime should be there.

Do I also have to add some kind of helper? Thanks again for your help.

Trace shows that it is being executed, but no sound ensues.

That’s a good sign, meaning the first part (Rhasspy to HA) is working as it should. :slight_smile:

To find out, what is going on, I’d recommend using Rhasspy-Watch. There you can exactly see, what is going on with the command, that is send back. Should bring up some information, where it get’s lost.

Oh, and before I forget, you’re sure, your volume is high enough on your Rhasspy speaker? :wink: Ask how I come up with such an idea… :rofl:

I installed and ran Rhasspy-Watch, though I have to specify host as 127.0.0.1 because rhassy-master throws an error. When I run the automation, RW responds with this:

[2022-06-04 07:54:46] [Dialogue] was ask to end session with id default-jarvis_raspberry-pi-8595f979-9bb4-4c18-936e-2f1d4c4c73e3 by saying 'it is 7 hour 54'

I get the same message, but in JSON, if I subscribe to the topic in a terminal window. So the message is sent but Rhasspy isn’t saying it. The audio is a powered speaker plugged into the pi’s audio jack and works in other respects; it plays WAV files and responds to the rhasspy-speak command. The question was why it’s not responding to the dialogue manager’s message.

So I went to rhasspy and saw the settings panel for dialogue management. Ah ha!, I thought, this will fix it, and set it to Hermes. Well, no, but this is what I get from the different DM settings:

When DM is set to Rhasspy, I hear the wake sound, the action is triggered, rhasspy watch shows the mqtt message, and trace shows the action was executed without error. But no output from the speaker.

When DM is set to Hermes, no wake sound (expected), action is not triggered, no watch output, and no trace report.

When run action is used in HA, no matter how DM is set in Rhasspy, I get this:

Error: Error rendering data template: UndefinedError: 'dict object' has no attribute 'event'

Do I need to do something more to set up Hermes as the dialogue manager, in Rhasppy or HA? I can’t find anything else in the docs. Thanks again.

I noticed another setting in rhasspy at the top, internal or external mqtt broker. I set that at external using 127.0.0.1 and port 1883. I then got an error on reloading and lost the web ui altogether. Had to use a backup profile.json and sentences.ini to recover.

I’ve attached a shot of my settings from rhasspy. Which ones would need to be changed and how to set them, hopefully without breaking rhasspy again. Thanks.

I don’t think the dialogue manager approach is going to work for me, so I go back to my original question. Is there any way to put variables into a payload and pass it to rhasspy_speak? I tried this, but there was an error loading the automation.

- id: '1654524145065'
  alias: New Automation
  description: ''
  trigger:
  - platform: event
    event_type: rhasspy_SayTime
  condition: []
  action:
  - service: rest_command.rhasspy_speak
    data:
      payload_template:  "text": "it is {{ now().hour }} hour {{ now().minute }}"}'
  mode: single
1 Like

It turns out that rhasspy doesn’t like having username and password as blank entries when using the external broker. Setting these to non-blank values made the automation work. Thanks for your help; I will use the dialog manager in the future.