Script with Variables: Message Malformed

I am creating the hereunder script and getting the following error when trying to save: Message malformed: Entity {{ entity2ctrl }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]

Can someone please help me with what am I doing wrong with a field/ variable script?

sequence:
  - if:
      - condition: template
        value_template: is_state('{{ entity2ctrl}}', 'unavailable')
    then:
      - wait_for_trigger:
          - platform: state
            entity_id: "{{ entity2ctrl }}"
            from: unavailable
      - service: "switch.turn_{{ entity2state }}"
        target:
          entity_id: "{{ entity2ctrl }}"
    else:
      - service: "switch.turn_{{ entity2state }}"
        target:
          entity_id: "{{ entity2ctrl }}"
mode: single
icon: mdi:camera-timer
fields:
  entity2ctrl:
    selector:
      entity: {}
    name: entity2ctrl
    required: true
  entity2state:
    selector:
      text: null
    name: entity2state
    required: true

There are at least two mistakes in the script.

First, change this:

      - condition: template
        value_template: is_state('{{ entity2ctrl}}', 'unavailable')

to this:

      - condition: template
        value_template: "{{ is_state(entity2ctrl, 'unavailable') }}"

Second, you can’t use a template for entity_id in a State Trigger.

          - platform: state
            entity_id: "{{ entity2ctrl }}"
            from: unavailable

It’s this second mistake that is responsible for the error message:

Entity {{ entity2ctrl }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]

The challenge is to find another kind of trigger that supports templates and is able to detect the entity’s previous state, because you want to detect from: ‘unavailable’). Normally I would suggest using a Template Trigger but it can’t check an entity’s previous state.

The only thing I can think of right now is to use a Template Trigger that checks if entity2ctrl has not changed to unavailable.

          - platform: template 
            value_template: "{{ states(entity2ctrl) != 'unavailable' }}" 

In other words, it waits for a state-change from any value (on, off, or unavailable) to either on or off. It’s not exactly the same as your State Trigger but, depending on your requirements, it might be close enough. After all, your script is designed to first check that the entity’s current state is unavailable. Then it wants to trigger on a state-change to some other value (on or off) and the suggested Template Trigger will do that.

1 Like

Thanks a lot @123 - that solved it.
For others, my final script:

  - if:
      - condition: template`
        value_template: "{{ is_state(entity2ctrl, 'unavailable') }}"
    then:
      - wait_for_trigger:
          - platform: template
            value_template: "{{ states(entity2ctrl) != 'unavailable' }}"
      - service: switch.turn_{{ entity2state }}
        target:
          entity_id: "{{ entity2ctrl }}"
    else:
      - service: switch.turn_{{ entity2state }}
        target:
          entity_id: "{{ entity2ctrl }}"
1 Like