Service call in automation problem

Can anyone help me with the syntax for this–or a better way of doing it? This is in an automation & I get the error message when I try to save it:

Message malformed: extra keys not allowed @ data[‘data_template’]

action:
  service: input_number.set_value
    target: 
      entity_id: input_number.guest_mode
      data_template: >-
         {% if states('input_number.guest_mode') == 0 %}
           1
         {% else %}
           0
         {% endif %}
  1. Your indents are off
  2. data_template is no longer needed
  3. you are missing the configuration key for the data type you are supplying.
  4. You need quotes around 0 in your if, because states are always strings and a string can’t be == to an integer. However, it is better to use the is_state() function.
action:
  - service: input_number.set_value
    target: 
      entity_id: input_number.guest_mode
    data:
      value: >
        {% if is_state('input_number.guest_mode',  '0') %}
          1
        {% else %}
          0
        {% endif %}

Sad to say, this doesn’t work.

(In an earlier version I had used |int(0) in the if part, so I was testing for a numerical; after hours of fiddling, I took it back to a basic outline.) I’ll go back to that, haven’t used is_state much but I’ll experiment.

You say:

though I don’t know what this means, unless you’re referring to the

data:
  value: >

part, which if so, is new to me, I haven’t seen it in my hunt for information. If that’s not what you mean, you haven’t added anything I didn’t have (or pointed to a missing entity) so I’m at a loss.

Thanks, everything else is a great help. But still not working…

Your hunt must have missed the example in the documentation for Input Number showing how to set a new value using the value option.

Screenshot_20230115-100426~2

What Didgeridrew suggested should work. This version should work as well.

action:
  - service: input_number.set_value
    target:
      entity_id: input_number.guest_mode
    data:
      value: "{{ iif(states('input_number.guest_mode')|int(0) == 0, 1, 0) }}"

If neither version works for you then we will need to see the entire automation to understand what triggers the automation containing this service call.

I’d just gotten this working. Some bogey in mix. Now that I have a working version, I’ll fiddle with it. I like your one line if then.

action:
  - service: input_number.set_value
    target:
      entity_id: input_number.guest_mode
    data:
      value: |
        {% if states('input_number.guest_mode')|float(0) == 0 %}
          1
        {% else %}
          0
        {% endif %}
'''

No need to convert to float if your goal is to simply compare to an integer value. int is sufficient for comparing to zero.