Invalid data for call_service at pos 1: Entity ID input_number. is an invalid entity id for dictionary value @ data['entity_id']

I’m trying to remove some errors from my logs to clean stuff up and see this one pop up everytime I restart home assistant.

Error while executing automation automation.store_last_motion_on_stairs. Invalid data for call_service at pos 1: Entity ID input_number. is an invalid entity id for dictionary value @ data[‘entity_id’]

Below is the automation that is called.

- alias: Store last motion on stairs
  trigger:
    platform: state
    entity_id:
      - binary_sensor.bewegingssensor
      - binary_sensor.motion_sensor_stairs_top
      - binary_sensor.motion_sensor_stairs_bottom
      - binary_sensor.motion_sensor_attic_stairs_top
      - binary_sensor.motion_sensor_attic_stairs_bottom
    to: 'on'
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.{{ trigger.from_state.object_id }}
      value: "{{ now().timestamp() | int }}"

It takes the object id from the trigger device and sets the current timestamp in an input_number-entity that has the same name (object id).
This is my input_number config, they do show up in home assistant and everything works as intended. All names as specified in the triggers are accounted for:

motion_sensor_stairs_top:
  min: 0
  max: 5000000000
  initial: 0
  mode: box
motion_sensor_stairs_bottom:
  min: 0
  max: 5000000000
  initial: 0
  mode: box
bewegingssensor:
  min: 0
  max: 5000000000
  initial: 0
  mode: box
motion_sensor_attic_stairs_top:
  min: 0
  max: 5000000000
  initial: 0
  mode: box
motion_sensor_attic_stairs_bottom:
  min: 0
  max: 5000000000
  initial: 0
  mode: box

Should I possibly check for the input_number platform or for the input_number.[id here] entity to be available? If so, any pointers would be appreciated!

As an experiment, change it to to_state:

entity_id: input_number.{{ trigger.to_state.object_id }}

If that still produces errors you can try this:

entity_id: input_number.{{ trigger.entity_id[14:] }}

Put quotes around the whole thing starting with input_number and ending with }}

1 Like

just to clarify for @swiftlyfalling, this is why it’s failing. You’re missing quotes.

      entity_id: "input_number.{{ trigger.from_state.object_id }}"

Thanks for all your help! Just to be sure (and learn something), I’ve tried all the combinations.

@petro and @swiftlyfalling With or without quotes doesn’t seem to matter in this case.

@123 switching trigger.from_state.object_id to trigger.to_state.object_id did work. The error is now gone. So it seems like on startup there is no from state when the automation triggers.
Can you explain what trigger.entity_id[14:] is? Never seen that before.

I suspected the issue might occur at startup when the entity lacks a previous state so using to_state avoids it.

The [14:] means slice off the first 14 characters and return the remainder of the string. The first 14 characters discarded would be binary_sensor. thereby leaving just the entity’s name.

Thanks for the explanation!