Help adding sensor data to an alexa script with random text

hi all,

I’ve reciently found a video on youtube from 3ative showing how to get alexa to respond with random phrases put into a script. he also showed how to add a fixed message with sensor data.

what I am trying to do is mix those two… I want to add sensor data to some random phrases.

mabe the script will help understand…

this works in giving me random phrases made from the two blocks…

alias: Testing speach script
sequence:
  - service: notify.alexa_media
    data_template:
      target:
        - "{{ states.sensor.last_alexa.state }}"
      data:
        type: tts
      message: '{{
        [
        "The temerature in this room is.. ",
        "This room is currently.. ", 
        "If I was to guess, I would say the room was.. ",
        "Let me check.. Hmm.. I would say.. it is.. ",
        "Let me check with Roxy.. Hmm.. shes not talking to me.. I dunno, maybe.. ",
        ]|random +
        
        [
        "Celsius.. WOW.. Ive known polar bears to freeze in temperatures higher than that. ",
        "Celsius.. But that is only a guess",
        "Celsius.. I hope that answers your question. ",
        "Celsius.. Wow!.. that is almost as hot as the surface of the sun. ", 
        "Celsius.. Mmmm.. thats a nice cosy temperature, dont you think? ",
        ]|random}}'

But when I try to add the sensor it gives me errors…

alias: Testing speach script
sequence:
  - service: notify.alexa_media
    data_template:
      target:
        - "{{ states.sensor.last_alexa.state }}"
      data:
        type: tts
      message: '{{
        [
        "The temerature in this room is.. ",
        "This room is currently.. ", 
        "If I was to guess, I would say the room was.. ",
        "Let me check.. Hmm.. I would say.. it is.. ",
        "Let me check with Roxy.. Hmm.. shes not talking to me.. I dunno, maybe.. ",
        ]|random +

        {{states('sensor.bedroom_temperature')}}, +
        
        [
        "Celsius.. WOW.. Ive known polar bears to freeze in temperatures higher than that. ",
        "Celsius.. But that is only a guess",
        "Celsius.. I hope that answers your question. ",
        "Celsius.. Wow!.. that is almost as hot as the surface of the sun. ", 
        "Celsius.. Mmmm.. thats a nice cosy temperature, dont you think? ",
        ]|random}}'

as is probably clear I have no idea what I am doing, but I’m hoping someone can help me to understand and point me in the correct direction.

1 Like

Try this:

alias: 'Testing speech script'
sequence:
  - service: notify.alexa_media
    data_template:
      target:
        - "{{ states.sensor.last_alexa.state }}"
      data:
        type: tts
      message: >
        {% set rm1 = [ "The temerature in this room is.. ",
                       "This room is currently.. ", 
                       "If I was to guess, I would say the room was.. ",
                       "Let me check.. Hmm.. I would say.. it is.. ",
                       "Let me check with Roxy.. Hmm.. shes not talking to me.. I dunno, maybe.. " ]
                     | random %}
        {% set rm2 = [ "WOW.. Ive known polar bears to freeze in temperatures higher than that. ",
                       "But that is only a guess",
                       "I hope that answers your question. ",
                       "Wow!.. that is almost as hot as the surface of the sun. ", 
                       "Mmmm.. thats a nice cosy temperature, dont you think? " ]
                     | random %}

        {{ '{} {} Celsius.. {}'.format(rm1, states('sensor.bedroom_temperature'), rm2) }}

EDIT
Added missing > to message:

2 Likes

thank you for your reply.

I tried the above and got this error…

Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
  in "/config/./scripts/alexa_test.yaml", line 10, column 10

hope that helps

Whoops! My mistake. Just replace:

      message:

with:

      message: >

I’ve corrected the example above.

you are a genius my friend! thank you it works a charm…

next question, is it possible to select a temperature sensor based on the alexa device that is responding?

for example

I have alexa devices named

bedroom,
guest_room,
spare,
kitchen and
lounge

I have temperature sensors

bedroom_temperature
guest_room_temperature
spare_room_temperature
kitchen_temperature
lounge_temperature

if its not, or its to complicated then I can create an average temp sensor and use that

Probably. Your script is using this:

{{ states.sensor.last_alexa.state }}

If that returns the name of the Alexa device in the desired room (and it’s the same as the room’s name) then we have a way of getting the room’s temperature.

If all of the assumptions I made are correct, then simply replace the last line of the message template with these two lines:

        {% set s = 'sensor.' ~ states('sensor.last_alexa') | lower ~ '_temperature' %}
        {{ '{} {} Celsius.. {}'.format(rm1, states(s), rm2) }}

NOTE
I looked at what you wrote more closely and you can probably remove this part:

| lower

I put that there to force the value of sensor.last_alexa into lowercase. If it’s already lowercase then the lower filter is unnecessary.

1 Like

you are indeed a genius my friend :smile: this works a charm!

now I’ll spend some time trying to understand why it works so I can replicate it if needed.

I can’t thank you enough, honestly, thank you.

EDIT: for anyone looking to replicate it.

this is the sensor for the last alexa (not my code it’s from 3ative on youtube)

- platform: template
  sensors:
    last_alexa:
      entity_id:
        - media_player.kitchen
        - media_player.lounge
        - media_player.bedroom
        - media_player.guest_room
        - media_player.spare_room
      value_template: >
        {{ states.media_player | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}

i’m wrong… it’s not working… i now get a temp of “unknown”

everything else comes through ok, I’ve ensured that the alexa devices are the same name as the temperature sensors and vice versa.

I worked my way though them all with copy and paste to ensure they matched up.

any ideas?

If it worked initially and then stopped working it means one or more sensors may have unexpected states. For example, if after restarting Home Assistant, the value of one of the sensors, that this template relies on, becomes unknown. Ensure that the state value of sensor.last_alexa is not unknown and that whatever value it does have corresponds to the name of room’s temperature sensor.


Here’s how this template works:

 {% set s = 'sensor.' ~ states('sensor.last_alexa') ~ '_temperature' %}

It finds the state value of sensor.last_alexa then prepends the string sensor. and appends the string _temperature. The result is assigned to a variable simply called s. So if last_alexa is guest_room then the value of s will be sensor.guest_room_temperature

Here’s how the last line works:

{{ '{} {} Celsius.. {}'.format(rm1, states(s), rm2) }}

It creates a string containing the word Celsius.. along with three placeholders. Each placeholder is indicated by a {}

'{} {} Celsius.. {}'

It proceeds to use something native to all strings (in python) which is the format function. The function’s three arguments represents values that will be substituted for the placeholders.

  1. The first argument is a variable called rm1 containing the first random message.
  2. The second argument is the states() function. It will return the value of sensor.guest_room_temperature.
  3. The third argument is the rm2 variable containing the second random message.
1 Like

this is where I was wrong. I thought I had refreshed the scripts and when I ran the script in each room I got a temp spoken out, this varied by a few points of a degree so I thought all was good. I then had to restart HA due to an update, when I did the result was temp unknown and I noticed that each room has a different temp by a couple of degrees but the average temp sensor was changing by a few points.

I greatly appretiate your help in this and cannot thank you enough, I’ll keep at it and let you know when I work out what I have done wrong.

thank you

Just want to say, thank you so much for this. This is gold!

This is great example with explanation, I understood more of this one thread than reading couple of pages of manual.

However I have one question I can’t figure out. How to add more spaces between arguments, it doesn’t matter how many spaces I add between arguments, it always shows only one. I need to use underscores instead, but that is visually ugly.

To be concrete - I want to show temperature, humidity and light brightness in template card next to each other, but separated by two or three spaces, something like this but without underscores.
21,5 C __ 48,5% __ 90%
Thanks for help.