Script - Loop - Review my code

That’s my point, you need to make an automation, not a script. An automation is closest to a “service”. A script is 1 and done.

So switch.chargeur_rom controls power to the phone charger and you have a separate switch that controls power to the desk. Is this separate switch controllable by Home Assistant or is it simply a manual switch?

I have modify the script in the way of loop

alias: Chargeur Téléphone Bureau Tweek
sequence:
  - repeat:
      while:
        - condition: device
          type: is_on
          device_id: dce41841c794af933df760081451cd69
          entity_id: ff220d51848d4c76660c7bb6d37da8f2
          domain: switch
      sequence:
        - if:
            - condition: numeric_state
              entity_id: sensor.samsung_tweek_battery_level
              below: 55
          then:
            - type: turn_on
              device_id: 17cb0643066fcf7479afa744337c0776
              entity_id: 89a79002a0b9c2bd8696cca7dfee4705
              domain: switch
        - if:
            - condition: numeric_state
              entity_id: sensor.samsung_tweek_battery_level
              above: 95
          then:
            - type: turn_off
              device_id: 17cb0643066fcf7479afa744337c0776
              entity_id: 89a79002a0b9c2bd8696cca7dfee4705
              domain: switch
        - delay:
            hours: 0
            minutes: 5
            seconds: 0
            milliseconds: 0
mode: restart
icon: mdi:cellphone-information

And made a automation, when my desk power switch is on he start the script and when is off he cut it.

sorry can’t reply faster I was block by the forum. Too many rep for the baby account

Yep all is on Home Assistant.

I have do a automation, when my desk power switch is on, they start the script.
I have do a conditionnal loop in the script with a timer. When the desk power switch is off he stop.

Thanks for all your help.
It’s make me in the good way to better understand script

1 Like

An endlessly looping script, is not the recommended way of solving the issue in Home Assistant. However, if you believe it’s the solution for you, so be it.

1 Like

Why not loop ?

What you’re doing is fine for your application.

1 Like

Because Home Assistant supports event-based monitoring which is more efficient than continuous polling (which is what your script is doing).

Then it’s possible for an automation to detect when the desk switch is turned on. It becomes one of several triggers the automation uses to control the phone charger’s switch.

Anyway, your script does what you want.


NOTE

On a completely different subject, I noticed that the script’s Device Action sets the value of entity_id to a long alphanumeric string (89a79002a0b9c2bd8696cca7dfee4705).

Home Assistant usually sets the value to a human-readable string like switch.chargeur_rom, like it’s shown in your previous post:

Do you know if you did anything differently to make it use 89a79002a0b9c2bd8696cca7dfee4705 instead of switch.chargeur_rom? For example, did you upgrade Home Assistant since your previous post a day ago?

I believe that’s the entity registry slug.

Seems to me like that’s something new because it has traditionally been a domain.object_id style of string.

FWIW, I first noticed it in another post where the user reported an error message indicating the value of the Device Action’s entity_id was not valid … despite the fact it (a long alphanumeric string) was auto-generated when the user created the automation via the UI. :thinking: :man_shrugging:

I instructed them to replace the Device Action with a Service Call and they reported it was successful.

It may be.

Yeah I saw that too. it seems to be a purposeful change and not a bug though. :man_shrugging: only time will tell