Script works when executed manuelly, only partly called from automation

Hi!

Disclaimer: this is my first time doing scripts and blueprints.

I have a script, that when executes, sets a scene based on a counter (like multiple clicks on a button) - once done it waits 5 seconds and then resets the counter.

This works perfectly when hitting the execute button, or when calling the script from the service dev tools.

But when called through the automation, only the part before the delay works. Its sets the scene, increments the counter, but never resets the counter.

I’ve already tried formatting the delay different, with 00:00:05 etc.
I realize that I have stuff that needs to be modified in my blueprint, but I don’t think its relevant since half the script execute.

Automation:

alias: ZHA - Bedroom Dimmer
description: ''
use_blueprint:
  path: porrekaj/zha-philips-hue-dimmer-switch.yaml
  input:
    remote: 196f7f803f2bea8323dc35fcdf69ca2c
    button_1_single_press:
      - service: script.zha_bedroom_dimmer
        data: {}
    light:
      entity_id: light.master_bedroom_lights
mode: restart

Script:

alias: ZHA Bedroom Dimmer
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: counter.zha_dimmer_bedroom
            state: '1'
        sequence:
          - service: script.lights_set_theme
            data:
              scene: nightlight
              light: light.master_bedroom_lights
            entity_id: light.master_bedroom_lights
          - service: counter.increment
            data: {}
            entity_id: counter.zha_dimmer_bedroom
      - conditions:
          - condition: state
            entity_id: counter.zha_dimmer_bedroom
            state: '2'
        sequence:
          - service: counter.increment
            data: {}
            entity_id: counter.zha_dimmer_bedroom
          - service: script.lights_set_theme
            data:
              scene: dimmed
              light: light.master_bedroom_lights
            entity_id: light.master_bedroom_lights
    default:
      - service: counter.reset
        data: {}
        entity_id: counter.zha_dimmer_bedroom
  - delay: '5'
  - service: counter.reset
    data: {}
    entity_id: counter.zha_dimmer_bedroom
mode: restart

In the script

Hmmm. … !
Maybe but use a delay to ensure your correct count

I’d put them in reverse order and maybe use queued

1 Like

I thought mode: restart was needed. because I need the counter to increment to set different scenes based on number of clicks, so it needs to run again before the reset.

So run the increment in the automation, you must allow each automation to complete. So queued.
Then call the script
The script can be restart with a 5 second delay at the start (so it only acts on the last call (in the last 5 secs))

The problem with that would be that you need to know what scene corresponds to what number of clicks, since you can’t cycle through them like my current automation in theory should do.
I was going for the same functionality like the philips hue switch has in the hue ecosystem, click to cycle through scenes, wait a bit of time to let it start over.

So why not have 4 scenes and just have a single click advance to the next ?

1 Like

Lets say I need to check on why my sons is crying at 3 am, and need the lowest light setting, which is the first click today.
If the last scene used was the one before ‘literal sun’ then I’d wager the crying won’t stop :sweat_smile:

I’m sure i’ll end up designing it differently eventually, as i’m not too confident that I can trust this, seeing that to me, it should work as it is now.

I should also point out that I was aiming at making this as expandability as posible, as I’m moving from Hue to ZHA, and I’d like to keep stuff as modular as possible to make it easier to maintain, and add devices.
I initially wanted as little logic in the automation as possible, and let the logic resides in scripts.

Are you going about this the right way?
What is your kid cries at 3am and 4am?
Why not set a scene depending on time?
If I push my bedside switch at 3am, just my lamp comes on at 1%. If I hold it a night scene comes on in the room.
If it’s daytime then a day scene comes on instead.

2 Likes

Are you going about this the right way?

Always a good question, doing time based for instance would require me to know every concievable reason for tuning on lights on all my 12 switches at any time of the day.

If my kid was throwing up at 2 AM I would like brighter and colder lights, and would probably not like to have to go back and find my phone to do so.

Too many variables. :slight_smile:

FYI, setting the automation to run single “fixes” the counter.reset issue. - of course it can’t cycle through scenes like this. But it is interesting that executing the script multiple timesthrough an automation breaks the script, but executing the script manually multiple times does not.

Possible bug.
Using Script.Turn_On instead, seems to work when using restart mode.
Needs more testing.

I would just bass the first click on time. Subsequent clicks could move to next scene.

Of course yes,
I intend to do stuff like that down the line for some switches - the “cycle through scenes” part would have been an issue there too though

NOT A BUG.
script.turn_on runs the script and waits for it to complete before continuing
service: script.my_script calls the script but imediately continues (running the script and the automation in parallel)
So they behave differently

As @samnewman86 points out you need to evaluate your needs and then you can decide how the logic works then how to implement it. TOO many times people decide they want to go down ‘this avenue’ but that avenue often does not take you where you want to go.

To be honest, why are you using scenes ? this seems like a small area with a limited number of affects/effects (intentional)

What are these scenes ? Controlling how many entities ? What is the ‘object’ (not physical, but the effect) of each scene ?

Stating the objective clearly and concisely gets you generally 70% of the way to a solution

2 Likes

I agree, but one gotta start somewhere to learn how it all works :slight_smile:

To be honest, why are you using scenes

Its not the built in scenes in HA, the terminology is just borrowed from Hue - “Preset” is more fitting.
All rooms will have some presets that are identical. “Nightlight”, “Dimmed”,“Normal” and “Bright”.
Instead of having to write actions for every preset in every room, I made a script that takes a ‘group of lights’ parameter, and a ‘preset’ parameter.

Today, on Hue, the On button cycles through the scenes, and after a brief pause the button resets so the next click would again set the first scene (nightlight on all my switches). and this is the functionality I’m trying to mimic.

Alot of the time in programming, you will end up reinventing the wheel, until you learn that a better solution exist :sweat_smile:
Thanks for answering!