Convert legacy template into new template

Hi,

I have the following automation that worked well with older versions of Home Assistant. With version 0.118.5 it stopped working:

- alias: 'FensterOffen'
  trigger:
    platform: state
    entity_id: binary_sensor.oeq1557949_state
    from: 'off'
    to: 'on'
  action:
    service: homematic.set_device_value
    data_template:
      address: NEQ1468231
      channel: 2
      param: SUBMIT
      value: "1,1,108000{% if is_state('binary_sensor.oeq1558420_state', 'on') %},1{% endif %} {% if is_state('binary_sensor.oeq1556931_state', 'on') %},5{% endif %}"

It works again, when I enable legacy templates like this:

homeassistant:
  legacy_templates: true

But this option is only available for a transitioning phase; hence, I have to rework the template. Unfortunately, I do not find any instructions on how to change this kind of template.

I get the following error message when I disable legacy templates again:

Logger: pyhomematic.devicetypes.generic
Source: /usr/local/lib/python3.8/site-packages/pyhomematic/devicetypes/generic.py:198
First occurred: 12:26:00 PM (1 occurrences)
Last logged: 12:26:00 PM

HMGeneric.setValue: SUBMIT on NEQ1468231:2 Exception: cannot marshal <class 'homeassistant.helpers.template.TupleWrapper'> objects

I have already created a github issue (HMGeneric.setValue: SUBMIT on NEQ1468231:2 Exception: cannot marshal <class 'homeassistant.helpers.template.TupleWrapper'> objects · Issue #51516 · home-assistant/core · GitHub), but that one was closed, saying it is not an issue.

But what do I need to change?

Thanks in advance.

You could use the choose function in the automation in place of the template. You mention the version it last worked, but what version are you running now.

I use version core-2021.5.5 now.

I don’t know the purpose of the legacy template flag, so I can’t help on that front. If you were to use the choose function, you could use the following.

  action:
    - choose:
        - conditions: "{{ is_state('binary_sensor.oeq1558420_state','on' }}"
          sequence:
            service: homematic.set_device_value
              data:
              address: NEQ1468231
              channel: 2
              param: SUBMIT
              value: 1,1,108000,1
        - conditions: "{{ is_state('binary_sensor.oeq1556931_state','on' }}"
          sequence:
            service: homematic.set_device_value
              data:
              address: NEQ1468231
              channel: 2
              param: SUBMIT
              value: 1,1,108000,5

Thanks for this proposal, but it does not help me.

My automation needs to create one string. The device is an audio player that I can tell which mp3 files to play after each other. Each file has a number starting with one. So I need to create a string like this to play the files 1, 2 and 3:

1,1,108000,1,2,3

The first three digits are information about the loudness etc. Unfortunately, I cannot send multiple instructions to the device directly after each other, because the device seems to have not buffer for incoming messages while it is playing an audio file. And as I don’t know how long it will take to play each file, I only have the option to create one long string with all the tracks to play.

The idea is to create an audio message when I open the front door that tells me which windows are open. As more than one windows can be open at once, I need a longer message. Like “Attention! Windows A is open. Window B is open” etc.

Traditionally, Home Assistant treated the value produced by a template as a string. That means even if the template produced a numeric value like 1.5 or a list such as ['kitchen', 'bedroom'] they would both be handled as strings.

That changed in version 0.118 when support for “native types” was added. Since then, Home Assistant infers the value’s type based on its appearance.

Most of the time this “inference” works well and values like 23.57 and light.front and ['switch.pump', 'switch.master'] are handled as float, string and list, respectively. However, there are cases where it guesses the value’s type incorrectly. Unfortunately, there is no way to suggest the desired value type. In your case, you want it to be handled as a string but, based on the error message, it appears to be handled as a tuple (and it doesn’t support that type or at least that’s my guess based on the error message).

The legacy_template option was included to permit the user to enable/disable “native types” globally. If you set it to true it means all templates are handled the traditional way where he value’s type is always a string.

There have been requests to provide the ability to force a value’s type (i.e. you would indicate you want the value’s type to be string). However, the requests have not been accepted by the development team.

1 Like

And how do I convert this expression into a string?

{% if is_state('binary_sensor.oeq1558420_state', 'on') %},1{% endif %}

There’s nothing you can do within the template to force Home Assistant to handle this 1,1,108000,1,2,3 as a string. That’s the unfortunate, and unavoidable, drawback of the native types feature.

And now? I need to send a comma separated string to the device.

And now you need to convince the development team the “native types” feature doesn’t handle this use-case correctly. Unless they do something about it I think you have no option other than using legacy_template: true while it’s still available.

I’m really glad you are on these boards. This comment is EXACTLY what I needed to know. You always seem to be there with a ready answer. Thanks!

1 Like