Can't get very simple REST command to work

I just want to check if a web server is working and if it is down, send an e-mail.

There are many helpful posts related to using REST but it often isn’t clear where the pieces go.

This is in configuration.yaml:

rest_command:
  check_dbsk:
    url: "http://dadler.dynalias.org:81/dbsk"
    method: GET

I put this in automation.yaml based on an example I found:

- id: '1731083834226'
  alias: Test DBSK server0
  description: ''
  triggers: []
  conditions: []
  actions:
  - action: rest_command.check_dbsk
    response_variable: dbsk_response
  - if: "{{ dbsk_response['status'] == 404"
    then:
    - action: notify.dwa12457_gmail_com
      metadata: {}
      data:
        message: server failed
        title: Checking server
        target: [email protected]
    else:
    - action: notify.dwa12457_gmail_com
      metadata: {}
      data:
        message: server ok
        title: Checking server
        target: [email protected]    
  mode: single

Nothing fails when I run this from Developer tools but it doesn’t succeed. Is there any place to look Developer tools for what happened? If I go into Automations I see:

I tried to create an automation but it wasn’t obvious how to do it.

Then I created this script:

alias: script1
sequence:
  - action: rest_command.check_dbsk
    data: {}
    response_variable: dbsk_response
  - condition: template
    value_template: "{{dbsk_response['status']}} == 200"
  - action: notify.dwa12457_gmail_com
    metadata: {}
    data:
      message: Server operational
      title: Server test
      target: [email protected]
description: ""

It fails.

It seems like this should be a trivial exercise but clearly I don’t know what I am doing!

The main issue in both is that the comparison needs to take place inside the expression:

- if:
    - condition: template
      value_template: "{{dbsk_response['status'] == 404}}"
  then:

or

  - condition: template
    value_template: "{{dbsk_response['status'] == 200}}"

But, the automation also doesn’t have a trigger, which is known to cause issues… If you don’t need or want a fixed trigger; use a script, not an automation.

- id: '1731083834226'
  alias: Test DBSK server0
  description: ''
  triggers: 
    - trigger: time_pattern
      hours: /2
  conditions: []
  actions:
    - action: rest_command.check_dbsk
      response_variable: dbsk_response
    - action: notify.dwa12457_gmail_com
      metadata: {}
      data:
        message: server {{ 'failed' if dbsk_response['status'] == 404 else 'ok' }}
        title: Checking server
        target: [email protected]
  mode: single

Thank you for the quick response but I must be doing something stupid in my script:

alias: script1
sequence:
  - action: rest_command.check_dbsk
    data: {}
    response_variable: dbsk_response
  - condition: template
    value_template: "{{dbsk_response['status'] == 200}}"
  - action: notify.dwa12457_gmail_com
    metadata: {}
    data:
      message: Status {{dbsk_response['status']}}
      title: Server test
      target: [email protected]
description: ""

If I run it without the condition, it sends the message with the code 200. With the condition it fails.
Sorry for being so dense…

I tried you example automation which runs fine. Not sure why my script doesn’t.

The script looks fine and is working for me as-is. Maybe add a short delay after the rest command to insure the response is received and the variable fully populated before it is checked in the condition…?