Creating string value from input_number state in automation actions

If I configure the automation action as

service: ocpp.configure
data:
  ocpp_key: MeterValueSampleInterval
  value: "120"

it works well. But if I try

service: ocpp.configure
data:
  ocpp_key: MeterValueSampleInterval
  value: "{{ states('input_number.ocpp_test') | round (0)}}"

I get the ERROR:

2021-09-07 09:47:56 ERROR (MainThread) [homeassistant.components.automation.ocpp_test] OCPP test: Error executing script. Invalid data for call_service at pos 1: expected str for dictionary value @ data['value']
2021-09-07 09:47:56 ERROR (MainThread) [homeassistant.components.automation.ocpp_test] Error while executing automation automation.ocpp_test: expected str for dictionary value @ data['value']

How to convert the numerical value to a string? Or what should be done?

value: "{{ states('input_number.ocpp_test') | int }}"

Rounding to 0 is the same as forcing to int.

But you could also use:

value: "{{ states('input_number.ocpp_test') | float | round (0)}}"

but it makes no sense.

I think it’s a similar issue to

An int is passed to the service call, which asks for a string.

As a confirmation/workaround, you can try enabling legacy_template, that render all templates as strings

homeassistant:
  legacy_templates: true

So you think it wants a string value?
That could be correct. I thought it gave the error because you can’t round a string.

But you might be correct.
In that case something like:

value: "{{ (states('input_number.ocpp_test') | int) | str }}"

should “round” it then make it string.

the template engine will type it no matter what. I believe this requires a bug report for this specific service call.

Yes, it does: ocpp/custom_components/ocpp/api.py at 258871056caba6d9681cedb36c97f6ff950ff6eb · lbbrhzn/ocpp · GitHub

It won’t work (see the issue), I think. The template rendering will “infer” the python type (int) from the end result

value: "{{ (states('input_number.ocpp_test') | int) | str }}"

This did not work. Same error occurs.

Enabling legacy_template made it work. But it broke other integrations, so I can not keep it enabled.

You need to write up an issue against the integration. It’s a bug in the custom integrations code. It should be updated to handle any type and convert the type to a string.

Ok, thanks. I will.

Arguable…

You have the same issue with input_text.set_value

</s> <s>service: input_text.set_value</s> <s>target:</s> <s> entity_id: input_text.test_text</s> <s>data:</s> <s> value: {{ "120" }}</s> <s>

fails the same way, while

</s> <s>value: 120</s> <s>

works, which is pretty counter-intuitive :wink:

Nah, fails in developer tools for other reasons.

That’s because you should be using an input_number when dealing with only a number. Not my logic, but what the core expects. You could easily write that up as well. It should behave the same way as message.

My bad. It actually works because cv.string is used, wich coerce to string.

Only if the value n is already a whole number or less than n.5

For example, if the value n is 1.5

{{ 1.5 | round(0) }}

produces 2 whereas this

{{ 1.5 | int }}

produces 1.