Templating entity_id for input_number.set_value

I’m trying to set up an automation that can reset the power usage of my house at midnight.

My current automation is

- id: 'set_power_consumpton'
  alias: 'set_power_consumpton'
  initial_state: true
  trigger:
    platform: time
    at: '00:00:00'
  action:
  - service: script.reset_consumption
    data:
      entity_id: sensor.power_consumption_normal
  - service: script.reset_consumption
    data:
      entity_id: sensor.power_consumption_low
  - service: script.reset_consumption
    data:
      entity_id: sensor.power_production_normal
  - service: script.reset_consumption
    data:
      entity_id: sensor.power_production_low
  - service: script.reset_consumption
    data:
      entity_id: sensor.gas_consumption

and script

reset_consumption:
  sequence:
  - service: input_number.set_value
    data_template:
      entity_id: >
        {% set lst = entity_id.split('.') %}
        input_number.midnight_{{ lst[1] }}
      value: "{{ states(entity_id) }}"

However I can’t get it to work, home assistant reports.

Error while executing automation automation.set_power_consumpton. Invalid data for call_service at pos 1: Entity ID "input_number.midnight_power_consumption_normal" is an invalid entity id for dictionary value @ data['entity_id']

“input_number.midnight_power_consumption_normal” is in fact a valid entity and is visible in the UI.

I did have 5 automations before but all of it was code duplication which I don’t like. Using templates I feel it must be possible to do it with one script.

What am I missing here? I could also try yaml anchors but would rather using jinja.

can’t override entity_id because the entity_id for the script is the scripts name. It just so happens that scripts can be called directly by their entity_id.

Change it to something like this:

- id: 'set_power_consumpton'
  alias: 'set_power_consumpton'
  initial_state: true
  trigger:
    platform: time
    at: '00:00:00'
  action:
  - service: script.reset_consumption
    data:
      myentity: sensor.power_consumption_normal
  - service: script.reset_consumption
    data:
      myentity: sensor.power_consumption_low
  - service: script.reset_consumption
    data:
      myentity: sensor.power_production_normal
  - service: script.reset_consumption
    data:
      myentity: sensor.power_production_low
  - service: script.reset_consumption
    data:
      myentity: sensor.gas_consumption
reset_consumption:
  sequence:
  - service: input_number.set_value
    data_template:
      entity_id: "input_number.midnight_{{ myentity.split('.')[-1] }}"
      value: "{{ states(entity_id) }}"

Ah, so it was just an unfortunate chosen variable name, thanks!

Yeah, pretty much. I always avoid names that are close to common field names in HalA for this exact reason

Hello,

I’m interesting by your automation and script but how do you display the value after store it in the entity_id ?

Thank you !