Repetitive task for test bench - "for loop?"

Hi!
My name is Jose and i am very noob using Home Assistant.
I am designing a work bech to help me test home apliance in home and i need help.

I am using some smart relays that have current meassurment. I connected one to Home Assistant to test it and worked, i can change the state of the relay and see the current in the dash board.

Now i need help to create the automation. The problem is that i dont know if i should use “Script” or “Automations” or what… i am quite confused about all the tools abaible and dont know how to even search what i need.

My idea is to have 4 relays that are 10 minutes ON, and then 1 minute OFF (for example) y the time ON i will log the current and then export it to a “cvs” or whatever, but i need to run this task for hours, even days.

In programming its simple i use a for loop and then delays or whatever, but i cant get around how to do this in home assistant.

I read about scripts, automations, blueprints, timers (helpers) and more and cant figure it out how to do it.

Can someone tell me where to start?

Thanks!

You can do it with an automation.

trigger:
  - id: 'turn_off'
    platform: state
    entity_id: switch.your_relay
    to: 'on'
    for:
      minutes: 10
  - id: 'turn_on'
    platform: state
    entity_id: switch.your_relay
    to: 'off'
    for:
      minutes: 1
action:
  - service: "switch.{{ trigger_id }}"
    target:
      entity_id: switch.your_relay
2 Likes

One typo in an otherwise wonderfully-elegant automation:

  - service: "switch.{{ trigger.id }}"

You could create a toggle helper (input_boolean) to provide a switch turn the automation on and off from the UI dashboard:

trigger:
  - id: 'turn_off'
    platform: state
    entity_id: switch.your_relay
    to: 'on'
    for:
      minutes: 10
  - id: 'turn_on'
    platform: state
    entity_id: switch.your_relay
    to: 'off'
    for:
      minutes: 1
  - id: 'toggle'
    platform: state
    entity_id: input_boolean.run_cycle
    to: 'on'
condition:
  - condition: state
    entity_id: input_boolean.run_cycle
    state: 'on'
action:
  - service: "switch.{{ trigger.id }}"
    target:
      entity_id: switch.your_relay
2 Likes

Thanks Troon i will search this idea to test it.

If you say automation its the best way i will try it.

For now i use Node-red and a eWelink API for a “quick solution” but the API its quite old, and i dont know why the node red server goes offline quite often when i use more than one device.

Thanks again and i will read the documentation to try to understand how the code that both gave me works.

Thanks tom_I.
If you say automation, i will read and try it.

First i need to understand how to read and code in YAML (that i suppose its the laguage that you wrote).

Again thanks!