Struggling to run a (looping) script from Lovelace UI

Hi!
I’ve read (extensively I thought…) the forum to figure out how to create a button on my overview UI which would trigger a script that powers on (2 minutes) and off (15 seconds) a plug, as long as the button is on.

I am facing a few issues (which could all be related):

  1. adding the script in my scripts.yaml seemed not to work and Lovelace tells me there is no such script
  2. adding it to configuration.yaml instead somewhat does the same as Lovelace says entity not available

So here is what I added:

usbpi_test:
  alias: Test the USB Key on a Pi
  sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.plug
    - delay:
        minutes: 2 # time for Pi to start properly
    - service: switch.turn_off
      data:
        entity_id: switch.plug
    - service: script.turn_on
      data:
        entity_id: script.usbpi_test_loop
usbpi_test_loop:
  alias: USB key test - Loop
  sequence:
    - delay:
        seconds: 15 # time to leave the Pi off
    - service: script.turn_on
      data:
        entity_id: script.usbpi_test

(also had identations issues but those seems to be resolved).

Now hassio is giving me this error:
Invalid config for [script]: expected dictionary for dictionary value @ data[‘script’]. Got [OrderedDict([(‘alias’, ‘Test the USB Key on a Pi’), (‘usbpi_test’, None), (‘sequence’, [OrderedDict([(‘service’, ‘switch.turn_on’), (‘data’, None), (‘entity_id’, ‘switch.plug’)]), OrderedDict([(‘delay’, OrderedDict([(‘minutes’, 2)]))]), OrderedDict([(‘service’, ‘switch.turn_off’), (‘data’, OrderedDict([(‘entity_id’, ‘switch.plug’)]))]), OrderedDict([(‘service’, ‘script.turn_on’), (‘data’, OrderedDict([(‘entity_id’, ‘script.usbpi_test_loop’)]))])])]), OrderedDict([(‘alias’, 'USB key test - Lo…(See /config/configuration.yaml, line 32). Invalid config for [automation]: Invalid time specified: 120 @ data[‘trigger’][0][‘at’][0]. Got None. (See /config/configuration.yaml, line 28).

Any idea what I am missing?

Thank you.
Fred

Just a guess. Shouldn’t the ‘data’ lines be indented below ‘service’ instead of being at the same level? Like so:

- service: script.turn_on
    data:
      entity_id: script.usbpi_test_loop

Or try service_data instead of data:

- service: script.turn_on
  service_data:
    entity_id: script.usbpi_test_loop

Thank you for the suggestion. Tried both: 1st one gives me the red invalid notification from the editor, the 2nd one gives me the same error.

So apart from not being able to add it, maybe it doesn’t work at all :wink:

Fred

ok seems it’s tricky then… Maybe I can break it in 2:

  1. How to run a script(automation?) which turn on (for 2 minutes) and off (for 15 seconds) my smart plug?
  2. How to make a button in the dashboard that activates some script?

Thank you.

OK so I have fixed issue No.1 which is to start something from the Overview. There is a page on home assistant kind of explaining it and it worked after trials and errors (https://www.home-assistant.io/integrations/input_boolean/ ) . I started doing it to turn on a lamp and then moved on little by little.

I am still stuck with the loop though. I’ve tried a few iteration and all failed. Here is the latest one, hopefully someone can step in and point the obvious because I’m really lost on the syntax.

- id: '32479234782383'
  alias: Start testing that USB key
  trigger:
    platform: state
    entity_id: input_boolean.rpi
    to: 'on'
  condition: []
  mode: queued
  action:
    sequence:
    - service: switch.turn_off
      data: {}
      entity_id: switch.plug
    - repeat:
      count: 2000
      sequence:
      - delay:
        seconds: 75
      - service: switch.toggle
        data: {}
        entity_id: switch.plug

I have also tried using data_template: and an indented entity_id line below, with no success.
Thank you for any suggestion.

Ok. So I couldn’t make it in HA but found out that the TP-Link HS110 has been reversed engineered. Found a script which turns it on, off, checks status, gives power consumption reading at https://gist.github.com/Khoulaiz/5ef21532585a64bc455c24070634cf14
Wrote my own bash script to do exactly what I wanted and it’s working great!
If anyone has similar needs one day, here it is:

#!/bin/bash

for value in {1..2000}
do
  ./hs110.sh IP port on
  sleep 90
  ./hs110.sh IP port off
  sleep 10
done

echo All done

Thank you for trying to help. I am a happy camper now!

Fred