Propagating all variables as a template dictionary

I have a service called python_script.set_state that is able to receive arbitrary data:

service: python_script.set_state
data:
   entity_id: 'myentity'
   state: 42
   foo: 'bar'
   lorem: 'ipsum'

Now I want to wrap it around a script, that is also able to receive arbitrary data. In its sequence section in the UI, I would like to add something like this:

service: python_script.set_state
data: {{ dict_with_all_available_variables() }}

Evidently, this doesn’t work.

Please note I know that the following works:

service: python_script.set_state
data: 
   entity_id: {{ entity_id }}
   state: {{ state }}

But the challenge here is to make it work for arbitrary / unknown keys.

For context, I’m interested in wrapping the python_script into a script because I want to be able to call it via the REST API.

You can’t search the current namespace for available attributes. Your only option is to expect a dictionary as a passthrough.

Why are you using the python_script.set_state? It really serves no purpose and 9 times out of 10, you can implement what that delivers without needing it.

I have a server (in the cloud) which calls this script in order to update a sensor.server_healthcheck state.

By the way, I was able to come up with a workaround:

service: python_script.set_state
data: >
  {% set data = {'allow_create': allow_create, 'entity_id': entity_id, 'state': state} %} 
  {{ dict(data, **attributes) }}

In this case, I had to add an extra level of indirection (the use of the attributes field to store all unknown fields), but it does the trick.