rogro82 / hass-variables

Home Assistant variables component
238 stars 58 forks source link

Native type templates seems to break attributes_template #50

Closed brahmafear closed 3 years ago

brahmafear commented 3 years ago

Since native types for templates was enabled in HA 0.118, most of my automations involving service calls for hass-variables are throwing this error:

voluptuous.error.MultipleInvalid: template value should be a string for dictionary value @ data['attributes_template']

Here's an example of one of the autos that is causing the issue:

      - service: variable.set_variable
        data_template:
          variable: rf_input
          value: >-
            # {% set payload = trigger.payload %}
            # {% set json = payload|from_json %}
            # {% set message = json.message %}
            # {{ message.id }}, {{ message.unit }}, {{ message.state }}
            {{ trigger.payload }}
          attributes_template: >
            {% set payload = trigger.payload %}
            {% set json = payload|from_json %}
            {% set message = json.message %}
            {
              "id": "{{message.id}}",
              "unit": "{{message.unit}}",
              "state": "{{message.state}}"
            }

This all worked fine pre-0.118. Any ideas how to make the yaml multiline string be interpreted as a string?

brahmafear commented 3 years ago

Workaround: Changing format of above to this seems to work:

      - service: variable.set_variable
        data:
          variable: rf_input
          value_template: >-
            # {% set payload = trigger.payload %}
            # {% set json = payload|from_json %}
            # {% set message = json.message %}
            # {{ message.id }}, {{ message.unit }}, {{ message.state }}
            {{ trigger.payload }}
          attributes: >
            {% set payload = trigger.payload %}
            {% set json = payload|from_json %}
            {% set message = json.message %}
            {
              "id": "{{message.id}}",
              "unit": "{{message.unit}}",
              "state": "{{message.state}}"
            }
Mariusthvdb commented 3 years ago

check this: https://github.com/rogro82/hass-variables/issues/47

it has been resolved, next to that, a fork of this repo has also been made, to update this seemingly abandoned repo.

brahmafear commented 3 years ago

Excellent. I scanned for existing issues before posting - no idea how I missed 47.

Interesting that the workaround of changing “attributes_template” to “attributes” works without the code changes Frenck lists in the other issue. Thanks.