Help with automation using templated json

I have an input_number component defined to store a preset value that is accessible through the lovelace interface.

I want to trigger an automation when the value changes, and then pass the same value to an action service.

Bottom line is I cannot get it to work…

As an experiment I can use the “Developer Tools | Services” area to explicitly set the data I want to send:

Service: amcrest.goto_preset
Service Data (JSON, optional):

{
  "entity_id": "camera.amcrest_camera",
  "preset": "{{ input_number.front_camera_preset }}"
}

The above setup gives the error:
“Failed to call service amgrest/goto_preset. expected int for dictionary value @data[‘preset’]”

If I instead use this as the Service data:

{
  "entity_id": "camera.amcrest_camera",
  "preset": 2
}

It works as expected…

So my question: Do I need to somehow format the template output data to be an integer.
I’m guessing the template, since it has to be enclosed in double quotes is being formatted as a string, which is not the type that the service call expects…

Any help is appreciated.
Thanx

Here are two ways to get the state of an entity:

{{ states.input_number.front_camera_preset.state }}
{{ states('input_number.front_camera_preset') }}

This is NOT how you get the state of an entity:

{{ input_number.front_camera_preset }}

To convert a numeric string to an integer, use the int filter:

{{ states('input_number.front_camera_preset') | int }}

To test templates, I recommend you use the Template Editor. Paste the template above into the left-hand side of the editor where it will be evaluated and its result shown on the right-hand side.

For example:

Ok… There’s some good information here…
I was able to recreate what you suggested…

I can see that the value returned is not an integer.
But that seems to only be the value inside the template.
The JSON formatting still requires me to put double quotes around the template.

Here is a screenshot of the latest example with the following JSON-formatted Service Data:

{
  "entity_id": "camera.amcrest_camera",
  "preset": "{{ states('input_number.front_camera_preset') | int }}"
}

Here is the screen shot after I press the “Call Service” button:

I left the double-quotes out of my examples because I was discussing the templates. You only need to delimit the template, with single or double-quotes, when you place it on the same line as the option.

option_whatever: "{{ template goes here }}"

Alternately, you do not need them if you use this convention:

option_whatever: >
  {{ template goes here }}

Ok, so I must be missing something here…

Remember that I am trying to reference the “input_number.front_camera_preset” value as the parameter for the “preset” data value inside a JSON structure.
That is where the problem seems to be…

Could you give an example of how I might do that?
I’m really hoping that there is just some syntax detail I am missing here…

The definition in my current automations.yaml, after making your suggested change, looks like this (which is still not working):

- id: '1568145494361'
  alias: Front Camera View
  trigger:
  - entity_id: input_number.front_camera_preset
    platform: state
  condition: []
  action:
  - data:
      entity_id: camera.amcrest_camera
      preset: '{{ states(''input_number.front_camera_preset'') | int }}'
    service: amcrest.goto_preset

Thanks

A few things:

  • I assume you already know that this automation will trigger only when the input_number entity’s state changes.
  • Although two single-quote characters '' are valid, my personal preference is to use one double-quote character " to delimit the entire template and the single-quote character ' to delimit strings within the template.
      preset: "{{ states('input_number.front_camera_preset') | int }}"
  • My personal preference would be to use trigger.to_state.state in the template.
      preset: "{{ trigger.to_state.state | int }}"

“Quote Pairing and when to use which”
I think that most people who have run across difficulties with templating use this format (as proposed by Taras) also.
it’s not a rule but the more people who use it, the easier for others to scan the code AND it’s good discipline for pairing the quotes.

What makes it worse is that I believe that the automation editor defaults to using the two single quotes instead of the single/double mix. I don’t have any way of verifying that since I’ve never used it but I’ve been running across that situation more recently and when pointed out it was reported that it was the way the editor created it.

A similar thing happens in regard to the order of the commonly accepted form of “triggers-conditions-actions” and all of the sub-entries below those when you use the automation editor. It puts them all in alphabetical order.

It doesn’t help when the software that is supposed to make things easier doesn’t support the commonly accepted format. It just creates more confusion for new users.

I wouldn’t be surprised. It would be easier to program that, always using single quotes.

Ok… I think my original question is still not being considered here…

For an automation I want to create an action,
that calls a service,
that passes an input_number as a parameter…

After considering the comments in this thread, here is a snapshot of my what I currently have defined:
image

Regardless of whether I use single or double quotes,
or which of the syntax choices discussed in this thread is used to derive the preset parameter,
I am still not able to make it work.

In my original post, I described an experiment that used the Developer Tools | Services page.
Unfortunately, I think that has only caused confusion about the problem statement…

So here’s a new experiment:

If I define the Automation’s Action with an explicit value for the preset, it works:

{
  "entity": "camera.amcrest_camera",
  "preset": "2"
}

If I define the preset parameter with a template it does not work:

{
  "entity": "camera.amcrest_camera",
  "preset": "{{ trigger.to_state | int }}"
}

So I’m still guessing there is a problem with the return value of the template.
What would be an easy way to inspect the actual value being returned as an action is being triggered?

Apologies,
We can see that if you present a 2 it works
present it with a template that evaluates to “2” and it doesn’t
Can you post your whole automation as just seeing this snippet may not give us the vital clue ?

Anything we provide you though will have to go in via a text editor, just so it won’t screw with the text.
This may mean that in future you will always need to use a text editor (no bad thing IMO)
Mutt

Because you can’t template in the dev tools service caller… That has never worked.

This automation should work.

- alias: move the camera
  trigger:
    platform: state
    entity_id: input_number.xxx
  action:
  - service: amcrest.goto_preset
    data_template:
      entity_id: camera.amcrest_camera
      preset: "{{ trigger.to_state.state | int }}"
1 Like

Success!
Thank you petro for your help and example…

I did read the “Important Template Rules” section in the “Automation Templating” help page:

But for others that might be following this thread, the GUI Automation editor in the Configuration menu, while it will allow configuring automations with templates, it does not insert the “data_template” keyword in place of “data” as it should.

I just needed to modify the automations.yaml manually.

Thanks again for the help.
I knew I had to just be missing something.

And that is another reason why just learning to manually create automations is best. The editors may be an OK tool to get you going in the right direction initially, but after the very beginning stages of learning programming the limited abilities of the editors are a hindrance to further learning and can confuse things.

1 Like