Need conditional action with notification in a script

HI,

In one of my scripts for a scene, I want to switch off a switch under the condition the iMac on that switch is Off, and if not, signal me to switch it Off, and continue. A bit puzzled how to achieve that though…
Think I need to do that in a sub script, but please have a look with me and add to it:

script:
  imac_off:
    alias: iMac off
    sequence:
     - condition: template
        value_template: >
          {{is_state('device_tracker.imac','not_home') and
            is_state('device_tracker.imac_wifi','not_home')}}
      - condition: template
        value_template: >
          {{is_state('switch.sw_netwerk_dorm_cl','on')}}
      - service: switch.turn_off
        entity_id: switch.sw_netwerk_dorm_cl

obviously I need to add the bit in case the first template condition is false, but am not sure if I should use a service_template ?
Maybe use a wait template, but then how to add the notification to switch off the switch, and wait for that to happen…

please have a look, thanks!

if a condition is not true, the script will stop.
I think what you need is a service_template, though I can’t remember if you can set the entity_id programmatically

Your scenario makes no sense.

If it is on, switch it off - else send me a message to switch it off.

It’s already off, why would you need a message?

Also, you are vastly overcomplicating your code there. There is no need to check that something is on before switching it off, and there’s certainly no need for templates to track simple states, that’s what a state condition is for.

I might have expressed myself incorrectly but the scenario is:

if it (iMac) is off, no message, simply switch off the switch (not the iMac)
if it (iMac) is on, don’t switch off yet, send a message to turn off the iMac, wait for that to happen and if switched off, turn off the switch.

Im good with the first if, can translate that to a service_template. the second part is a bit more complex, and would hope you could give me some pointers for that.

script:
  imac_check:
    alias: iMac check
    sequence:
     - service_template: >
        {% if is_state('device_tracker.imac' , 'not_home') and
            is_state('device_tracker.imac_wifi' , 'not_home') %}
          script.imac_off
        {% else %} script.prompt_imac {% endif %} 

  prompt_imac:
    alias: Prompt iMac
    sequence:
      - service: notify.YOU
        data:
          message: "Switch off the iMac" 
      - wait_template: >
          {{ is_state('device_tracker.imac' , 'not_home') and
            is_state('device_tracker.imac_wifi' , 'not_home') }}
      - service: script.imac_off

  imac_off:
    alias: iMac off
    sequence:
      - service: switch.turn_off
        entity_id: switch.sw_netwerk_dorm_cl

*formatting might be a bit out, done in a hurry on my phone.

yes!
that what I was looking for, thank you very much. I knew I needed an extra script, but was fuzzy about where and how.
Thanks, will test and let you know :wink:

just fyi, the switch has more than the iMac attached to it, so it needs to be powered off, but obviously have my worries simply cutting power to a powered iMac… .

I’ll rename the imac_off script accordingly, but that’s of no consequence for this scenario, cool. thanks again.

btw, im deeply impressed… how on earth do you do this on your phone??

result for now:

##########################################################################################
# a few subScripts to use in scripts for activity and presence automations
##########################################################################################

netwerk_dorm_off_check:
  alias: Netwerk Dorm off check
  sequence:
   - service_template: >
      {% if is_state('device_tracker.imac' , 'not_home') and
            is_state('device_tracker.imac_wifi' , 'not_home') %}
        script.netwerk_dorm_off
      {% else %} script.prompt_imac
      {% endif %}

prompt_imac:
  alias: Prompt iMac
  sequence:
    - service: notify.notify
      data:
        message: 'Switch off the iMac'
    - wait_template: >
        {{ is_state('device_tracker.imac' , 'not_home') and
           is_state('device_tracker.imac_wifi' , 'not_home') }}
    - service: script.netwerk_dorm_off
    - condition: template
      value_template: >
        {{ is_state('input_boolean.notify_system', 'on')}}
    - service: notify.notify
      data:
        message: 'Switched off the iMac, continuing...'

netwerk_dorm_off:
  alias: Netwerk Dorm off
  sequence:
    - service: switch.turn_off
      entity_id: switch.sw_netwerk_dorm_cl
    - condition: template
      value_template: >
        {{ is_state('input_boolean.notify_system', 'on')}}
    - service: notify.notify
      data:
        message: 'Netwerk Dorm switched off'
1 Like

With a lot of patience and a disclaimer :wink:

Hope it all works as expected :+1:

yess!
My first test was just run successfully, with all messages in place (always use the message to be sure each part is in order, and after testing turnoff the boolean, not to be bothered again)

Since this is part of the larger bedtime- scenario , I will have complete and hopefully successful feedback in the morning :wink:

Now trying to force an iOS-actionable notification in there, which would allow me to override and switch-off regardless.

1 Like

let me know if you need help with that. Done a few of them now that I should be able to assist :slight_smile:

ha yes, please.
I have several of these iOS notifications, but in this case the challenge is where to put it. I want the above script to run, but if it would take too long, lets say over 3 minutes, I’d want an actionable notification to switch Off regardless.

I’d be able to use my all-in-one ios.notification_action_fired automation, but would need some thinking on the notification. It should be positioned after the wait_template I guess? Maybe have a time-out there, and use the continue_on_timeout: option. But how to combine that with the actionable notification…
I could do this with the timeout: set to true and continue, but then I wouldn’t have the option. Don’t want the automation to take control.

I normally use a wait_template for such scenario

getting back to this… for the silliest thing yet… I can get the notification in the final script to work. No matter what I try, the

    - service: notify.notify
      data:
        message: 'Netwerk Dorm switched off'

won’t show. All other notifications work. the first service in that final script works fine:

    - service: switch.turn_off
      entity_id: switch.sw_netwerk_dorm_cl

and the input_boolean is in fact on. Without that, the other notifications would not show either. if I try the notification in the dev-service:

38

it runs fine.

the script.netwerk_dorm_off is called alright, since I get the notification from the prompt_imac script (which is performed after calling the netwerk_dorm_off script)
How.can.this.be??

while:

03

Update

never mind the last cry for help, found it myself… had another script with the exact same name, which I had forgotten about. renamed the script here, and all is notifying as desired :wink:

it surprises me no script _2 was created, or some kind of error or warning was issued though.