Example using response from a script?

Does anybody have an example of an script or automation that calls a script, and then uses the information returned by “response_variable”? I can see on the docs how to return the value, but I cannot wrap my mind about how to use it? Thanks!

This post covers it

and this post

2 Likes

Just what I needed, thanks!

can you use the value of a response_variable in a condition home assistant ?

Something like-

condition: state
entity_id: script.is_someone_home
state: "on"

Where the script (in this case, returns either true or false) ?

You have to use a template condition.

i.e if you assign the response variable to foo and foo contained {'value': True} or {'value': False}, then…

condition: template
value_template: "{{ foo.value }}"

Perfect, ty

This should be stupidly simple but my brain has gone blank.

If the response variable returns a boolean, I get this error in the logs:

Error while executing automation automation.someone_is_home: Service response data expected a dictionary, was <class 'bool'>

This is the script:

alias: Is Someone Home
variables:
  peoplearehome: >
    {{ states('zone.home')|int(0)>0 or
    is_state('binary_sensor.people_at_home_wifi','on')}}
sequence:
  - stop: All Done
    response_variable: peoplearehome
mode: single

And this is the automation calling it (Time trigger was just to make it fire):

alias: Someone is Home
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - service: script.is_someone_home
    data: {}
    response_variable: someoneishome
  - condition: template
    value_template: "{{ someoneishome.value }}"
  - service: notify.mobile_app_my_phone_here
    data:
      message: Someone is Home
      title: Home Alert
mode: single

But according to the trace log, the script just hangs.

the returned value of a script needs to be a dictionary. Right now you’re just sending peoplearehome, which is a single boolean result. i.e. Just True or False. The return needs to be a dictionary which would be {"somekey": True} or {"somekey": False}, which in turn means you’d use {{ someoneishome.somekey }} as your condition template. Keep in mind, I used "value" in place of "somekey" but it can be anything you want it to be, but it needs to match your condition.

So change your script to output a dictionary.

alias: Is Someone Home
variables:
  peoplearehome:
    value: >
      {{ states('zone.home')|int(0)>0 or is_state('binary_sensor.people_at_home_wifi','on') }}
sequence:
  - stop: All Done
    response_variable: peoplearehome
mode: single
2 Likes

Thanks Petro, all working fine now. If I could mark it as my solution, I would :wink:

I missed that in the example above, they did it in a slightly different way:

  duration: >
    {% set value = buffer if current == 0 else current %}
    {{ {'value': value / flow } }}

Perhaps its too late to revive this subject, but I’ll give it a try. @petro is often coming to the rescue :wink:
I’m running HA 2024.4.3
I’m trying to pass a response_variable from one script to another via an automation. The final result is sent as a notify message to my mobile phone. When I use a static message and title, everything works fine.
But when I add a variable to the equation, it fails with the error message:

Error: Service response data expected a dictionary, was <class 'str'>

I can see from your reply above that you say the returned value of a script needs to be a dictionary What do you mean? That only dictionary type variables are allowed? My responsevariable is a string type, and that’s what I want it to be, so how can I solve this?

It means you have to return a dictionary.

This is a dictionary:

{'something': 'value'}

This is a string

'value'

Indeed! :+1:
Thank you so much @petro. That solved my problem :blush:

Following on from this solution, I have a script:

alias: Daily Summary Script
variables:
  my_message: >
    Today, Phil walked {{ states('sensor.galaxy_watch_daily_distance',
    with_unit=True) }} in {{ states('sensor.galaxy_watch_daily_steps') |
    int(0) }} steps.
  result: |
    {{ {'value': my_message } }}
sequence:
  - stop: End
    response_variable: result
description: ""

And an Automation that uses it:

alias: Daily Summary  Notification
description: ""
triggers:
  - at: "21:00:00"
    trigger: time
conditions: []
actions:
  - action: script.daily_summary_script
    metadata: {}
    data: {}
    response_variable: result
  - metadata: {}
    data:
      title: Phils Daily Activity Summary for {{ states('sensor.date') }}
      message: "{{ result.value }}"
    action: notify.mobile_app_phils_phone

This works, but I can’t get the string into a single variable - note the nested {{ in the script variables my_message and result - is there a simpler solution?

That’s not nested. That’s outputting a dictionary.

outputting → {{ }}
a dictionary → { ... }

outputting a dictionary {{ { ... } }}

You could also just do

alias: Daily Summary Script
variables:
  result:
    value: > 
      Today, Phil walked {{ states('sensor.galaxy_watch_daily_distance',
      with_unit=True) }} in {{ states('sensor.galaxy_watch_daily_steps') |
      int(0) }} steps.
sequence:
  - stop: End
    response_variable: result
description: ""
1 Like