Change trigger.entity_id from sensor to switch in automation action

Hi all

I have a few sensor and the associated switch entity_ids in the following format: sensor.microwave_power / switch.microwave
sensor.coffee_machine_power / switch.coffee_machine
etc.

I want a trigger to fire when the power usage numerical_state is above 1500

The trigger’s action should then switch off the relevant outlet.

- alias: "LTSS Overcurrent Switch off"
  trigger:
    platform: numeric_state
    entity_id:
      - sensor.ltss_microwave_power
      - sensor.ltss_airfryer_toaster_power
    above: 1500
    for: "00:00:15"
  action:
    - service: switch.turn_off
      target:
        entity_id: "{{ trigger.entity_id | replace('sensor','switch') | replace('_power', '') }}"

This unfortunately does not seem to work
Any ideas?

Your template to transform the entity_id looks fine. What about it doesn’t work? Does the automation never trigger? Does it trigger but you get errors? Perhaps share a trace from a failed automation run.

Sorry. Thank you for the quick reply. I see that it is working now, but now the message part does not work? The replace parts are not taking working.

- service: tts.google_say
   entity_id: media_player.home_group
   data:
     message: "Notification: I switched off the {{ trigger.entity_id | replace('_power', '') | replace('ltss_', '') }} Outlet"

Got it working by adding a variable…

variables:
  named_entity: "{{ state_attr(trigger.entity_id, 'friendly_name') | replace('LTSS ','') }}"
action:
- service: tts.google_say
  entity_id: media_player.home_group
  data:
    message: "Notification: I switched off the {{ named_entity }}"

the indentation is wrong on that, you shouldn’t mark it as a solution.

service should be in-line with entity_id and data.

e.g.

 - service: tts.google_say
   entity_id: media_player.home_group
   data:
     message: "Notification: I switched off the {{ named_entity }}"

OK Petro. I fixed it.