Scripting question - selector options changed by HA

I have created a basic script to turn on Hue effects on my Hue lights. It has two inputs, a text box for the ieee and a drop down select for each effect.

The dropdown select includes label/value pairs. When I enter them like this:

- label: Candle
  value: '21000101'
- label: Fireplace
  value: '21000102'
- label: Prism
  value: '21000103'
- label: Sunrise
  value: '21000109'
- label: Sparkle
  value: '2100010a'
- label: Opal
  value: '2100010b'
- label: Glisten
  value: '2100010c'
- label: Sunset
  value: '2100010d'
- label: Underwater
  value: '2100010e'
- label: Cosmos
  value: '2100010f'
- label: Sunbeam
  value: '21000110'
- label: Enchant
  value: '21000111'

and then save the script, only certain options work. Reopening the script and the dropdown select reveals that HA has reformatted the entries to look like this:

- label: Candle
  value: "21000101"
- label: Fireplace
  value: "21000102"
- label: Prism
  value: "21000103"
- label: Sunrise
  value: "21000109"
- label: Sparkle
  value: 2100010a
- label: Opal
  value: 2100010b
- label: Glisten
  value: 2100010c
- label: Sunset
  value: 2100010d
- label: Underwater
  value: 2100010e
- label: Cosmos
  value: 2100010f
- label: Sunbeam
  value: "21000110"
- label: Enchant
  value: "21000111"

You can see that the values ending with a letter have had the quotes removed (these are the options that work) and the values ending in a number have had their quotes replaced by double quotes and these do not work.

Why is HA changing how I entered values?

I have tried removing the double quotes from those values but the script throws an error on attempting to save:

image

How do I get the values that don’t work to work?

Here is the yaml for the entire script:

sequence:
  - action: zha.issue_zigbee_cluster_command
    data:
      ieee: "{{ ieee }}"
      endpoint_id: 11
      cluster_id: 64515
      command: 0
      command_type: server
      params:
        data: "{{ hue_effect }}"
alias: Turn on Hue Effect
description: Turns on the chosen Hue Light with the selected effect.
icon: hue:festavia
fields:
  ieee:
    selector:
      text: {}
  hue_effect:
    selector:
      select:
        options:
          - label: Candle
            value: "21000101"
          - label: Fireplace
            value: "21000102"
          - label: Prism
            value: "21000103"
          - label: Sunrise
            value: "21000109"
          - label: Sparkle
            value: 2100010a
          - label: Opal
            value: 2100010b
          - label: Glisten
            value: 2100010c
          - label: Sunset
            value: 2100010d
          - label: Underwater
            value: 2100010e
          - label: Cosmos
            value: 2100010f
          - label: Sunbeam
            value: "21000110"
          - label: Enchant
            value: "21000111"
        mode: dropdown
        custom_value: false

Both yaml representations lead to exactly the same data. So what is causing the action to fail has little to do with how the string is specified.

If the numbers inside the strings are being rejected it is probably because templates always return strings, and HA tries to convert it to native types when it can. I think the action expects a string. The things looking like numbers will be converted to number, the ones ending in a letter are never numerical to begin with, so they remain a string.

The hard part is that you cannot prevent it, even by using a string filter. What you can do is model the entire data in one template, returning a dictionary. Inside the dictionary HA will keep the types intact.

So try this:

  data: |
    {{  {
      "ieee": ieee,
      "endpoint_id": 11,
      "cluster_id": 64515,
      "command": 0,
      "command_type": "server",
      "params": {
        "data": hue_effect 
        }
      } }}

Hello Orange-GT3,

I don’t know for certain, but if I count the characters there it’s ~390, and there are ~140 characters in the keys (label: value:).
I wonder if you are running into a 255 character limitation…

the values are being seen as hex, it’s a byproduct of yaml. :tada:

to get around it, you should try to use multiline formatting with a |- or >-

1 Like

Thanks. I’ll have to go off and look up how to use those.

The ones that look like hex due to the characters seem to work if I read correctly, the numerical ones gave the headache. The template tester sees the hex ones as strings. But I do not know if yaml tries to be smart about it too.

You are indeed correct.

The error is saying thst it expects a string. The only non-strings are the ones without quotes.

And those without quotes work. But the ones without letters in them are converted to numbers by HA here I think.

Decimal to be more precise, while the values were probably intended to be hex.

That will only happen at the template and the way to get around that is to template the entire data section.

EDIT: Which I think you mentioned above. However you should force a string

I was in doubt but I figured it was still a string up to that point due to the double quotes. But I maybe should have just to be sure.

The script is based off this web page: https://kjagiello.github.io/hue-gradient-command-wizard/.

Pick ‘Apply an effect’ and then choose the effect and the command for ZHA is shown at the bottom.

It most likely is. Only templates do the string → number magic