Passing a variable from one script to another

I have made 2 scripts where I was hoping to use the data from one script to another. But that seems to be failing / not getting it to work.
Hopefully you guys can help me out.

Or point me in a nicer solution of course :smiley:

I have this script which checks who is at the door with the help of double take:

  deurbel_persoon_check:
    description: "Check who is at the front door"
    sequence:
      variables:
        person_detect: >-
          {% set time = as_timestamp(now()) %}
          {% set rik = state_attr('sensor.double_take_rik', 'timestamp') %}
          {% set amy = state_attr('sensor.double_take_amy', 'timestamp') %}
          {% set max = state_attr('sensor.double_take_max', 'timestamp') %}
          {% set sander = state_attr('sensor.double_take_sander', 'timestamp') %}
          {% set sandra = state_attr('sensor.double_take_sandra', 'timestamp') %}
          {% set postbode = state_attr('sensor.double_take_postbode', 'timestamp') %}
          
          {% if (((as_timestamp(rik)) - time) | int  >= -30 and (( time  - as_timestamp(rik))) | int  <= 30 ) %}
            Rik staat bij de voordeur
          {% elif (((as_timestamp(amy)) - time) | int  >= -30 and (( time - as_timestamp(amy))) | int  <= 30 ) %}
            Amy staat bij de voordeur
          {% elif (((as_timestamp(max)) - time) | int  >= -30 and (( time - as_timestamp(max))) | int  <= 30 ) %}
            Max staat bij de voordeur
          {% elif (((as_timestamp(sander)) - time) | int  >= -30 and (( time - as_timestamp(sander))) | int  <= 30 ) %}
            Sander staat bij de voordeur
          {% elif (((as_timestamp(sandra)) - time) | int  >= -30 and (( time - as_timestamp(sandra))) | int  <= 30 ) %}
            Sandra staat bij de voordeur
          {% elif (((as_timestamp(postbode)) - time) | int  >= -30 and (( time - as_timestamp(postbode))) | int  <= 30 ) %}
            De postbode staat bij de voordeur.
          {% else %}
            Er staat iemand bij de voordeur nogsteeds
          {% endif %}

And I have another one for controlling the google home speaker where I want to use the found text in the above script to tell who is at the door.
Bonus is that I can also use this script other messages, laundry finished ,3dprinter finished ,etc

  huiskamer_notify:
    description: "Send a TTS to all google nest devices"
    fields:
      message:
        description: "The message content"
        example: "The light is on!"
    sequence:
# Set volume to 50 procent
      - action: media_player.volume_set
        data:
          entity_id: media_player.huiskamer
          volume_level: '0.5'
# Send tts to google speakers
      - action: tts.speak
        data:
          message: "{{ message }}"
          media_player_entity_id: media_player.huiskamer
          language: nl
        target:
          entity_id: tts.google_nl_com
# Delay for 10 seconds
      - delay: "00:00:10"
# Set volume back to 40 procent
      - action: media_player.volume_set
        data:
          entity_id: media_player.huiskamer
          volume_level: '0.4'

but for the usage of the doorbell, I kick off both the scripts after the doorbell has been pressed

  doorbell_audio:
    sequence:
      - action: script.deurbel_persoon_check
      - action: script.huiskamer_notify
        data:
          message: "{{ person_detect }}"

I was than hoping the variable person_detect would have been passed through.
But looking at the logs and listening to the google home speaker it doesnt seem to like it this way…

Main reason for asking this is that I now have the first script ( checking who is at the door template) in multiple message parts.
This feel like it can be done more nice and on 1 spot for instance ( in a script :smiley: ).

2025-06-24 22:14:38.139 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'person_detect' is undefined when rendering '{{ person_detect }}'

Use a stop action to have the script.deurbel_persoon_check create a response variable.

deurbel_persoon_check:
  description: "Check who is at the front door"
  sequence:
    - variables:
        person_detect: >-
          {% set time = now().timestamp() | int(0) %}
          {% set mapper = {
          "Rik": state_attr('sensor.double_take_rik', 'timestamp'),
          "Amy": state_attr('sensor.double_take_amy', 'timestamp'),
          "Max": state_attr('sensor.double_take_max', 'timestamp'),
          "Sander": state_attr('sensor.double_take_sander', 'timestamp'),
          "Sandra":state_attr('sensor.double_take_sandra', 'timestamp'),
          "postbode": state_attr('sensor.double_take_postbode', 'timestamp') } %}
        
          {% set most_recent = mapper | dictsort(false, 'value', reverse=true) | first %}
          {% set in_time = time-30 <= most_recent[1] | int(0) <= time+30 %}
          {% if not in_time %}
            Er staat iemand bij de voordeur nogsteeds.
          {% elif most_recent[0] == 'postbode' %}
            De postbode staat bij de voordeur.
          {% else %}
            {{ most_recent[0] }} staat bij de voordeur.  
          {% endif %}
        message: "{{ {'value': person_detect} }}"
    - stop: "Respond to calling script by passing the message mapping"
      response_variable: "message"

Then:

doorbell_audio:
  sequence:
    - action: script.deurbel_persoon_check
      response_variable: script_response
    - action: script.huiskamer_notify
      data:
        message: "{{ script_response.value }}"
1 Like

Oh wow, I have not read that part yet. That seems to be the thing I’ve been looking for.

Will give it a go tomorrow. Thank you already

Hmm I have tried the following to be added to the script:

  deurbel_persoon_check:
    description: "Check who is at the front door"
    sequence:
      - variables:
          person_detect: >-
            {% set time = as_timestamp(now()) %}
            {% set rik = state_attr('sensor.double_take_rik', 'timestamp') %}
            {% set amy = state_attr('sensor.double_take_amy', 'timestamp') %}
            {% set max = state_attr('sensor.double_take_max', 'timestamp') %}
            {% set sander = state_attr('sensor.double_take_sander', 'timestamp') %}
            {% set sandra = state_attr('sensor.double_take_sandra', 'timestamp') %}
            {% set postbode = state_attr('sensor.double_take_postbode', 'timestamp') %}
            
            {% if (((as_timestamp(rik)) - time) | int  >= -30 and (( time  - as_timestamp(rik))) | int  <= 30 ) %}
              Rik staat bij de voordeur
            {% elif (((as_timestamp(amy)) - time) | int  >= -30 and (( time - as_timestamp(amy))) | int  <= 30 ) %}
              Amy staat bij de voordeur
            {% elif (((as_timestamp(max)) - time) | int  >= -30 and (( time - as_timestamp(max))) | int  <= 30 ) %}
              Max staat bij de voordeur
            {% elif (((as_timestamp(sander)) - time) | int  >= -30 and (( time - as_timestamp(sander))) | int  <= 30 ) %}
              Sander staat bij de voordeur
            {% elif (((as_timestamp(sandra)) - time) | int  >= -30 and (( time - as_timestamp(sandra))) | int  <= 30 ) %}
              Sandra staat bij de voordeur
            {% elif (((as_timestamp(postbode)) - time) | int  >= -30 and (( time - as_timestamp(postbode))) | int  <= 30 ) %}
              De postbode staat bij de voordeur.
            {% else %}
              Er staat iemand bij de voordeur nogsteeds
            {% endif %}
      - stop: 
        response_variable: "person_detect"

And still the similar follow up script

  doorbell_audio:
    sequence:
      - action: script.deurbel_persoon_check
      - action: script.huiskamer_notify
        data:
          message: "{{ person_detect }}"

Did I miss something? It still doenst pass it through according the logs

When running the script in the UI. The check person scripts seems to fail.

It mentions something about expecting a dictionary but received <class 'str>

Yes…

See my previous post.

Oh chips ! I didnt notice you edited your post. Will have a look. Sorry !

Thanks works like a charm !
The response_variable: script_response and {{ script_response.value }}

Isn’t document any where is it? I was totally unaware of the needing of those additions.

Not really… I think it’s one of those things where the docs assume there is a broader understanding of how to work with response variables from other contexts, and they give you that one sentence about using a mapping as a hint.

Just to be clear, you can use anything for the name of the response variable, you don’t have to use script_response.

EDIT:

There is a pretty good tutorial showing how to do it in the video linked at the bottom of the Script integration page.

1 Like