Script - Loop - Review my code

Hi,
I’m pretty new to Scripting in HASS.
I think I have a problem with my script. I try to automate a power switch ON when my phone drop down 55% battery and power switch OFF when above 95%

I write this :

alias: Chargeur Téléphone Bureau Tweek
sequence:
  - if:
      - condition: numeric_state
        entity_id: sensor.samsung_tweek_battery_level
        below: 55
    then:
      - type: turn_on
        device_id: 17cb0643066fcf7479afa744337c0776
        entity_id: switch.chargeur_rom
        domain: switch
  - if:
      - condition: numeric_state
        entity_id: sensor.samsung_tweek_battery_level
        above: 95
    then:
      - type: turn_off
        device_id: 17cb0643066fcf7479afa744337c0776
        entity_id: switch.chargeur_rom
        domain: switch
mode: restart
icon: mdi:cellphone-information

When I run it (with battery phone at 38%) he put the power switch ON.
But if I look in information, I did see the script restart.
Capture d'écran 2023-07-12 131113

Can you help me ?

Is there a reason you chose to create a script instead of an automation?

Or is this script called by an automation?

Yes.
The power switch is in my desk and all my desk is in an other switch who power on when my computer is awake.
Somes times, I power my desk and my phone is above this 55% and automation not start.

I think was @123 is saying is that a script will never execute on its own… it has no triggers.
You can call that script from an automation, or move the script code wholy into an automation and do it that way.

Post the automation.

For ON

alias: Samsung Tweek -55%
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: d3ab40160402390cc6d13aff164d456c
    entity_id: sensor.samsung_tweek_battery_level
    domain: sensor
    below: 55
condition: []
action:
  - type: turn_on
    device_id: 17cb0643066fcf7479afa744337c0776
    entity_id: switch.chargeur_rom
    domain: switch
mode: single

For OFF

alias: Samsung Tweek +95%
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: d3ab40160402390cc6d13aff164d456c
    entity_id: sensor.samsung_tweek_battery_level
    domain: sensor
    above: 95
condition: []
action:
  - type: turn_off
    device_id: 17cb0643066fcf7479afa744337c0776
    entity_id: switch.chargeur_rom
    domain: switch
mode: single

I guess a script loop ever when is lunch once, no ?

In order for the script to restart, you would have to run the script a second time while the script is still executing the first time.

The chance of this happening seems to be very rare because your script only turns a switch on or off which takes less than one second.

What is the evidence that indicates the script restarted? Is it appearing in the script’s traces?

I have no log, and sems to be off.

The second screenshot shows the script started/ran/ended at 13:02:10. In other words, it executed once and completed in under a second.

You should check the script’s traces to examine its operation. Traces are produced for automations and scripts.

This ?


It’s look normal

Exactly; it did not restart.

You can double-check by comparing this trace with the previous trace.

Click the arrow on the left side of 12 juillet 2023 à 14:36:42 to see the previous trace.

  • If the time of the previous trace is almost the same time as 14:36:42 then the script restarted.
  • If the time is not close to 14:36:42 then it did not restart.

I have nothing.
Strange because, in code I have this

No, scripts are only ran when you press the button.

1 Like

It’s possible that you have misunderstood the meaning of mode: restart. All of the modes are explained here:

The mode you selected (restart) means that if you run the script and then try to run it again while the script is still running, it will stop the running script and run it again from the beginning.

There’s almost no chance of that happening for your script because it executes in less than a second.

From the documentation:

mode string (optional, default: single)
Controls what happens when script is invoked while it is still running from one or more previous invocations. See Script Modes.

restart Start a new run after first stopping previous run.

1 Like

Yep I have read this. But still understanding

Hummm. How can I run it like a service ?

That’s an automation, not a script.

But it’s a script