Calling a script from an automation

I’m tearing my hair out trying to call a script from an automation. It is probably something simple but I’m just not seeing it so any help would be very welcome please.

This is on hass.io v0.98.5

I have an automation as follows:

- alias: 'Test automation'
  initial_state: false
  trigger:

  action:
  - service: homeassistant.turn_on
    data:
      entity_id:
        - switch.sonoff_basic_02_relay  
  - delay: '00:00:10' 
  - service: script.turn_on
    data:
      entity_id: script.test_script

And have a script as follows (this is saved in config/script/test.yaml) with script: !include_dir_merge_list script/ in configuration.yaml:

test_script:
  alias: Test from Automation
  sequence:
    - service: homeassistant.turn_off
      data:
        entity_id:
          - switch.sonoff_basic_02_relay
    - service: notify.pushover01
      data:
          title: "Test"
          message: "Test script from automation"

When I trigger the automation, the relay closes but the script does not run to open the relay, nor does the Pushover notification work.

I have reloaded automations and scripts and have restarted hass.

Check Config is not returning any errors and running it from the services tab does nothing visible - no errors and no actions.

What am I missing?

Thanks.

Richard

1 Like

What errors are generated in your log when you trigger this automation?

I’m guessing it can’t find the script. Because…

Multiple scripts are specified as a dictionary not a list (note the absence of ‘-’ in front of each script).

Use !include_dir_merge_named script/ not !include_dir_merge_list script/

Also scripts are services. You should be able to simplify the actions of the automation to this:

  action:
    - service: homeassistant.turn_on
      entity_id: switch.sonoff_basic_02_relay  
    - delay: '00:00:10' 
    - service: script.test_script

FYI the above is a list of actions. Not a dictionary of actions. Note the use of ‘-’.

This is a dictionary of scripts not a list of scripts:

fireplace_0:
  sequence:
    service: light.turn_off
    data:
      entity_id: light.fireplace

fireplace_25:
  sequence:
    service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 64

fireplace_50:
  sequence:
    service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 128

fireplace_75:
  sequence:
    service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 192

fireplace_100:
  sequence:
    service: light.turn_on
    data:
      entity_id: light.fireplace
      brightness: 255
4 Likes

As good an explanation as I have ever read, why doesn’t the documentation explain it as well as that ?

Normally I just blunder around till I get a format that works, and then blindly follow that format.

Since I have moved to packages this has become a LOT simpler.
But on the forum, dealing with all different types of user with all types of configuration, it is a nightmare trying to remember possible permutations that ‘may’ work

1 Like

@tom_l thank you so much for that very, very useful response. It works! I spent hours and hours over the last few days trying to figure out what I was doing wrong and it was so simple in the end!

@Mutt I agree, I find I read and re-read the various help pages but have never seen it expressed as clearly as Tom did above.

By the way, at the risk of going off topic! - can I ask what this means “Since I have moved to packages this…” What does moving to packages mean?

Do a search on packages.
But here’s the link to the docs : -


Basically you don’t need automation.yaml, sensors.yaml, script.yaml etc. anymore. You just have packages. Each package has everything it it (well some packages have overlaps but …)
I have a package for my bedroom 1 called light_bed1.yaml another for light_bed2.yaml etc. I have several picore and amp players in boxes controlled by a z-wave switch, so the first box has a package and it’s written so you can copy it, search and replace key strings and save as box 2, rinse and repeat. I have 37 packages (more being added). Here is an example.
input_boolean:
  ib_switch_pifi01_timer_enable:
    name: Timer Enable
    #initial: on
    icon: mdi:timer
  ib_switch_pifi01_timeslot1_enable:
    name: Time Slot 1 Enable
    #initial: on
    icon: mdi:clock-outline
  ib_switch_pifi01_timeslot2_enable:
    name: Time Slot 2 Enable
    #initial: on
    icon: mdi:clock-outline

input_number:
  in_switch_pifi01_timer:
    name: On Timer (mins)
    #initial: 30
    min: 1
    max: 360
    step: 1
    mode: box
    icon: mdi:alarm

input_datetime:
  id_switch_pifi01_tmeslt1_on:
    name: Time Slot 1 On Time
    has_time: true
    #initial: "09:00:00"
    icon: mdi:clock-start
  id_switch_pifi01_tmeslt1_off:
    name: Time Slot 1 Off Time
    has_time: true
    #initial: "10:00:00"
    icon: mdi:clock-end
  id_switch_pifi01_tmeslt2_on:
    name: Time Slot 2 On Time
    has_time: true
    #initial: "18:00:00"
    icon: mdi:clock-start
  id_switch_pifi01_tmeslt2_off:
    name: Time Slot 2 Off Time
    has_time: true
    #initial: "19:00:00"
    icon: mdi:clock-end
    
automation:
    #name: Switch H1 Off Delay
  - alias: au_switch_pifi01_offdelay
    trigger:
      - platform: state
        entity_id: switch.fib_fgwpef_wallplug_f01
        to: 'on'
    condition:
      - condition: state
        entity_id: input_boolean.ib_switch_pifi01_timer_enable
        state: 'on'
    action:
      - service: script.turn_off
        entity_id: script.sc_switch_pifi01_timer
      - service: script.sc_switch_pifi01_timer
    #radio button (toggle) timer to time slot
  - alias: au_switch_pifi01_radiob_tmr_to_slot
    trigger:
      - platform: state
        entity_id: input_boolean.ib_switch_pifi01_timer_enable
        to: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.ib_switch_pifi01_timeslot1_enable, input_boolean.ib_switch_pifi01_timeslot2_enable
    #radio button (toggle) time slot to timer
  - alias: au_switch_pifi01_radiob_slot_to_tmr
    trigger:
      - platform: state
        entity_id: input_boolean.ib_switch_pifi01_timeslot1_enable, input_boolean.ib_switch_pifi01_timeslot2_enable
        to: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.ib_switch_pifi01_timer_enable
    #turning off timer enable cancels timer
  - alias: au_switch_pifi01_cancel_tmr
    trigger:
      - platform: state
        entity_id: input_boolean.ib_switch_pifi01_timer_enable
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.sc_switch_pifi01_timer
    #time slot 1 on
  - alias: au_switch_pifi01_ontime1
    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pifi01_tmeslt1_on') [0:5] }}"
    condition:
      - condition: state
        entity_id: input_boolean.ib_switch_pifi01_timeslot1_enable
        state: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.fib_fgwpef_wallplug_f01
    #time slot 1 off
  - alias: au_switch_pifi01_offtime1
    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pifi01_tmeslt1_off') [0:5] }}"
    condition:
      - condition: state
        entity_id: input_boolean.ib_switch_pifi01_timeslot1_enable
        state: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.fib_fgwpef_wallplug_f01
    #time slot 2 on
  - alias: au_switch_pifi01_ontime2
    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pifi01_tmeslt2_on') [0:5] }}"
    condition:
      - condition: state
        entity_id: input_boolean.ib_switch_pifi01_timeslot2_enable
        state: 'on'
    action:
      - service: switch.turn_on
        entity_id: switch.fib_fgwpef_wallplug_f01
    #time slot 2 off
  - alias: au_switch_pifi01_offtime2
    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.id_switch_pifi01_tmeslt2_off') [0:5] }}"
    condition:
      - condition: state
        entity_id: input_boolean.ib_switch_pifi01_timeslot2_enable
        state: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.fib_fgwpef_wallplug_f01
    #name: Switch Pi-Fi-01 Off Cancels Timer
  - alias: au_switch_pifi01_offcancelstimer
    trigger:
      - platform: state
        entity_id: switch.fib_fgwpef_wallplug_f01
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.sc_switch_pifi01_timer

script:
  sc_switch_pifi01_timer:
    alias: Switch Pi-Fi-01 Timer Script
    sequence:
    - delay: "00:{{ states('input_number.in_switch_pifi01_timer') | int }}:00"
    - service: switch.turn_off
      entity_id: switch.fib_fgwpef_wallplug_f01

So one format for everything, need a new automation? , copy it from something similar, and fettle it.

2 Likes

Thanks, @Mutt - I’m off to do some reading (and probably head scratching!) :wink:

1 Like