jonasb
September 7, 2021, 10:08am
1
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?
Hellis81
(Hellis81)
September 7, 2021, 11:09am
2
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.
koying
(Chris B)
September 7, 2021, 11:16am
4
I think it’s a similar issue to
opened 12:54PM - 30 Aug 21 UTC
closed 08:19PM - 04 Sep 21 UTC
The problem
Ref: https://community.home-assistant.io/t/file-output-with-curly-braces/333609?u=koying
It's not possible to output valid JSON from a template in a service call.
Outputting a non-template JSON string works.
Outputting...
integration: template
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
Hellis81
(Hellis81)
September 7, 2021, 11:22am
5
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.
petro
(Petro)
September 7, 2021, 11:29am
6
the template engine will type it no matter what. I believe this requires a bug report for this specific service call.
koying
(Chris B)
September 7, 2021, 11:31am
7
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
jonasb
September 7, 2021, 11:35am
8
value: "{{ (states('input_number.ocpp_test') | int) | str }}"
This did not work. Same error occurs.
jonasb
September 7, 2021, 11:38am
9
Enabling legacy_template
made it work. But it broke other integrations, so I can not keep it enabled.
petro
(Petro)
September 7, 2021, 11:39am
10
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.
koying
(Chris B)
September 7, 2021, 12:45pm
12
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
Nah, fails in developer tools for other reasons.
petro
(Petro)
September 7, 2021, 12:57pm
13
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.
koying
(Chris B)
September 7, 2021, 1:07pm
14
My bad. It actually works because cv.string
is used, wich coerce to string.
123
(Taras)
September 7, 2021, 1:07pm
15
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
.