Converting names with spaces to entity names

I’ve been figuring out the integration of a 3rd party automation controller and its entities and now have 2-way communication and control (ie I can use HASS or native interfaces that the state of the entity is kept aligned).
Next step is to roll this out for 10s of entities eg lights, areas etc. The 3rd party controller allows spaces in the names, HASS doesn’t in its entity names. I could add pages of similar entries for each light etc but is there a way to programatically convert a name with spaces to a name with eg underscores?
eg the following webhooks work for one light, if I could get the light name that the 3rd party system uses into an entity name I could have 1 webhook for each class of entity.
Thanks

- alias: cortex light state
  description: "update light status on webhook message"
  trigger:
    - platform: webhook
      webhook_id: cortex-light-state
  action:
    service: python_script.hass_entities
    data:
      action: set_state
      entity_id: light.familyrm_light_star
      state: "{% if 'On' in trigger.json.Cortex_HAAPI.State %}on  {% else %}off {% endif -%}"
#

Similarly for control I’ve hard coded but if the entity name could be converted to one with spaces it would simplify things

#rest_command:
  familyrm_light_star_off:
    url: http://192.168.0.78/api/v1/json/objects/family%20room%20star?Turn%20off=1
    method: POST
    headers:
      authorization: !secret CortexAuthBasic
    content_type: "application/json"
    payload: "{}"

Templating - String Filters

slugify

{{ entity.entity_id | replace("_","%20") }}

obviously replace entity.entity_id with whatever you need to

Thanks both! My weekend plans to implement have been thwarted, but I’ll post an update when done :+1:

Got this working for the webhook, working well so far, thanks for the help !

- alias: cortex light state update
  description: "update light status on webhook message"
  trigger:
    - platform: webhook
      webhook_id: cortex-light-state-update
  action:
    service: python_script.hass_entities
    data:
      action: set_state
      entity_id: light.{{trigger.json.Cortex_HAAPI.Light |lower | replace(" ","_")}}
      state: "{% if 'On' in trigger.json.Cortex_HAAPI.State %}on  {% else %}off {% endif -%}"

If you use slugify you don’t need to use lower (or replace).

entity_id: 'light.{{ trigger.json.Cortex_HAAPI.Light | slugify }}'

That is neater, don’t know why I missed it! Thanks!
Would be even neater if I could use friendly name as an automation parameter