#Help with a variable influenced Script?

1 messages · Page 1 of 1 (latest)

manic linden
#

I think I finally have a uses case for a script but I don’t know how to implement it. So far, I’ve only been working lightly in the Automation space and I haven’t found a need for a script until now. I’m hoping that I can explain this clearly to get some help.

I have 2 separate automations that I want to include a notification but only if myself, my spouse, or both of us are home. Right now, my first automation has it built directly into the actions and I realized how painful it would be to have to copy/paste each step to the other automation and that has led me to trying out a Script for the first time.

With this in mind, how to I pass a variable to the script that says that it was triggered by Automation A vs Automation B, and in such a way that the variable makes the notification unique to the situation?

Example: “Don’t forget to swap the laundry!” vs “The freezer door was left open!”

acoustic belfry
#

In the script click on the doors in the top right corner and "add field". There you can add a text field and then when you call the script from the automation you can pass it the text of the notification to send out

opaque gust
#

But outside the box, are you two the only two in the house? Then the condition can be as simple as:

  - if:
      - condition: numeric_state
        entity_id: zone.home
        above: 0

If you only want to send it to the person home, yeah, that's a bit more complicated for which a script could be useful.

And instead of passing "automation A" or "automation B", would it not be more versatile to pass the message to the script? Then you don't need to update the script if you want to update the notification.

manic linden
manic linden
#

Any chance someone has an example of what the automation action should be?

slim ermine
# manic linden Any chance someone has an example of what the automation action should be?

From this link scroll past the first example and look at the second and third examples:

https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts

The docs for passing variables to scripts is poorly written because it makes you think fields are required. Fields are not required; their only purpose is to give you some place to input data if you call the script from the UI (for example from developer tools -> actions).

If you want to call a script from an automation and pass variables to it, you just need to pass the variables in the data: argument, which is shown in the second and third examples from the link.

Home Assistant

Instructions on how to setup scripts within Home Assistant.

#

In the script you just assume those variables exist. You don’t need the fields section at all.

If your automation calls the script and passes a variable called message then you can just reference that variable in a template in the script. Like this:

      - action: notify.mobile_app_my_phone
        data:
          message: "{{ message }}"
opaque gust
#

Yeah, field are pure for the UI. But does make it nice and easy to call in an automation without any yaml.