Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] 'request:' to be able to parse variables #3

Closed
Netesfiu opened this issue Apr 15, 2023 · 4 comments
Closed

[Request] 'request:' to be able to parse variables #3

Netesfiu opened this issue Apr 15, 2023 · 4 comments

Comments

@Netesfiu
Copy link

Netesfiu commented Apr 15, 2023

I am trying to make a google home compatible device work with HA using the google assistant sdk and a generic thermostat.

I tried to debug how it would work but the event never gets fired:
the entities are 2 text inputs (one for the request and one for the response)

the script

alias: google text send
sequence:
  - service: google_assistant_sdk.send_text_command
    data:
      command: "{{states('input_text.google_input_debug')}}"
mode: single

the automation that should catch the response:

alias: google test
description: ""
trigger:
  - platform: event
    event_type: google_assistant_sdk_custom_event
    id: response
    event_data:
      request: "{{states('input_text.google_input_debug')}}"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: response
        sequence:
          - service: input_text.set_value
            data:
              value: "{{ trigger.event.data.response }}"
            target:
              entity_id: input_text.google_response
mode: queued
max: 10

I can see my responses in the https://myactivity.google.com/myactivity yet the automation never gets triggered and starting it manually does not result any response

[edit] I also tried handling the request and response commands in a single automation using a wait_for event flow, but that does nots seem to fix the problem either.

[edit] fater further testing it seems that the 'request:' object does not parse any variables. Is there a way to change that? It would make it much more flexible when it comes to other devices.

@Netesfiu Netesfiu changed the title automation is never gets triggered by the event [Request] 'request:' to be able to parse templating Apr 15, 2023
@Netesfiu Netesfiu changed the title [Request] 'request:' to be able to parse templating [Request] 'request:' to be able to parse variables Apr 15, 2023
@tronikos
Copy link
Owner

Can you try again after restarting home assistant?

@Netesfiu
Copy link
Author

I've restarted it multiple times. It works if i write out the exact string like this

trigger:
  - platform: event
    event_type: google_assistant_sdk_custom_event
    id: response
    event_data:
      request: what temperature is the ac at

what i wanted is to have it take a {{states('input_text.name')}} as request object so i dont need to hard-code every possible request

@tronikos
Copy link
Owner

I don't think you can have templates in triggers. You can just remove the event_data from your trigger. And you can have if then actions where in the if you can have templates based on trigger.event.data.request

@Netesfiu
Copy link
Author

Netesfiu commented Apr 24, 2023

Thank you for your support! i've managed to make it work.
posting the working code if anyone else is struggling to make google home only compatible devices work with HA

UPDATE 11.24.2023: If anyone is having problem with the event not triggering the automation:

I don't know if it's a bug or a rewrite, but after reinstalling the integraton, the automations stopped working. Turns out that the event_type that would trigger the automation name changed from google_assistant_sdk_custom_event to google_assistant_sdk_custom_custom_event. If anyone experiencing the automation not getting triggered by the google_assistant_sdk_custom_event try adding google_assistant_sdk_custom_custom_event as a 3rd trigger condition.

here's the updated code:

alias: AC sync
description: >-
  This automation is resposible for syncing any non HA change made on a Hisense
  smart air conditioning unit, (namely on/off state and target temperature) the
  response gets regexed to its core components then gets synced with a dummy
  generic thermostat.
trigger:
  - platform: event
    event_type: automation reloaded
  - platform: event
    event_type: google_assistant_sdk_custom_custom_event
    event_data: {}
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{trigger.event.data.request == 'is the ac on'}}"
            alias: Ac state check
        sequence:
          - alias: regex response and set it as ''state'' variable
            variables:
              state: "{{trigger.event.data.response|regex_findall_index('on|off')}}"
          - alias: choose the on/off service
            service_template: >-
              {% if state == 'on' %} climate.turn_on {% elif state == 'off' %}
              climate.turn_off {% else %} stop {% endif %}
            data: {}
            target:
              entity_id: climate.baja_klima
      - conditions:
          - alias: Ac temperature check
            condition: template
            value_template: >-
              {{trigger.event.data.request == 'what temperature is the ac set
              to' and trigger.event.data.response != 'Sorry, I couldn\'t reach
              ConnectLife.' and states('climate.baja_klima') != 'off'}}
          - condition: template
            value_template: >-
              {{state_attr('climate.baja_klima','temperature')|round(0) !=
              trigger.event.data.response|regex_findall_index('[0-9]{2}')|round(0)}}
        sequence:
          - service: climate.set_temperature
            data:
              temperature: "{{trigger.event.data.response|regex_findall_index('[0-9]{2}')}}"
            target:
              entity_id: climate.baja_klima
    default:
      - stop: unhandled exception
mode: single

The second part of the automation:

alias: Baja klíma control
description: >-
  This automation makes changes on the Hisense air conditioning unit based on
  the dummy Generic thermostat. Also it tries to update thegeneric thermostat
  every 5 minutes if there was a change that is not made by HA (I.E using the
  remote or the default smart home app)
trigger:
  - platform: state
    entity_id:
      - climate.baja_klima
    attribute: temperature
    id: temp
  - platform: state
    entity_id:
      - climate.baja_klima
    id: "off"
    to: "off"
  - platform: state
    entity_id:
      - climate.baja_klima
    from: "off"
    id: "on"
  - platform: time_pattern
    minutes: /5
    id: time
  - platform: event
    event_type: automation_reloaded
    id: time
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: temp
        sequence:
          - service: google_assistant_sdk_custom.send_text_command
            data:
              command: >-
                Change klíma temperature to
                {{state_attr('climate.baja_klima','temperature')}} C
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - service: google_assistant_sdk_custom.send_text_command
            data:
              command: turn on Klíma
      - conditions:
          - condition: trigger
            id: "off"
        sequence:
          - service: google_assistant_sdk_custom.send_text_command
            data:
              command: turn off Klíma
      - conditions:
          - condition: trigger
            id: time
        sequence:
          - service: google_assistant_sdk_custom.send_text_command
            data:
              command: is the ac on
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - automation.baja_klima_sync
                to: "0"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
                attribute: current
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
          - service: google_assistant_sdk_custom.send_text_command
            data:
              command: what temperature is the ac set to
          - wait_for_trigger:
              - platform: state
                entity_id:
                  - automation.baja_klima_sync
                to: "0"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
                attribute: current
            timeout:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
mode: single

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants