Scripts w/ entity to stay on for certain time / or disabling automation for certain time (in use with Fibaro Keyfob)

I don’t know how to best script this, because my tries so far are failing miserably.

In our bath room I have a motion detector that is scripted to turn on/off lights after certain time. This works flawlessly. However, if someone wants to take a bath we don’t want the motion detector to control the lighting in the bathroom for a set time (30 minutes).

I thought it would be an easy fix setting up an input_boolean.bath. I have a blueprint / automation for our Fibaro Keyfob which is used in the bathroom. I made a scene for the bath light settings. I then set this up for one of the buttons:

      button_1_held:
      - service: scene.turn_on
        target:
          entity_id: scene.bathing
        metadata: {}
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.bath
      - delay:
          hours: 0
          minutes: 30
          seconds: 0
          milliseconds: 0
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.bath

And then in the motion detection automation I included input_boolean.bath to block it from going on/off if the input_boolean.bath is on. Technically it works, the automatic lighting is blocked for 30 minutes.

However the problem is that Keyfob is completely unresponsive to any other presses until the delay timeframe (30 minutes) is up. I tried moving everything to a script and just have the blueprint / automation for Keyfob run the script and not have the actual delay in the automation itself, but it renders the same end result. The Keyfob is unresponsive for the duration of the delay.

So I figure there must be a better way of doing this. Thus disabling the motion sensor / automatic lights for 30 minutes with a button press and still be able to use the rest of the buttons.

And as a side question. If you run a script with a delay command and you then run another script in the timeframe, will the first script still trigger after the delay is up or will it get cancelled by the second script changing the entitty ahead of time?

Instead of your boolean and delay method use a timer helper. Getting rid of the use of delay and making sure the mode is set to one of the options other than the default single should help the unresponsiveness issue.

button_1_held:
  mode: restart
  sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.bathing
        metadata: {}
      - service: timer.start
        target:
          entity_id: timer.bathroom_light 
        duration: "00:30:00"

Then in your automation, include the timer as a trigger and condition as appropriate.
If you need help doing that, post the motion light automation you are currently using so we can integrate everything in one place.

This looks promising. However when I try editing with the above I get the following error message from the UI:

Message malformed: Unable to determine action @ data['action][1]['choose'][0]['sequence'][0]

I don’t really understand what is wrong

The only way I could get the UI to accept the code was with it like below. This is part of a remote with lots of buttons. Should the mode: restart be for them all or specifically the button with the timer?

alias: Fibaro KeyFob Bad
description: ""
use_blueprint:
  path: jaxonstagecrew/fibaro-keyfob-fgkf601.yaml
  input:
    keyfob_device: bc5dcf3f2bcf11537ff16246423ea812
    button_1_pressed: []
    mode: restart
    sequence:
      - service: scene.turn_on
        target:
          entity_id: scene.badbada
        metadata: {}
      - service: timer.start
        target:
          entity_id: timer.bad_light
        duration: "00:45:00"
    button_3_pressed:
      - service: media_player.media_stop
        data: {}
        target:
          entity_id: media_player.bad

Now that you have posted the blueprint you are using we can actual see how things are supposed to work together… and what’s possible within the framework. The mode of the blueprint is set to single. To make that work and avoid the unresponsiveness, you need to avoid delay or wait within the actions. The timer method should work fine. If you find you need a sequence that includes a wait move it to a standalone script. The reason you had issues with that method previously is that you likely didn’t use the correct method to call the script and avoid waiting for it to complete.

alias: Fibaro KeyFob Bad
description: ""
use_blueprint:
  path: jaxonstagecrew/fibaro-keyfob-fgkf601.yaml
  input:
    keyfob_device: bc5dcf3f2bcf11537ff16246423ea812
    button_1_pressed:
      - service: scene.turn_on
        target:
          entity_id: scene.badbada
        metadata: {}
      - service: timer.start
        target:
          entity_id: timer.bad_light
        duration: "00:45:00"
    button_3_pressed:
      - service: media_player.media_stop
        data: {}
        target:
          entity_id: media_player.bad
    .....

Gonna try this in just a bit. Before I start, how can you determine that the blueprint mode is “single” ? Or is this the default if not defined?

Would it be better changing the whole blueprint to “restart”. Can’t really see why or what is better with single compared to restart? There are no advanced actions. The scences include two light entities. Other buttons are just controlling mediaplayer (sonos).

Also a second question now that came into mind now that you introduced the timer function.

The motion detection for the bathroom is split in four automations, two for on (one daytime and one nighttime) and two for off (also different timing for day and night).

The off scripts trigger is just when motion sensor goes to ‘off’ for 5 minutes (listed below).
Would it be better/cleaner just to have one script for ON and using the timer function as well so the lights go auto off after timer? Instead of separating on and off to different automations? Any certain pros/cons?

  trigger:
  - platform: state
    entity_id: binary_sensor.bad_motion
    to: 'off'
    for:
      hours: 0
      minutes: 5
      seconds: 0

The default mode is single, but it is explicitly defined on line 101 of the blueprint.

If your actions are short and you don’t normally press buttons repeatedly (like you might to change volume, skip tracks, or change brightness) or in sequence, it should be fine in “restart”, otherwise “queued” might be a better option.

You will need a timer event trigger and related conditions to choose when turning off is appropriate.

It is best for you to set it up in whatever way makes sense to you. Some people like lots of small automations and scripts, others prefer to have more complicated automations that cover every aspect of how a single device operates…

You are one step ahead of me. Didn’t realize you looked at the whole blueprint, thought you distinguished it from my cut out bit of code :slight_smile:

Single seems like the better choice then since volume presses can be frequent presses.

The LIGHT ON automations are simple enough. Adding this condition should keep it from changing any of the lights as long as the timer is on:

    condition:
      condition: state
      entity_id: timer.bathroom_light
      state: 'idle'

However little bit more unsecure on the OFF automations. If I add the condition above (changed to active), then there might be situations where there won’t be a new trigger once the timer is off and the lights will stay on indefintitely.
My best solution to this would be to add even more automation with the finished timer as trigger and action is a on and off toggle of the motion sensor. That way the lights will go off if no motion for an additional five minutes? I am sure there is a better way of doing the off automations? :slight_smile:

Is there any latency issue or stability with split automations or everything in one?

Played around all night and day and think I got everything working like I want now.
Ended up cleaning up all the automations and making it into one big one using trigger ids.

Everything seems to be working. Different lighting depending on sun state. Different off depending on time of day. And when triggering bath script timer “pauses” light automation until finished then it kicks back in again.

BIG thanks for your help @Didgeridrew , couldn’t have done it without you!

alias: Badrumsljus
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bad_motion
    id: motion-bad
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.bad_motion
    id: stilla-bad-dag
    to: "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.bad_motion
    id: stilla-bad-night
    to: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - platform: state
    entity_id:
      - timer.bad
    id: Timer-bad-slut
    from: active
    to: idle
condition:
  - condition: state
    entity_id: timer.bad
    state: idle
    for:
      hours: 0
      minutes: 0
      seconds: 0
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion-bad
          - condition: numeric_state
            entity_id: sun.sun
            above: -5
            attribute: elevation
        sequence:
          - service: light.turn_on
            data:
              brightness: 200
            target:
              entity_id:
                - light.bad_spot
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.bad_spegellampa
      - conditions:
          - condition: trigger
            id: stilla-bad-dag
          - condition: time
            after: "06:00:00"
            before: "23:59:59"
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
              - sat
              - sun
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.bad_spegellampa
                - light.bad_spot
      - conditions:
          - condition: trigger
            id: motion-bad
          - condition: numeric_state
            entity_id: sun.sun
            attribute: elevation
            below: -5
        sequence:
          - service: light.turn_on
            data:
              brightness: 20
            target:
              entity_id: light.bad_spot
      - conditions:
          - condition: trigger
            id: stilla-bad-night
          - condition: time
            after: "00:00:00"
            before: "05:59:59"
            weekday:
              - mon
              - tue
              - wed
              - thu
              - sat
              - fri
              - sun
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.bad_spegellampa
                - light.bad_spot
      - conditions:
          - condition: trigger
            id: Timer-bad-slut
          - condition: state
            entity_id: binary_sensor.bad_motion
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 0
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.bad_spegellampa
                - light.bad_spot
mode: single