Problem with templating in automation action

I’m trying to make some automations for a utility meter. I use different input_numers to keep values persistent and at certain times I want to reset the different input_numbers. My idea was to use an automation that triggers once a day and then use templating to reset certain values once a day, once a month and once a year. But my automation doesn’t work, the values doesn’t reset.

This is the code for my automation:

- alias: Energy - Reset input_numbers 
  initial_state: on
  trigger:
    platform: time
    at: "0:00:03"
  action:
    - service: input_number.set_value
      data_template:
        entity_id: >-
          {% if now().month == 1 and now().day == 1 %}
            - input_number.energy_daily
            - input_number.energy_monthly
            - input_number.energy_yearly
          {% elif now().day == 1 %}
            - input_number.energy_daily
            - input_number.energy_monthly
          {% else %}
            - input_number.energy_daily
          {% endif %}
        value: '0'

Can someone please tell me what is wrong with my code?

Try it like this:

      data_template:
        entity_id: >

By using “>-” I think you are stripping the line termination characters from your result template making the entity id list look like this:

 - input_number.energy_daily - input_number.energy_monthly - input_number.energy_yearly

What errors are you seeing in the log?

EDIT: Actually “>-” only strips the last newline so that’s probably not it.

The log says

Error while executing automation automation.energy_-_reset_input_numbers. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

Try it like this:

  action:
    - service: input_number.set_value
      data_template:
        entity_id: >
          {% if now().month == 1 and now().day == 1 %}
            - input_number.energy_daily, input_number.energy_monthly, input_number.energy_yearly
          {% elif now().day == 1 %}
            - input_number.energy_daily, input_number.energy_monthly
          {% else %}
            - input_number.energy_daily
          {% endif %}
        value: '0'
1 Like

Templates report their results as strings. In your automation, your template is attempting to report its result as a list. It may look like a list but it’s still a string.

The entity_id option requires multiple entities to be presented either as a list (which is what you tried to do with your template) or as a comma-delimited string. We will choose the second option because a template can only produce a string.

- alias: Energy - Reset input_numbers 
  initial_state: on
  trigger:
    platform: time
    at: "0:00:03"
  action:
    - service: input_number.set_value
      data_template:
        entity_id: >-
          {% if now().month == 1 and now().day == 1 %}
            input_number.energy_daily, input_number.energy_monthly, input_number.energy_yearly
          {% elif now().day == 1 %}
            input_number.energy_daily, input_number.energy_monthly
          {% else %}
           input_number.energy_daily
          {% endif %}
        value: '0'

2 Likes

That did the trick, thank you!

1 Like