Check action taken was successful- before sending any notification

Hi

I’m still getting to grips with Automation, and I’m struggling to comprehend how you could check that an action has been completed before a confirmation notification is sent ?

  - alias: 'Power Off, Light On'
    trigger:
      platform: time
      after: "10:25:00"
    action:
      - service: notify.pushbullet
        data:
          message: 'Turning multiblock power!'
          title: 'Home Assistant'
      - service: switch.turn_off
        entity_id: switch.living_room_multiblock
      - service: notify.pushbullet
        data:
          message: 'Turning ON Varantha Light!'
          title: 'Home Assistant'
      - service: switch.turn_on
        entity_id: switch.kitchen```

How would you structure this code so that it checks the action had been completed successfully first, before the notification is sent?

Hi @nodecentral, maybe put your notifys in scripts. After turning switch on or off, call script with condition switch on or off and push the notify.

You always can use like binary sensor with your action and while it running you can set that sensor to “running” and check in your automation that sensor state. after your action finished you should updated the sensor state to something like “not running”/“stopped”.

Hi @VDRainer

Do you have an example of such a script and how that would work?

OK, here is some example with on of your switches
Turn your switch, then call the script.

#automation 4:
- alias: 'Power Off'
  trigger:
    platform: time
    after: "10:25:00"
  action:
    - service: switch.turn_off
      entity_id: switch.living_room_multiblock
    - service: homeassistant.turn_on
      entity_id: script.check_switch

The script checks that the switch is off, then pushes the notify. If the switch is not off, it exits, so no notify.

#script:
check_switch:
  sequence:
    # wait a few second, maybe switch needs some time
    - delay: '00:00:03'
    # if switch state 'off'
    - condition: state
      entity_id: switch.living_room_multiblock
      state: 'off'
    # push notify
    - service: notify.pushbullet
      data:
        message: 'Turning multiblock power!'
        title: 'Home Assistant'

For your second switch you need another script, or you can check both switches with an additional condition in the script.

Not testet, but i think this could work.

Thanks so much for that @VDRainer

Would you be able to help enhance the script so that if the Multiblock fails to power off that, it sends a notification to say it was not turned off, and then maybe waits a bit longer so it can check it again (just in case there is a delay) - the notification message can say it will try again in e.g 1 minute?)

In this particular set up/example it is very important that the power is turned off to this multiblock - so I’m keen for it to check it is successful and try again if needed, ultimately going to the end where is would send me a warning notification to say something like ‘Home Assistant’ was unable to turn XYZ off, please take alternative action"

I’ve tried to have two ‘Scripts’ in my configuration.yaml, but I’m getting an error in the log “expected a dictionary for dictionary value @ data” etc.

I have put the lines via YAML validates amd they come back and tell me it is correct ?

#script:

script:

  • check_switch_off:
    sequence:

    wait a few second, maybe switch needs some time

    - delay: '00:00:03'
    

    check if switch state ‘off’

    - condition: state
      entity_id: switch.living_room_multiblock_229
      state: 'off'
    

    push notify

    - service: notify.pushbullet
      data:
        message: 'CONFIRMED, The Lounge Multiblock has been turned off'
        title: 'Home Assistant'
    
  • check_switch_on:
    sequence:

    wait a few second, maybe switch needs some time

    - delay: '00:00:03'
    

    check if switch state ‘on’

    - condition: state
      entity_id: switch.living_room_multiblock_229
      state: 'on'
    

    push notify

    - service: notify.pushbullet
      data:
        message: 'CONFIRMED, The Lounge Multiblock has been turned on'
        title: 'Home Assistant'

The exact error is as follows…

17-04-21 19:10:47 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: expected a dictionary for dictionary value @ data[‘script’]. Got [OrderedDict([(‘check_switch_off’, OrderedDict([(‘sequence’, [OrderedDict([(‘delay’, ‘00:00:03’)]), OrderedDict([(‘condition’, ‘state’), (‘entity_id’, ‘switch.living_room_multiblock_229’), (‘state’, ‘off’)]), OrderedDict([(‘service’, ‘notify.pushbullet’), (‘data’, OrderedDict([(‘message’, ‘CONFIRMED, The Lounge Multiblock has been turned off’), (‘title’, ‘Home Assistant’)]))])])]))]), OrderedDict([(‘check_switch_on’, OrderedDict([(‘sequence’, [OrderedDict([(‘delay’, ‘00:00:03’)]), OrderedDict… (See /home/homeassistant/.homeassistant/configuration.yaml, line 187). Please check the docs at https://home-assistant.io/components/script/
17-04-21 19:10:47 ERROR (MainThread) [homeassistant.setup] Setup failed for script: Invalid config.

The solution to your problem is in the error message. Please check the docs at https://home-assistant.io/components/script/ .
Here you will see that scripts don’t need hyphens in front of them.

script:
  check_switch_off:
    sequence:
      ...

  check_switch_on:
    sequence:
      ...

And please read the blue box on top of every page here. :slight_smile:

Many Thanks @VDRainer

I have ro admit , I read and re-read the link / blue box at the top many times and I still can’t work out how I should post configuration text (I’m just not getting it) :frowning:

As for you suggestions about fixing my script error - where you say I “don’t need hyphens in front of them”

I’m confused there, as I had just copied what you had help me with earlier in this thread and that example had used hyphens ?

I think I may have made some progress with the formatting thing, but still need to work out how to indent the lines … :slight_smile:

script:
  - check_switch_off:
      sequence:
      # wait a few second, maybe switch needs some time
        - delay: '00:00:03'
      # check if switch state 'off'
        - condition: state
          entity_id: switch.living_room_multiblock_229
          state: 'off'
      # push notify
        - service: notify.pushbullet
          data:
            message: 'CONFIRMED, The Lounge Multiblock has been turned off'
            title: 'Home Assistant'
  - check_switch_on:
      sequence:
      # wait a few second, maybe switch needs some time
        - delay: '00:00:03'
      # check if switch state 'on'
        - condition: state
          entity_id: switch.living_room_multiblock_229
          state: 'on'
      # push notify
        - service: notify.pushbullet
          data:
            message: 'CONFIRMED, The Lounge Multiblock has been turned on'
            title: 'Home Assistant' ```

Well, let’s give it one last try. :mask:

For the indentation, just type three backticks, paste yor code, and close it with three backticks.
You can copy and paste the next three lines in your next post.
```
paste code here
```

Now the automation an scripts.

# automation aliases have a hyphen in front of it
automation:
  - alias: 'Power ON'
    trigger:
      platform: time
      ....
    action:
      - service: switch.turn_on
        ...

 - alias: 'Power OFF'
    trigger
      platform: time
      ...
    action:
      - service: switch.turn_off
        ...

# scripts names have no hyphens in front of it
script:
  check_switch_off:
    sequence:
      # wait a few second, maybe switch needs some time
      - delay: '00:00:03'
        ...

  check_switch_on:
    sequence:
      # wait a few second, maybe switch needs some time
      - delay: '00:00:03'
        ...

My first example looked i little different because i have the automations/scripts in included files, but the system is the same.

Many thanks again for that, I think I have it working now.

One more quick thing, as the script names are added to the home screen, can you have an alias for a script name?

E.g so rather than “check_switch_off” it can shows something else like “Living room Multiblock Power”?