How can repeat this automation?

hello i have this automation

- alias: 'Porta casa de banho'
  trigger:
    platform: state
    entity_id: binary_sensor.porta_casa_de_banho
    state: 'on'
    for:
      seconds: 20
  action:
    - service: shell_command.play_wc

What can I do to make this automation repeat every 20 seconds?
thanks

Like this

automation:
  - alias: 'Porta casa de banho'
    trigger:
      platform: state
      entity_id: binary_sensor.porta_casa_de_banho
      state: 'on'
      for:
        seconds: 20
    action:
      - service: script.stuff

script:
  stuff:
    sequence:
      - service: shell_command.play_wc
      - service: script.stuff_loop
  stuff_loop:
    sequence:
      - delay:
          seconds: 20
      - service: script.stuff

Although this might not what you actually want…this loops until forever! If you only want it to loop as long as the state persists you might want to add a condition to those scripts like this:

automation:
  - alias: 'Porta casa de banho'
    trigger:
      platform: state
      entity_id: binary_sensor.porta_casa_de_banho
      state: 'on'
      for:
        seconds: 20
    action:
      - service: script.stuff

script:
  stuff:
    sequence:
      - condition: state
        entity_id: binary_sensor.porta_casa_de_banho
        state: 'on'
      - service: shell_command.play_wc
      - service: script.stuff_loop
  stuff_loop:
    sequence:
      - condition: state
        entity_id: binary_sensor.porta_casa_de_banho
        state: 'on'
      - delay:
          seconds: 20
      - service: script.stuff

Or you could add an automation to kill those scripts off when the state changes to off like this:

automation:
  - alias: 'Porta casa de banho'
    trigger:
      platform: state
      entity_id: binary_sensor.porta_casa_de_banho
      state: 'on'
      for:
        seconds: 20
    action:
      - service: script.stuff
 - alias: 'Porta casa de banho NOT'
    trigger:
      platform: state
      entity_id: binary_sensor.porta_casa_de_banho
      state: 'off'
    action:
      - service: script.turn_off
        data:
          entity_id: script.stuff
      - service: script.turn_off
        data:
          entity_id: script.stuff_loop

script:
  stuff:
    sequence:
      - service: shell_command.play_wc
      - service: script.stuff_loop
  stuff_loop:
    sequence:
      - delay:
          seconds: 20
      - service: script.stuff

Might want to check out this if it suffices to have a notification https://home-assistant.io/components/alert/.

~Cheers

5 Likes

Thank you so much for yours knowledges. do you have ghitub for me to see your stuff?

1 Like

No sorry I don’t have my config on github. It wouldn’t be impressive anyways as I don’t have that much set up myself :slight_smile:

If you have any questions or need help with so!merging feel free to ask.

~Cheers

1 Like

@PhyberApex hello again, i get home now and i did some tests:
on this case:

automation:

  • alias: ‘Porta casa de banho’
    trigger:
    platform: state
    entity_id: binary_sensor.porta_casa_de_banho
    state: ‘on’
    for:
    seconds: 20
    action:
    • service: script.stuff

It should not be like this

action:

  • service: script.turn_on
    entitidy id: script.stuff

???
i did that changes and i dont hear the soud. this line not show me on .log

17-02-22 13:07:41 INFO (MainThread) [homeassistant.core] Bus:Handling Event call_service[L]: service_call_id=3050659088-1, service_data=, service=play_wc, domain=shell_command

and if i run my code:

  • alias: ‘Porta casa de banho’
    trigger:
    platform: state
    entity_id: binary_sensor.porta_casa_de_banho
    state: ‘on’
    for:
    seconds: 20
    action:
    • service: shell_command.play_wc

show me.

You don’t need that change you can call scripts directly. Your change should not work.

~Cheers

if a call script directly like this;
-service: script.stuff

give this error:

17-02-22 13:25:09 INFO (MainThread) [homeassistant.components.automation] Executing Porta casa de banho aberta
17-02-22 13:25:09 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: message=has been triggered, domain=automation, entity_id=automation.porta_casa_de_banho_aberta, name=Porta casa de banho aberta>
17-02-22 13:25:09 INFO (MainThread) [homeassistant.helpers.script] Script Porta casa de banho aberta: Running script
17-02-22 13:25:09 INFO (MainThread) [homeassistant.helpers.script] Script Porta casa de banho aberta: Executing step call service
17-02-22 13:25:09 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_call_id=3050388752-1, service_data=, domain=script, service=stuff>
17-02-22 13:25:09 WARNING (MainThread) [homeassistant.core] Unable to find service script/stuff

i think that problem is on SERVICES:
when i click DOMAIN after SERVICES should not be appears “stuff”???

Yes it should be there do you have the script section of what I posted in your config? Is the script component loaded up in your logfile?

~Cheers

@PhyberApex

17-02-22 20:44:52 INFO (MainThread) [homeassistant.bootstrap] Setting up script
17-02-22 20:44:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=script, service=turn_on>
17-02-22 20:44:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=script, service=turn_off>
17-02-22 20:44:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=script, service=toggle>
17-02-22 20:44:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event component_loaded[L]: component=script>

script.stuff no

Just to make sure you have that script in your configuration right? And you did a restart after yes?

~Cheers

@PhyberApex
configuration.yaml

script: !include_dir_merge_list includes/scripts
automation !include_dir_merge_list includes/automation
shell_command: !include includes/shell_command.yaml

inside folder includes/scripts
i have two scripts.

stuff.yaml

sequence:
    - condition: state
      entity_id: binary_sensor.porta_casa_de_banho
      state: 'on'
    - service: shell_command.play_wc
    - service: script.stuff_loop

stuff_loop.yaml:

sequence:

- condition: state
  entity_id: binary_sensor.porta_casa_de_banho
  state: 'on'
  - delay:
    seconds: 20
  - service: script.stuff

inside folder automation

portacasadebanho,yaml

  • alias: ‘Porta casa de banho’
    trigger:
    platform: state
    entity_id: binary_sensor.porta_casa_de_banho
    state: ‘on’
    for:
    seconds: 20
    action:
    - service: script.stuff

shell_command.yaml
play_wc: sudo omxplayer /sons/wc.mp3

and yes i restarted always when i change something.

17-02-22 23:10:08 WARNING (MainThread) [homeassistant.core] Unable to find service script/stuff
any ideas?

if i change this:

- alias: 'Porta casa de banho'
  trigger:
    platform: state
    entity_id: binary_sensor.porta_casa_de_banho
    state: 'on'
    for:
      seconds: 20
  action:
    - service: script.stuff

for this:

  • alias: ‘Porta casa de banho’
    trigger:
    platform: state
    entity_id: binary_sensor.porta_casa_de_banho
    state: ‘on’
    for:
    seconds: 20
    action:
    • service: script.turn_on
      entity_id: script.stuff

shows me:

17-02-22 23:17:32 INFO (MainThread) [homeassistant.components.automation] Executing Porta casa de banho
17-02-22 23:17:32 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: message=has been triggered, entity_id=automation.porta_casa_de_banho, name=Porta casa de banho, domain=automation>
17-02-22 23:17:32 INFO (MainThread) [homeassistant.helpers.script] Script Porta casa de banho: Running script
17-02-22 23:17:32 INFO (MainThread) [homeassistant.helpers.script] Script Porta casa de banho: Executing step call service
17-02-22 23:17:32 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=entity_id=['script.stuff'], service=turn_on, service_call_id=3050736912-1, domain=script>
17-02-22 23:17:32 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=3050736912-1>

but i cant hear mp3 sound

Could you try without the include? Just put it directly in your configuration.yaml.

~Cheers

@PhyberApex yes now it works, Why does not my way work?

Hey try changing

to include_dir_named with your way. That’s how I did it.

~Cheers

1 Like

Now it works very well.
it was hard. Lol

thank you so much for your patience.

1 Like

hi eveybody
i use this configuration but i got only two notify
do you know the problem?

automation:
    - alias: 'Porta casa de banho'
      trigger:
      - above: '1'
        entity_id: sensor.ovenoo
        platform: numeric_state
      action:
        - service: script.stuff

    - alias: 'Porta casa de banho NOT'
      trigger:
      - below: '50'
        entity_id: sensor.ovenoo
        platform: numeric_state
        for:
          seconds: 20
      action:
        - service: script.turn_off
          data:
            entity_id: script.stuff
        - service: script.turn_off
          data:
            entity_id: script.stuff_loop

script:
   stuff:
    sequence:
      - service: notify.ios_vahids_iphone
        data:
          title: "Home Assistant Alert"
          message: "oven is on!"
      - service: script.stuff_loop
  stuff_loop:
    sequence:
      - delay:
          seconds: 10
      - service: script.stuff

I have the exact same issue:

When switch.main_pump goes to ‘off’, it alerts twice.
When switch.main pump goes to ‘on’ I get the restarted message.

Toggling again gets two more alerts.

My expection is that the script would repeat until switch.main_pump goes to ‘off’.

Automation:

- alias: 'Main pump stopped alert'
  trigger:
    platform: state
    entity_id: switch.main_pump
    from: 'on'
    to: 'off'
    for:
      seconds: 5
  action:
    - service: script.main_pump_alert
 
- alias: 'Main pump running again'
  trigger:
    platform: state
    entity_id: switch.main_pump
    from: 'off'
    to: 'on'
  action:
    - service: notify.ALL_DEVICES
      data:
        title: "Koi pond..."
        message: "...main pump has restarted"
    - service: script.turn_off
      data:
        entity_id: script.main_pump_alert
    - service: script.turn_off
      data:
        entity_id: script.main_pump_alert_loop

Scripts:

main_pump_alert:
    sequence:
      - service: notify.ALL_DEVICES
        data:
          title: "Koi pond..."
          message: "...main pump has stopped"
      - service: script.main_pump_alert_loop

  main_pump_alert_loop:
    sequence:
      - delay:
          seconds: 5
      - service: script.main_pump_alert

Did you find a solution?

Found it!

The scripts have to be written thus:


  main_pump_alert:
    sequence:
      - service: notify.ALL_DEVICES
        data:
          title: "Koi pond..."
          message: "...main pump has stopped"
      - service: script.turn_on
        data:
          entity_id: script.main_pump_alert_loop

  main_pump_alert_loop:
    sequence:
      - delay:
          seconds: 5
      - service: script.turn_on
        data:
          entity_id: script.main_pump_alert