Blueprint Script: Fire an event

I don’t seem to be able to fire an event in my blueprint. I have checked the script syntax (Script Syntax - Home Assistant) but I can’t seem to get it working. I have tried a few variations as you do.

My aim:

  • When a condition is met an action is performed
  • Once action performed, fire a manual event

Any help would be much appreciated, thank you

blueprint:
  name: "Nutrient Feed Phase"
  description: ''
  domain: script
  input:
    veg_phase_2:
      name: "Vegetative Phase 2 - Nutrient Type"
      description: "Select nutrient feed script" 
      selector:
        entity:
          filter:
            - domain: script

    add_nutrients_complete_event:
      name: "Add Nutrients by Phase Completed Notification"
      description: "Default has been set - This can be changed but must match manual event set in the plant feed full process automation"
      default: "add_nutrients_complete"
      selector:
        text:

variables:
  add_nutrients_complete_event: !input add_nutrients_complete_event

sequence:
- if:
  - condition: template
    value_template: >
          {% set startPhase = states('sensor.vegetative_phase_1') | as_datetime | as_local %}
          {% set endPhase = states('sensor.vegetative_phase_2') | as_datetime | as_local %} 
          {% set startDate = states('input_datetime.cycle_start_date') | as_datetime | as_local %} 
          {% set endDate = states('sensor.cycle_end_date') | as_datetime | as_local %} 
          {% set today = [now().month, now().day] %} 
          {{ today >= [startPhase.month, startPhase.day] and today < [endPhase.month, endPhase.day] and today >= [startDate.month, startDate.day] and today <= [endDate.month, endDate.day] }}
  then:
  - action:  !input veg_phase_2
  - event: event_type
    event_data:
      name: event
      customeDate: !input add_nutrients_complete_event

Can you also share how you are calling the script as well as a trace from when the script is called?

FWIW, as well as it being a bit inefficient, your conversion from a string to a datetime to just month and day could cause issues if you ever have anything that crosses from one year to the next… you can simplify the comparison like and avoid the year issue by using something like:

...
  {% set today = now() %} 
  {{ endPhase > today >= startPhase and endDate >= today >= startDate }}

Regarding the event action, when you are testing are you listening for an event type of “event_type”…? Because, that’s what you are creating.

I’m far from being an expert on events, but I’m pretty sure event: event_type is wrong and doesn’t seem to exist in the linked examples.

Was this code written by AI by any chance?

I hope I am understanding your questions correctly. I am calling the script by an automation that runs a number of scripts in a certain order, and after each script is run, an event is triggered to start the next script:

blueprint:
  name: Plant Feeding Full Process
  description: This blueprint runs all scripts neccessary to feed plants
  domain: automation
  input:
    tank_fill_script:
      name: Select Tank Fill Script
      description: "Select script to fill nutrient tank with water"
      selector:
          entity:
            filter:
              - domain: script
    tank_fill_complete_event:
      name: "Fill Tank Completed Notification"
      description: "Default has been set - This can be changed but must match manual event set in tank fill script"
      default: "fill_tank_complete"
      selector:
        text:

    add_nutrients_script:
      name: Select Nutrient Dosing by Phase Script
      description: "Select script to add nutrients to nutrient tank "
      selector:
          entity:
            filter:
              - domain: script
    add_nutrients_complete_event:
      name: "Add Nutrients by Phase Completed Notification"
      description: "Default has been set - This can be changed but must match manual event set in dosing by phase script"
      default: "add_nutrients_complete"
      selector:
        text:

    water_plants_script:
      name: Select Plant Watering Script
      description: "Select script to water plants once nutrient solution is ready"
      selector:
          entity:
            filter:
              - domain: script
    water_plants_complete_event:
      name: "Water Plants Completed Notification"
      description: "Default has been set - This can be changed but must match manual event set in water plants script"
      default: " water_plants_complete"
      selector:
        text:

variables:
  tank_fill_script: !input tank_fill_script
  tank_fill_complete_event: !input tank_fill_complete_event

  add_nutrients_script: !input add_nutrients_script
  add_nutrients_complete_event: !input add_nutrients_complete_event

  water_plants_script: !input water_plants_script
  water_plants_complete_event: !input water_plants_complete_event

# Trigger the automation when the date/time is equal to the feed date/time
triggers:
  - triggers:
      - trigger: time
        at: input_datetime.feed_date

# Set Condition so that the feed automation only runs during the cycle start and end date
conditions:
  - condition: template
    value_template: >
      {% set startDate = states('input_datetime.cycle_start_date') | as_datetime | as_local %} 
      {% set endDate = states('sensor.cycle_end_date') | as_datetime | as_local %}
      {% set today = [now().month, now().day] %} 
      {{ today >= [startDate.month, startDate.day] and today <= [endDate.month, endDate.day] }}

actions:
# Fill Tank
  - action: !input tank_fill_script
  - wait_for_trigger:
    - trigger: event
      event_type: !input tank_fill_complete_event

# Add Nutrients 
  - action: !input add_nutrients_script
  - wait_for_trigger:
    - trigger: event
      event_type: !input tank_fill_complete_event

# Water Plants
  - action: !input water_plants_script
  - wait_for_trigger:
    - trigger: event
      event_type: !input tank_fill_complete_event

mode: single

As for the trace, I’m not sure what you are referring to there sorry.

:sweat_smile: I can imagine it’s inefficient. Thanks for the suggestion on dates, I will take a look over that. The startDate and endDates are not really needed, if removing them also helps. It was just to make sure that it was all within the grow cycle. The dates that are important are the dates within each phase.

After you create your script from the blueprint, each run will create a debug trace which can be accesses from the Script menu:

You are correct, it doesn’t exist. I get to a point where I try anything that might work within the realms of what I have already learnt and don’t understand.

Nope. No AI. I have used ChatGPT on a number of occasions but it’s very limited. Most of this code comes from examples and help from talented people like @Didgeridrew

Any suggestions on how to resolve it would be much appreciated

Sorry, missed your updated question. regarding listening to the “event_type” I do have an automation setup to test that. Easy way of checking it works as you know and validated that it works by another automation that actions an event_type:

I’m not sure if this is what you’re looking for:

I noticed the event wasn’t in the trace timeline, so I added a 5 second delay to see if that did anything. Which of course it didn’t but interestingly it is now displayed in the trace timeline. However, the event isn’t logged in the logbook entries:

And I also checked that the variable input does seem to be setting the value “add_nutrients_complete” to the customdata?

So I’ve finally taken a look at your suggestion with dates. Am I correct in thinking that {{ endPhase > today >= startPhase}} is just a more efficient way of writing {{ today >= startPhase and today < endPhase}}

Regarding my event_types, I was being a bit dense. I found that below correctly fired an event:

  - event: ''
    event_data:
      event_type: !input add_nutrients_complete_event

Additionally, I may have realised that waiting to fire an event to start the next script was not the best way of approaching this and I may have found a better way of waiting to start the next script:

  - wait_template: "{{ is_state(add_nutrients_script, 'off') }}"