Anyway to expose a sensor value to Alexa/Echo/Google

Don’t know why I’m persevering but hopefully third time lucky.

If you actually read the link you’d see you can do EXACTLY what you want to do, this is my specific post I was referring too…

it’s triggered by “Alexa, what is the lounge temperature?” just like you want.

Ok. Thanks. Guess I misunderstood. I’ll have to read through the posts to figure out how lounge temp is the device to trigger your script. Sounds very promising. I gues I could set up something similar for google time sensors which was kind of my goal.

Sounds like I should be able to say Alexa how long is my commute to work and have a TTS message from my echo provide the answer.

Thanks again. Very cool. Sorry I missed that. I have not spent as much time on that thread as I had my Econ’s set up with the other “testers needed” custom component.

No worries, I was hopeful we’d get there in the end :slight_smile:

Doing what you want is something that frustrated me back when I first started with HA so this was a Holy Grail tick box when I sussed it. Must admit I haven’t tried the media player version as the original did everything I wanted but assume you should be able to achieve the same results following the same logic if you want to stick with that.

@ptdalen and @Bobby_Nobble do you have confirmation that this “out of the box” approach will work for any sensor? In my case I am trying to get Alexa to read the value of my BMW i3 battery level. The sensor is exposed via the Home Assistant cloud along with the my other devices - see below:

However when initiating discovery in the Alexa app it doesn’t seem to find the new sensor resulting in Alexa being unaware of it entirely. It seems to me that Alexa can read the value of specific sensors that can be controlled and have a value (e.g. thermostat), but not other generic or read-only sensors. At this point it seems to me that the only way to achieve that is by following the approach outlined here:

If indeed there is an “out of the box” solution that I am missing I’d appreciate your help.

Currently, the Amazon Smart Home Skill API is quite limited.

[https://developer.amazon.com/de/docs/smarthome/understand-the-smart-home-skill-api.html#what-kinds-of-devices-are-supported](Amazon Reference)

At this moment it supports binary sensors (Amazon refers to them as sensors) and temperature sensors.

Of course, everything is possible with a custom skill (not using the Smart Home Skill API), but this is much more effort.

I do not understand, why Amazon is supporting Cooking Appliances, but not Covers (covers can be faked as dimmable lights though). Cooking Appliances would be much much further down the list for me :wink:

@hsepm thanks for the reply. Based on this I’ll go the custom skills route. I suppose the cooking thing may be prioritized by Amazon because its a way of appealing to and bringing in the broader non-enthusiast market?!

Maybe they want to sell some Alexa-enabled microwaves, but I don’t get the point. After all you have to physically interact with the device to put food in and out, whereas that would not be required when adjusting a cover or to check on the humidity in a room :wink:

The only use case for an Alexa-enabled microwave I can see is for visually impaired people.

If you read my solution you’ll see I’m just getting Alexa to read out the value of a sensor as it is seen in Home Assistant using TTS which is no different than getting her to read any other piece of text and will therefore work for absolutely anything Home Assistant can see, I’m not getting Alexa to see anything as a sensor or otherwise.

BTW - here is the custom skill intents

so I can ask

Alexa, tell me the status of the front door
Alexa, tell me everything that is on

1 Like

So this is what I am doing.
First I have the Alexa TTS script working. It’s pretty straight forward to set up, but still takes a bit of work. The cookie seems to be the hardest part for most.

Once you get this working, I then created a dummy light as recommended by @lonebaggie

His way of doing it used app daemon, but I’m not using app daemon. The dummy light is still used though

- platform: template
    lights:
      alexa_virtual:
        friendly_name: "Alexa Dummy Light"
        turn_on:
        turn_off:
        set_level:

I have several echo devices, and I want alexa to answer me on the echo that I asked the question on so I created a sensor and a switch using the alexa TTS script. So…

This is my sensor
  - platform: command_line
    name: Last Used Echo
    command: "/home/homeassistant/.homeassistant/alexa_remote_control.sh -lastalexa"
    entity_id: switch.last_echo_used

This is my switch
switch:
  - platform: command_line
    switches:
      last_echo_used:
        command_on: "/home/homeassistant/.homeassistant/alexa_remote_control.sh -lastalexa"
        command_off: ""

Last step is to go into the alexa app and create routines. If you look at my automation below you will see that I have routines set up for each dimmer level of the dummy bulb. So my routine basically says something like
"Alexa, where is Paul. " This sets my dummy bulb to 2%, which kicks of the appropiate response. This might be overkill for you, if you only have one sensor you want to expose, you could just create an input_boolean and turn that on/off via alexa and have that kick off the routine TTS.

- alias: "Alexa Report"
  trigger:
  - platform: state
    entity_id: light.alexa_virtual
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1
  action:
  - service: homeassistant.update_entity
    data:
      entity_id: sensor.last_used_echo
  - service_template: "notify.{{ states('sensor.last_used_echo') }}"
    data_template:
      message: >-
        {% if (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 1 and states('device_tracker.tracy_all') == 'not_home' %}
          Tracy is away and is approximately {{ states('sensor.tracys_time_to_home') }} minutes from home.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 1 and states('device_tracker.tracy_all') != 'home' %}
          Tracy is at {{ states('device_tracker.tracy_all') }} and is approximately {{ states('sensor.tracys_time_to_home') }} minutes from home.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 1 and states('device_tracker.tracy_all') == 'home' %}
          Tracy is at home.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 2 and states('device_tracker.paul_all') == 'not_home' %}
          Paul is away and is approximately {{ states('sensor.pauls_time_to_home') }} minutes from home.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 2 and states('device_tracker.paul_all') != 'home' %}
          Paul is at {{ states('device_tracker.paul_all') }} and is approximately {{ states('sensor.paul_time_to_home') }} minutes from home.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 2 and states('device_tracker.paul_all') == 'home' %}
          Paul is at home.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 3 %}
          Here is your local surf report.  The next tide is {{ states('sensor.noaa_tides') }}.  The waves are currently {{ states('sensor.virgina_beach_surf_report_forecast') }} feet.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 4 %}
          At the moment, your commute to the work 2 will take {{ states('sensor.time_to_work2') }} minutes.  Traffic is {{ states('sensor.pauls_traffic_density_to_base') }} at the moment.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 5 %}
          At the moment, your commute to wok 1will take {{ states('sensor.time_to_work1') }} minutes.  Traffic is {{ states('sensor.pauls_traffic_density_to_caci') }} at the moment.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 6 %}
          At the moment, your commute to work will take {{ states('sensor.time_to_work_tracy') }} minutes.  Traffic is {{ states('sensor.tracys_traffic_density_to_avis') }} at the moment.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 7 %}
          The Garage Door is currently {{ states('cover.garage_door_opener') }}.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 9 %}
          Based on current traffic, you have 15 minutes to leave if you want to get to work by {{ states.input_datetime.work_time_paul.attributes.hour }} {{ states.input_datetime.work_time_paul.attributes.minute }} A M .  Traffic is {{ states('sensor.pauls_traffic_density_to_work') }} today.
        {% elif (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round  == 10 %}
          {{ as_timestamp(now())|timestamp_custom ('%-I %M') }} and then {{ as_timestamp(now())|timestamp_custom ('%H %M') }} and one more {{ as_timestamp(now())|timestamp_custom ('%I %M') }}  and lastly {{ as_timestamp (now()) | timestamp_custom('%-I %-M %p') }}
        {% else %}
         {{ states('light.alexa_virtual.attributes.brightness') }}
        {% endif %}
5 Likes

I have written a Parser to expose sensor values to Alexa/ Google

1 Like

@Bobby_Nobble thank you for the reply. I think the challenge is that the solution(s) are buried and intertwined in long threads - so I missed the crux of how it works.

@ptdalen great summary - this helped me a lot, and your approach is not overkill for me as I will be exposing a bunch other stuff too - that script is super helpful.

I got the tts working finally - it looked like I was able to sort our the cookie issue with Chrome, but there was no audio when issuing the command to Alexa. Performing the same steps with FireFox worked (could ave something to do with it being a clean install?).

@RobDYI thanks for posting your intent script.

@lonebaggie thanks for the parser script. I’ll take a look at it soon, but I’m sure it will come in handy.

Its awesome to be part of such a capable and helpful community.

1 Like

You seem to really know what is going on and have achieved what i am trying to achieve.My hat is off to you sir! I have read thru so many different ways to do something that should be so simple that my head hurts, and have accomplished nothing but a headache, lol. My problem is i am not a coder by any since of the word and wind up getting lost, and all the different ways run together. Could you PLEASE PLEASE PLEASE put together a step by step of how you accomplished what seems unachievable to me. I would be willing to pay you for your time. :grinning::grin::wink:

I have a script that will run based on an emulated hue light where I expose the script like this:

   emulated_hue:
      type: alexa
      expose_by_default: true
      exposed_domains:
        - script
      entities:
        light.ha2:
          name: "ha2"

Alexa discovers the scripts as lights. When I toggle the light, the script plays the house temperature over the last_alexa. After seeing it, I understand how the emulated_hue is seen as a light.
In your example, you don’t use emulated_hue but the alexa_virtual template
which appears to have the same brightness feature as an emulated_hue bulb. When I run discovery on Alexa app, all that shows up are the scripts as light devices. It does not see the alexa_virtual light.

That said, I can get a script to run by setting up an Alexa routine in the app which turns on the script directly. In your example you show a sensor and a switch (in configuration.yaml?) and an automation. I don’t understand how the automation gets run if the alexa app doesn’t seem to see the virtual light which is set up like yours after discover devices.

So at the moment, I have an automation called ‘virtual’ that has your automation in it with some changes but I don’t know how to get alexa to see it and set a brightness level.

BTW: if I tell Alexa to set the script to 50% bright, the script ‘light’ goes to 10% in the Alexa app. Does this behavior seem odd?

Sorry this is so long and hits on several questions, but I’m flapping about here for a while without much progress. Any hints to get me off the ground on this?
Thanks

I use dummy bulbs like you use the scripts .

I have to convert from 0 -100 (alexa) to 0-255 (HA) that why you maybe getting odd results , however I cannot emulate your results

I’m not using appdaemon, but the yaml automation has worked great for me.

if (states.light.alexa_virtual.attributes.brightness | int / 255 * 100 ) | round == 1…
This should help with the math.

My alexa routines set values of 1 to 100 (I actually only have 10 right now, so 1-10), and have not had any issues at all

@ptdalen Thanks. The math in @lonebaggie s solution and the one you point out is clear. I was seeing the odd numbers before I tried that automation/script. Yes that part all works fine now but it is limited to returning a TTS action. Thanks to both of you.

Ahh, I think I misunderstood. Are you setting a “script” to a percentage? Do you have a dummy bulb set up and exposed to alexa using emulated hue?

like this one

- platform: template
    lights:
      alexa_virtual:
        friendly_name: "Alexa Dummy Light"
        turn_on:
        turn_off:
        set_level:

Then expose the light.alexa_virtual with emulated hue, then discover it with alexa, then set the levels on the light.alexa_virtual, and have the levels of that light trigger scripts, automations, etc using alexa routines.

While you can expose the scripts, I dont think you can set a level on them. I have a couple scripts that I use, and just turn them on.

I do have the virtual light set up and working fine. So I’m all set there. I do just turn scripts on and off.
Thanks

I’ve found that using an input_number helper and an automation to synchronize a sensor value to that input_number is a functional method. You can expose the input_number entity to Alexa and ask for the value.

Alexa, what is the value of <sensor name>?

2 Likes