IF/THEN in script data field

I’m not sure why this script (in script.yaml) doesn’t work. It is supposed to turn the air conditioner on or off, depending on the current state, but won’t turn it on or off (so neither the if or else part is working):

ac_mode_change:
  alias: ac mode change
  sequence:
  - data:
      data: >
        {% if is_state('sensor.ac_status', 'off') %}
          '{"SystemON":"on"}'
        {% else %}
          '{"SystemON":"off"}'
        {% endif %}
      url: http://192.168.1.192/SystemON
    service: shell_command.ac_command

I know that the following works:

ac_mode_change:
  alias: ac mode change
  sequence:
  - data:
      data: '{"SystemON":"on"}'
      url: http://192.168.1.192/SystemON
    service: shell_command.ac_command

So, I’m assuming that my if command is broken, or I can’t include an if command in that location. However, I’m not getting any errors.

The sensor code is:

- platform: rest
  name: AC Status
  resource: http://192.168.1.192/SystemSettings
  method: GET
  verify_ssl: false
  value_template: '{{ value_json.SysOn }}'
  # Options - on, off

Does anyone know what is wrong?

Thanks…

use data_template. Not data.

data_template: >
        {% if is_state('sensor.ac_status', 'off') %}
          '{"SystemON":"on"}'
        {% else %}
          '{"SystemON":"off"}'
        {% endif %}
      url: http://192.168.1.192/SystemON
    service: shell_command.ac_command

this should work:

ac_mode_change:
  alias: ac mode change
  sequence:
  - data_template:
      data: >
        {% if is_state('sensor.ac_status', 'off') %}
          '{"SystemON":"on"}'
        {% else %}
          '{"SystemON":"off"}'
        {% endif %}
      url: http://192.168.1.192/SystemON
    service: shell_command.ac_command

and if that doesn’t, this may work as well

ac_mode_change:
  alias: ac mode change
  sequence:
  - data_template:
      data:
        SystemON: >
          {% if is_state('sensor.ac_status', 'off') %}
            on
          {% else %}
            off
          {% endif %}
      url: http://192.168.1.192/SystemON
    service: shell_command.ac_command

as @tom_l said, your data section needs to be a data_template, but don’t get that confused with the data section that you need to send to your command. Also, if this is expecting a JSON object, you need to use the 2nd example as the first example only returns a string representation of a JSON object where the second will be an actual json object.

Thanks for the feedback guys. I probably should have included my shell command for context:

ac_command: "curl --data '{{ data }}' {{ url }}"

It doesn’t need an actual json object, just the string. Neither of those suggestions are working out of the box, but it is after midnight, so I’ll look at them properly in the morning. I appreciate your help and I’ll let you know how I get on tomorrow having fixed the data_template issue, that was clearly a problem.

I don’t suppose there is a linux command to monitor what the curl/shell statement being produced is? It would be easier to debug if I could see the command? Or is there a log file that I can log it to somehow?

Cheers,
Matthew

so your successful curl command would look like this without templates?

curl --data '{"SystemON":"on"}' http://192.168.1.192/SystemON

If not, post what it should look like

I.E if it should look like this:

curl --data '{SystemON:on}' http://192.168.1.192/SystemON

or

curl --data 'on' http://192.168.1.192/SystemON

I solved it. Thanks for your help. it was the single quotes within the if statement, they needed removal, more like your second option (which I’ll revisit to see why I couldn’t get to work last night).

The command line works with:

curl --data '{"SystemON":"on"}' http://192.168.1.192/SystemON

And, the version in my original post without the if statement works with the single quotes, but they need to come out for the if statement.

My script is now working like:

ac_mode_change:
  alias: ac mode change
  sequence:
  - data_template:
      data: >
        {% if is_state('sensor.ac_status', 'off') %}
          {"SystemON":"on"}
        {% else %}
          {"SystemON":"off"}
        {% endif %}
      url: http://192.168.1.192/SystemON
    service: shell_command.ac_command

Thanks so much for reaching out to help.

1 Like