Use variables in Google Assistant input command for Chromecasting via HA

So I have tried to see if anyone else has tried to implement something like this in the forums but I haven’t managed to find anything. I have four cameras in my house and another four TVs that have the ability to chromecast. I already have the cameras and chromecasts in HA and have implemented a script to cast cameras to a TV based on drop-downs as referenced here.

However, I use Google Assistant for a lot of automation and really want to be able to say something like:

“Hey Google, chromecast Camera1 to TV2”

where Camera1 and TV2 are variables. I could obviously do this by creating a script for every permutation of camera and TV but this isn’t ideal! It would also mean more scripts if future cameras or TVs are added into the mix.

So the question is, does anyone know if I am able to have a Google command that expects two variables which then casts a camera to a particular TV?

Thanks for reading!

Craig

I can’t offer any help. But I am interested in seeing what the outcome / solution is!
Would love to know how to get better HA - Google interactions.

ifttt for google assistant with " Say a phrase with a text ingredient" ?

repeat the call for each camera, and pass the tv as the variable?

1 Like

The ifttt suggestion would work and also an dialogflow solution which is probably the most difficult/advanced approach. Dialogflow is intended to for chatbot like solutions.

So I want to say thanks everyone for your help. After looking into IFTTT I have realised that this will do exactly what I need.

As walaj suggested, I can setup one IFTTT for each camera and pass the chromecast as the variable. Unfortunately, I am having trouble getting the variable. Hopefully someone can help with where I am going wrong!

To trigger I say: show me the driveway camera on the $ TV

where $ is the variable like ‘kitchen’. (Something weird that happened is for some reason $ is being stored as ‘the kitchen’ despite the word ‘the’ already being outside of the variable. When it confirms this back to me it says ‘OK, casting now to the the kitchen TV’. Note the second ‘the’. Not a big deal as I can have HA look for the variable equally ‘the kitchen’. Just a weird one).

My IFTTT body looks like this: { “action”: “call_service”, “ifttt”: {{TextField}} }

Then I have a HA automation which looks like this:

- alias: chromecast_camera_driveway
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
    - service: notify.craig_phone
      data:
        message: "Chromecast = {{ ifttt }}"

I was originally trying to get the camera to cast in the code but then in trying to debug I changed it to notify me of the variable. All I ever get is in the notification is 'Chromecast = ’ and the variable is blank. I have tried using variations of:

    message: "Chromecast = {{ifttt}}"
    message: "Chromecast = ifttt"

I feel like I am missing something really obvious! Whatever I do I can’t get the variable in HA but IFTTT has ‘the kitchen’.

Any help is appreciated! Many thanks.

Quick response - looking at my webhocks (I’m just using straight API web calls rather than ifttt_webhook_received events), but try changing the IFTTT body to

{ “action”: “call_service”, “ifttt”: "{{TextField}}" }

(put the textfield in " )

I had tried that but I have just tried it again but it makes no change for me. I still get the 'chromecast = ’ with a blank variable. Can you confirm if I am calling it in HA in the correct way? i.e. message: “Chromecast = {{ifttt}}”

Cheers.

I’ve just looked at https://www.home-assistant.io/components/ifttt/

try changing your message to:

message: '{{ trigger.event.data.textfield }}'

(guessing here)

Ha, you are right! I don’t believe it. I tried loads of variations including using ‘textfield’ but I must have had quotes around it or something. I can now get the message to consistently work to my phone so HA is getting the variable. Thanks for your help! Even though this works I still can’t get the chromecast template to work. This is what I currently have:

- service: notify.craig_phone
  data_template:
    message: '{{ trigger.event.data.textfield }}'
- service: media_player.play_media
  data_template:
    entity_id: >
      {% if ( '{{trigger.event.data.textfield }}' == "the bedroom") %}
            media_player.bedroom_tv
      {% elif ( '{{trigger.event.data.textfield }}' == "the kitchen") %}
            media_player.kitchen_tv
      {% endif %}
    media_content_id: https://URL:PORT/mjpg/cam-driveway/?user=USER&pw=PASSWORD
    media_content_type: 'image/jpg'

When I trigger the automation my pushover notification shows the variable as ‘the kitchen’ so that must mean that ‘{{ trigger.event.data.textfield }}’ works but this doesn’t seem to work when inserted into the casting script. Here is what shows in the log:

2019-01-08 08:13:36 INFO (MainThread) [homeassistant.components.http.view] Serving / to 192.168.0.11 (auth: False)
2019-01-08 08:13:38 INFO (MainThread) [homeassistant.components.http.view] Serving /api/webhook/9472eb3ceafbf301b0e061307ffff5f48848971f2cfdc5ccfc42f51a66b71f4a to 127.0.0.1 (auth: False)
2019-01-08 08:13:38 INFO (MainThread) [homeassistant.components.automation] Executing chromecast_camera_driveway
2019-01-08 08:13:38 INFO (MainThread) [homeassistant.helpers.script] Script chromecast_camera_driveway: Running script
2019-01-08 08:13:38 INFO (MainThread) [homeassistant.helpers.script] Script chromecast_camera_driveway: Executing step call service
2019-01-08 08:13:39 INFO (MainThread) [homeassistant.helpers.script] Script chromecast_camera_driveway: Executing step call service
2019-01-08 08:13:39 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “C:\Python364\lib\site-packages\homeassistant\components\automation_init_.py”, line 294, in async_trigger
await self.async_action(self.entity_id, variables, context)
File "C:\Python364\lib\site-packages\homeassistant\components\automation_init
.py", line 378, in action
await script_obj.async_run(variables, context)
File “C:\Python364\lib\site-packages\homeassistant\helpers\script.py”, line 130, in async_run
await self._handle_action(action, variables, context)
File “C:\Python364\lib\site-packages\homeassistant\helpers\script.py”, line 172, in _handle_action
action, variables, context)
File “C:\Python364\lib\site-packages\homeassistant\helpers\script.py”, line 261, in _async_call_service
context=context
File “C:\Python364\lib\site-packages\homeassistant\helpers\service.py”, line 81, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context)
File “C:\Python364\lib\site-packages\homeassistant\core.py”, line 1101, in async_call
processed_data = handler.schema(service_data)
File “C:\Python364\lib\site-packages\voluptuous\schema_builder.py”, line 267, in call
return self._compiled([], data)
File “C:\Python364\lib\site-packages\voluptuous\schema_builder.py”, line 589, in validate_dict
return base_validate(path, iteritems(data), out)
File “C:\Python364\lib\site-packages\voluptuous\schema_builder.py”, line 427, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: Entity ID is an invalid entity id for dictionary value @ data[‘entity_id’]

As you can see it says invalid entity id for dictionary value so it is as though this bit isn’t matching:

{% elif ( ‘{{trigger.event.data.textfield }}’ == “the kitchen”) %}

Any ideas? Any help is appreciated! Thanks.

Craig

try changing the "the bedroom" to 'the bedroom' - I’m not convinced the " and ’ are the same

next step is to change ( '{{trigger.event.data.textfield }}' to trigger.event.data.textfield

Can’t believe I missed that URL, I doubled checked the log excerpt but completely missed the obviously URL in my code. It has been redacted now and password changed. Thanks for the heads up.

Also, you are 2 for 2 on the syntax front. Again, I’m not sure how I didn’t try this variation but you are bang on with this: trigger.event.data.textfield. I really thought it would need the {} to be considered a variable but I’m happy it is working! When I had it working I tried both the single and double quotes and both worked fine. Thanks for your help.

The last issue I have is being able to call different automatons depending on the IFTTT triggered. I was planning on having one IFTTT applet for each camera. So far I have one applet which works now but that triggers one automation which has a single camera URL within it. I need a way to trigger different automations. I think there should be some text I can add to the body to pass another variable that is hardcoded into the IFTTT Body. Something like this:

{ “action”: “call_service”, “camera”: “driveway”, “textfield”: “{{TextField}}” }

But I am not sure how to use this ‘camera’ in home assistant. It looks like this guy here has tried to do something similar using a ‘webhook_id’ but his example is using an event of webhook rather than an event of ifttt_webhook_received,

Any other ideas to crack this?

Many thanks.

ok - looking at https://www.home-assistant.io/components/ifttt/

suggests you can use trigger.event.data.camera to pull the camera value out

so try this:

- service: media_player.play_media
  data_template:
    entity_id: >
      {% if trigger.event.data.textfield == "the bedroom") %}
            media_player.bedroom_tv
      {% elif trigger.event.data.textfield == "the kitchen") %}
            media_player.kitchen_tv
      {% endif %}
    media_content_id: >
      {% if  trigger.event.data.camera == 'cam1' %}
        https://URL:PORT/mjpg/cam-cam1/?user=USER&pw=PASSWORD
      {% elif trigger.event.data.camera == 'cam2' %}
        https://URL:PORT/mjpg/cam-cam2/?user=USER&pw=PASSWORD
      {% endif %}
    media_content_type: 'image/jpg'

You have nailed it :slight_smile:
I didn’t think about having one automation for all cameras and passing the camera variable. I was thinking I would need one automation per camera. Thanks so much for your help dude, really appreciate it.

For anyone else that tries to implement this, here is my finalised IFTTT applet which I duplicated for each one of my cameras only changing the camera name in the trigger and the also in the Body:

Here is my automation which takes the camera variable for the media_content_id and the spoken variable (textfield) for the entity_id:

- alias: chromecast_camera
  trigger:
    platform: event
    event_type: ifttt_webhook_received
    event_data:
      action: call_service
  action:
    # Used for testing to see what the textfield variable is on my phone when the automation is triggered
    # - service: notify.craig_phone
      # data_template:
        # message: '{{ trigger.event.data.textfield }}'
    - service: media_player.play_media
      data_template:
        entity_id: >
          {% if (trigger.event.data.textfield == 'the bedroom') %}
            media_player.bedroom_tv
          {% elif (trigger.event.data.textfield == 'the family room') %}
            media_player.family_room_tv
          {% elif (trigger.event.data.textfield == 'the kitchen') %}
            media_player.kitchen_tv
          {% elif (trigger.event.data.textfield == 'the living room') %}
            media_player.living_room_tv
          {% endif %}
        media_content_id: >
          {% if  trigger.event.data.camera == 'cam-driveway' %}
            https://URL:PORT/mjpg/cam-driveway/?user=USER&pw=PW
          {% elif trigger.event.data.camera == 'cam-garage' %}
            https://URL:PORT/mjpg/cam-garage/?user=USER&pw=PW
          {% elif trigger.event.data.camera == 'cam-pippa' %}
            https://URL:PORT/mjpg/cam-pippa/?user=USER&pw=PW
          {% elif trigger.event.data.camera == 'cam-sadie' %}
            https://URL:PORT/mjpg/cam-sadie/?user=USER&pw=PW
          {% endif %}
        media_content_type: 'image/jpg'

I can now successfully say 'Hey Google, show me the driveway camera on the Living room TV and it will cast :slight_smile:

Good luck to anyone else implementing

1 Like

not bad considering i was guessing :smiley:

Pleased its working for you! Now to investigate if I can get my dafang camera to do something similar.

A year later… Is still ifttt the only solution for arguments? I use it too, but want an alternative, since ifttt is limited in Google assistant languages

Thank you very much @walaj and @dwarfer16 - for detailing this in such a great way that we can all benefit.

If you were to do this today, would you still use the same methods?

Hi MattP

I have to say that this solution is still going strong for me. I still use this regularly to view cameras on different TVs. I guess idelly I would prefer a not using IFTTT just to limit the number of systems that this is dependent on but I never found a solution. This still does the job :slight_smile:

1 Like

I’m trying to do something similar. I want a command to change the channel on my TV.
With google home and hassio I already turn on thelevision and change channel but with an “hardcoded” channel in the script

Now i want a vocal command like “Hey google, change the channel on 5”…

With ifft i already write a webhooks.
The trigger is working…
if in automation i write something like this

- id: '1580408252907'
  alias: Cambia Canale
  description: Cambia il canale della televisione
  trigger:
  - event_data:
      action: call_service
    event_type: ifttt_webhook_received
    platform: event
  condition: []
  action:
  - data:
      message: "ciao"
    service: notify.televisione

i see the message “ciao” in the television every time i say “hey google, metti canale #” where # is a number

but if i write

- id: '1580408252907'
  alias: Cambia Canale
  description: Cambia il canale della televisione
  trigger:
  - event_data:
      action: call_service
    event_type: ifttt_webhook_received
    platform: event
  condition: []
  action:
  - data:
      message: '{{ trigger.event.data.canale }}'
    service: notify.televisione

i see nothing on the tv… so i can’t “read” the variable that i say to google. Why?

This is the webhook body

{ "action": "call_service", "service": "media_player.play_media", "canale": "{{NumberField}}" }

there is no suggestions for this problem?

There is nothing obviously wrong with the webhook body or code - I would suggest changing the message to include all the fields, and using a textual notifier (email for example) so that you can test what is going on.