Random Greetings in Home Assistant

There's a server in my office and it has an audio card in it, so I figured why not have Home Assistant speak things to me. I started simple with just a basic greeting when the lights came on, and a goodbye when the lights went off. But then I decided to up the ante and have Home Assistant pick a greeting randomly from a list. Spice things up a bit.

This setup relies on a working MQTT broker to pass messages from a script to Home Assistant. If you don't have MQTT set up and working, go do that first. https://www.home-assistant.io/components/mqtt/

Next, go grab random-message.pl from my Github repo, https://github.com/heytensai/homeassistant/tree/master/random-message. I'm running Hassbian and put it in /home/homeassistant/bin. You may need to adjust that depending on your install.

Did I mention that this is a pretty manual process and not for the faint of heart?

Create a directory to store your random messages. The script chooses a single line out of a file and sends it to an MQTT topic. The name of the file can be anything. One message per line. I created /home/homeassistant/random-messages as the directory and made two files named "hello" and "goodbye".

Now comes the time to set up Home Assistant. Step 1 is to create an MQTT sensor to store the message text.

https://github.com/heytensai/homeassistant/blob/master/random-message/sensor.yaml

- platform: mqtt
  name: "Random Message Hello"
  state_topic: "message/hello"
- platform: mqtt
  name: "Random Message Goodbye"
  state_topic: "message/goodbye"

Now we need a notify service to update the message right before we play it. This could probably be simplified to a single notify.

https://github.com/heytensai/homeassistant/blob/master/random-message/notify.yaml

- name: random_message_hello
  platform: command_line
  command: "/home/homeassistant/bin/random-message.pl hello"

- name: random_message_goodbye
  platform: command_line
  command: "/home/homeassistant/bin/random-message.pl goodbye"

Then we create a script that calls the update and subsequently plays the audio. You'll need to substitute your actual notify command here, which could be anything.

https://github.com/heytensai/homeassistant/blob/master/random-message/scripts.yaml

office_hello:
  sequence:
    - service: notify.random_message_hello
      data_template:
        message: ""
    - service: notify.your_notify_service
      data_template:
        message: >-
          {{ states('sensor.random_message_hello') }}

office_goodbye:
  sequence:
    - service: notify.random_message_goodbye
      data_template:
        message: ""
    - service: notify.your_notify_service
      data_template:
        message: >-
          {{ states('sensor.random_message_goodbye') }}

And finally, something to trigger it. I have a binary sensor based on a light sensor that tells whether the office is occupied or not, and that determines when I play the message. Substitute your own criteria here.

https://github.com/heytensai/homeassistant/blob/master/random-message/automations.yaml

- alias: WHEN office occupied on DO say something crazy
  trigger:
    - platform: state
      entity_id: binary_sensor.office_occupied
      to: 'on'
  action:
    service: script.office_hello

- alias: WHEN office occupied off DO say something crazy
  trigger:
    - platform: state
      entity_id: binary_sensor.office_occupied
      to: 'off'
  action:
    service: script.office_goodbye

Subscribe to Comments for "Random Greetings in Home Assistant" Subscribe to zmonkey.org - All comments