Issue loading script; Validation is trying to evaluate fields before running?

Hello, first time poster, I am trying to write a script for a sequence to engage the button on my A/C. It is a little tricky since it requires 2 presses, 1 to wake up the control panel, and then 1 more press to actually toggle the A/C on/off.

This is my script:

toggle_ac:
  alias: Toggle A/C
  description: ""
  sequence:
    - if:
        - condition: template
          value_template: >-
            {{ (now().timestamp() - state_attr(last_pressed_state, 'timestamp')) > 60 }}
      then:
        - type: turn_on
          device_id: "{{ device_id(switch_bot) }}"
          entity_id: "{{ switch_bot }}"
          domain: switch
        - service: input_datetime.set_datetime
          data:
            timestamp: "{{ now().timestamp()|round }}"
          target:
            entity_id: "{{ last_pressed_state }}"
    - delay:
        hours: 0
        minutes: 0
        seconds: 5
        milliseconds: 0
    - type: turn_on
      device_id: "{{ device_id(switch_bot) }}"
      entity_id: "{{ switch_bot }}"
      domain: switch
    - service: input_datetime.set_datetime
      data:
        timestamp: "{{ now().timestamp()|round }}"
      target:
        entity_id: "{{ last_pressed_state }}"
  fields:
    last_pressed_state:
      name: Last Pressed State
      required: true
      example: input_datetime.living_room_ac_button_last_pushed
      default: input_datetime.living_room_ac_button_last_pushed
      selector:
        entity:
          domain: input_datetime
    switch_bot:
      name: Switch Bot
      required: true
      example: switch.bot_40d6
      default: switch.bot_40d6
      selector:
        entity:
          integration: switchbot
          domain: switch
  mode: queued
  icon: mdi:air-conditioner
  max: 5

I’ve tested this with hardcoded entity IDs and such and it works, but trying to make it into a script where I can repeat this across different A/C units in my house is proving tricky. When I edit this YAML and try to load it into HA, it fails to load and leaves this error in my logs:

Logger: homeassistant.components.script
Source: components/script/config.py:212
Integration: Script (documentation, issues)
First occurred: 8:17:54 AM (11 occurrences)
Last logged: 8:33:42 AM

Script with alias 'Toggle A/C' failed to setup actions and has been disabled: Entity ID {{switch_bot}} is an invalid entity ID for dictionary value @ data['entity_id']. Got None
Script with alias 'Toggle A/C' failed to setup actions and has been disabled: Entity ID {{ switch_bot }} is an invalid entity ID for dictionary value @ data['entity_id']. Got None

My sense as a programmer is telling me that the evaluation of the template is happening when the YAML is loaded by HA, so since the fields have not been selected yet that results in None values and it causes the validation to fail.

My question in short is: Is there some different way of writing this so HA understands that the templates should be evaluated later?

Any help is appreciated thanks!

You can’t template anything in device triggers or actions. You can only template service:, data:, and target:.

Sorry but you’ll need to approach this differently. If switch_bot is just a switch entity_id, then simply change this

to the way less verbose call service version

        - service: switch.turn_on
          target:
            entity_id: "{{ switch_bot }}"

:clap: Wow! That did it!

I knew it would be a slightly obvious “gotcha!” Thank you so much @petro!