#Force cast blueprint variable to string

1 messages · Page 1 of 1 (latest)

scenic swallow
#

I have a variable that is being read in from an attribute on an entity. This particular attribute is a 4 - 8 digit string I need to pass it as a string to a service call that requires it be a string as leading zeros are important for this call since it's setting PIN codes on a lock. Every permutation of rendering the variable into the call as a variable and I get the following error:

Error while executing automation automation.testing: not a string value: 4444 for dictionary value @ data['code']

I've tried forcibly casting to string via | string when reading in the attribute, I've tried forcibly casting to string when using it in the action call. I've even tried using >- and putting the value write on the next line to see if it will fix the problem but none of them seem to be working. Any ideas?

Yes, I've also tried doing my attribute lookup directly inline with the call.

This seems to be an issue only for PINs that do not start with a leading 0 which is most PINs. If the PIN starts with a 0 then everything works as expected.

radiant moss
#

can you post the yaml of the automation action?

#

The problem is probably that you are using a template, and that the template parser will convert something which looks like an integer to an integer

#

you need to template the whole data section to avoid that

scenic swallow
#
- choose:
            - conditions:
                - condition: template
                  value_template: "{{ which_event != 'none' }}"
              sequence:
                - action: schlage.add_code
                  metadata: {}
                  data:
                    entity_id:
                      - "{{ lock }}"
                    code: >-
                      {{ code }}
                    name: "{{ slot_name }}"
                - action: notify.persistent_notification
                  metadata: {}
                  data:
                    title: Set code
                    message: "Set code '{{ code }}' in slot {{ slot_name }}"
            - conditions:
                - condition: template
                  value_template: "{{ which_event == 'none' }}"
              sequence:
                - action: notify.persistent_notification
                  metadata: {}
                  data:
                    title: No code to set
                    message: "No code to set"
          default:
            - action: notify.persistent_notification
              metadata: {}
              data:
                title: No action taken
                message: "No action taken in trip start"
#

That's the section of the blueprint that is failing with any code that is not leading with a 0

#

How do you mean template the whole data section. So something more like:

- action: schlage.add_code
  metadata: {}
  data: >-
    entity_id:
      - {{ lock }}
    code: "{{ code }}"
    name: "{{ slot_name }}"

?

#

Hrmm... just found some information on data_template I'm going to convert the data section to that and see if it fixes it.

#

Though I'm not 100% that I should use it since I find information that data_template is deprecated???

glossy drum
#

Your only option is what @radiant moss already said:

you need to template the whole data section to avoid that

scenic swallow
#

Ok, I'm going to try with the following:

data: >-
  {%- set payload = {
    "entity_id": [ "{{ lock }}" ],
    "code": "{{ pin }}",
    "name": "{{ slot_name }}"
  } -%}
  {{ payload }}

To see if that corrects it (I've changed the name of the code variable to pin so that I'm absolutely certain I'm calling it correctly

glossy drum
#

that looks correct but you can just go straight to the goodness. EDIT: You can't nest templates

#

with

data: >
  {{ {
    "entity_id": [ lock ],
    "code": pin,
    "name": slot_name
    } }}
#

you can also make the quoting easier with

scenic swallow
#

hmm.... ok, if this works I'll simplify the actual code to that.

glossy drum
#
data: >
  {{ dict(entity_id=[lock], code=pin, name=slot_name) }}
#

worse case you need to use the string filter in place of pin. e.g. pin | string

scenic swallow
#

I'm currently making sure it's a string when I set the variable by putting my variable set through a | string filter

radiant moss
#

BTW, the input for entity_id doesn't need to be a list, you can just use a string with the entity_id

glossy drum
scenic swallow
#

Oh? Ok, let me try your latest version and pass the it as code=pin | string

glossy drum
#

Did it work?

scenic swallow
#

Was just coming back to report that it works! Thank you! that takes care of the most picky bug I've had with this blueprint. Now if I can get my other part of it fixed I'll be golden, but I've got a workable, though not 100% ideal (yet) blueprint 🙂