Reset Input Number via Automation

I’m close but I can’t seem to figure out this automation.

I have a PLC that provides the hourly runtime of my sump pump. This value appears in HA as sensor.current_hour_runtime. It resets to zero at the start of each hour.

I have an input number that I want to use to track how long my sump pump has run each day (input_number.cumulative_sump_pump_runtime).

I wrote a small automation that adds the current hour runtime to the cumulative runtime at 59 minutes and 30 seconds past the hour. This seems to be working well.

I want to reset the daily runtime value at 11:59:30 each day. I can’t seem to master how the reset value should be configured in the automation. I get these errors in my log:

Error while executing automation automation.update_sump_pump_daily_runtime. Invalid data for call_service at pos 2: extra keys not allowed @ data[‘value_template’]

Here is the automation as I currently have it:

  - alias: update sump pump daily runtime
    initial_state: True
    trigger:
      platform: time_pattern
      minutes: 59
      seconds: 30
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.cumulative_sump_pump_runtime
          value: "{{ states('sensor.current_hour_runtime')|int + states('input_number.cumulative_sump_pump_runtime')|int }}"
      - service: input_number.set_value
        data_template:
          entity_id: input_number.cumulative_sump_pump_runtime
          value_template: >
            {% if (now().hour == 23 ) %}
              0
            {% endif %}

I’ve tried Value: 0 and {{ states(‘input_number.cumulative_sump_pump_runtime’) = 0 }} but neither seem to work even though they pass the built in configuration validation.

Thank you in advance for your help formatting that if/then in the Value_template section.

you already told it that a template was forthcoming in the data section by using “data_template”. Try changing “value_template:” to just “value:”.

You also might have to move to a single line template but try it multi-line first.

The other issue is going to be what happens when the hour isn’t “23”? the template will fail at that point on an invalid service call for no “value” information given. try to come up with a satisfactory “else” statement that will always be valid if now =/= 23.

How about this:

      - service: input_number.set_value
        data_template:
          entity_id: input_number.cumulative_sump_pump_runtime
          value: >-
            {%- if (now().hour == 23 ) -%}
              0
            {%- else -%}
              {{ states('input_number.cumulative_sump_pump_runtime') }}
            {%- endif -%}

looks OK.

Give it a try.

Thank you finity, test run looks good!

Here is the final automation in case it can help someone else:

  - alias: update sump pump daily runtime
    initial_state: True
    trigger:
      platform: time_pattern
      minutes: 59
      seconds: 30
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.cumulative_sump_pump_runtime
          value: "{{ states('sensor.current_hour_runtime')|float + states('input_number.cumulative_sump_pump_runtime')|float }}"
      - service: input_number.set_value
        data_template:
          entity_id: input_number.cumulative_sump_pump_runtime
          value: >-
            {%- if (now().hour == 23 ) -%}
              0
            {%- else -%}
              {{ states('input_number.cumulative_sump_pump_runtime') }}
            {%- endif -%}

I might write this similarly like this:

  - alias: update sump pump daily runtime
    initial_state: True
    trigger:
      platform: time_pattern
      minutes: 59
      seconds: 30
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.cumulative_sump_pump_runtime
          value: "{{ states('sensor.current_hour_runtime')|float + states('input_number.cumulative_sump_pump_runtime')|float }}"
      - condition: template
         value_template: "{{ now().hour != 23 ) }}"
      - service: input_number.set_value
        data:
          entity_id: input_number.cumulative_sump_pump_runtime
          value: 0

Steve,

Help me understand the role of the condition in your example. Is it tied to the first service call or designed to prevent the second service call from running when it isn’t 11 PM?

I always want the first service call to execute, even during the 11 PM hour. At the 11 PM hour, I want both services to execute in order.

Thank you for your post, it is always good to learn different methods; even it only to help people adapt code samples to their project.

^ this…

Your interpretation is correct. It just avoids the second set altogether when not necessary.