Help with writing script

I want to write two scripts in the same scripts.yaml file.
I wrote the first script and every things worked great.
Here is the first script:

ac_commands:
  alias: AC Commands
  sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/ACֹ_LivingRoom/IRsend
      payload_template: '{% if is_state(''input_select.ac_switch'',''False'') %} 0,8984,4500,644,564,640,564,644,1664,644,560,648,564,640,568,640,1664,644,560,648,
1660,648,564,644,560,644,1672,640,560,644,560,648,564,640,564,644,564,644,560,
644,560,648,564,640,560,648,1664,644,568,640,564,640,568,640,620,584,564,644,
564,644,1660,644,564,644,1668,640,564,644,560,648,1660,648,564,640,19996,648,1664,
644,564,644,568,640,564,644,564,640,560,648,564,640,564,644,564,640,564,644,568,
640,560,644,568,640,568,640,564,640,568,640,624,580,564,644,564,644,564,640,568,
640,568,640,564,644,564,640,564,644,564,640,568,640,564,644,1668,640,1664,644,1668,640,564,644
        {% elif is_state(''input_select.ac_switch'',''True'') and is_state(''input_select.ac_mode'',''Heat'')
        and states(''input_number.ac_temperature'')|int == 16 %} 0,8984,4500,616,592,616,592,612,1696,616,1692,612,596,612,592,640,1668,640,568, 
616,592,612,596,636,568,616,592,616,592,612,592,616,592,612,596,612,592,612, 
596,612,596,612,592,616,592,612,1696,616,1692,612,596,612,592,616,592,616, 
592,612,596,612,1696,612,592,616,1692,612,596,612,596,612,1696,612,592,616, 
20032,640,1668,640,568,612,592,616,592,616,592,612,596,612,592,616,592,616, 
592,612,592,616,592,612,596,612,592,616,592,616,592,612,596,612,592,616,592, 
616,592,612,592,616,592,612,596,612,596,612,592,616,592,612,596,612,592,616, 
592,616,592,612,1696,612,1696,612,596,612
        {% elif is_state(''input_select.ac_switch'',''True'') and is_state(''input_select.ac_mode'',''Heat'')
        and states(''input_number.ac_temperature'')|int == 17 %} 0,8984,4500,612,596,640,564,616,1692,616,1696,612,592,616,592,616,1692,640,568,616,1692,640,568,612,592,640,568,616,592,636,572,612,592,616,5$
        {% elif is_state(''input_select.ac_switch'',''True'') and is_state(''input_select.ac_mode'',''Heat'')
        and states(''input_number.ac_temperature'')|int == 18 %} 0,8984,4500,612,596,640,564,616,1692,616,1696,612,592,616,592,616,1692,640,568, 
616,1692,640,568,612,592,640,568,616,592,636,572,612,592,616,592,612,592,616, 
592,632,576,612,596,612,592,616,1692,640,1672,612,592,616,592,612,596,612,592, 
616,592,612,1696,612,596,612,1696,612,596,636,568,616,1692,612,596,616,20028,616, 
1696,612,592,616,592,612,592,616,592,616,592,612,596,636,568,616,592,612,592,616, 
592,616,588,644,564,616,592,612,596,612,604,604,592,616,592,612,592,616,592,612, 
592,640,568,616,592,612,592,616,592,616,592,640,576,604,592,616,1692,616,1696,612,1696,616,592,612
{% endif %}
     

Today I wanted to write another script that doing another thing, in the same file. So I tried to create a new script in the home assistant UI and edit it: (I don’t want to call a service in this script, I just want to check conditions in template and active an automation)

'1587080539465':
  alias: Rainy weather
  sequence:
  - data: {}
    service: cover.close_cover
    data_template:
            entity_id: >
               {% if is_state('sun.sun','above_horizon') and (states.sensor.time.state < "14:00") and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_bbvqr
               {% elif (states.sensor.time.state >= "14:00") and is_state('sun.sun','above_horizon') and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_btshryym
               {% elif is_state('sun.sun','below_horizon') and (states.sensor.time.state < "23:00") and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_khh_ts
               {% elif (states.sensor.time.state >= "23:00") and is_state('sun.sun','below_horizon') and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_blylh
               {% endif %}

Now, I have an error in the configuration check:

Invalid config for [script]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['script']['1587080539465']['sequence'][0]['data_template']['entity_id']. Got '{% if is_state(\'sun.sun\',\'above_horizon\') and (states.sensor.time.state < "14:00") and ((is_state(\'weather.byt\',\'sunny\') or is_state(\'weather.byt\',\'cloudy\') or is_state(\'weather.byt\',\'partlyclycloudy\')) %}\n    automation.ptykht_trysym_bbvqr\n{% elif (states.sensor.time.state >= "14:00") and is_state(\'sun.sun\',\'above_horizon\') and ((is_state(\'weather.byt\',\'sunny\') or is_state(\'weather.byt\',\'cloudy\') or is_state(\'weather.byt\',\'partlycloudy\')) %}\n    automation.... (See /home/homeassistant/.homeassistant/configuration.yaml, line 14). Please check the docs at https://home-assistant.io/components/script/

I don’t know what the problem. I want to write the two scripts in the same file

Thanks for your help.

Can you please read the sticky at the top of the forum, mainly about formatting your code so others can read it.

Sorry, I edited my code

For some reason half your code is getting truncated on the right side of all lines. You might have to copy from the text files

I fixed it, Thank you. The first script works good (AC Commands) but when I add the second script to the file it starting making problems.

Your second script miss some “)” at the end of every if and elif, try checking the brackets.

'1587080539465':
  alias: Rainy weather
  sequence:
  - data: {}
    service: cover.close_cover
    data_template:
            entity_id: >
               {% if is_state('sun.sun','above_horizon') and (states.sensor.time.state < "14:00") and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_bbvqr
               {% elif (states.sensor.time.state >= "14:00") and is_state('sun.sun','above_horizon') and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_btshryym
               {% elif is_state('sun.sun','below_horizon') and (states.sensor.time.state < "23:00") and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_khh_ts
               {% elif (states.sensor.time.state >= "23:00") and is_state('sun.sun','below_horizon') and ((is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_blylh
               {% endif %}

I will check this. How can I write the second script without calling a service?

The script allows you to specify a sequence of actions to be executed.
If your need to activate an automation you need to use the service automation.trigger.

Ok so instead of writing service: cover.close_cover I should write service: automation.trigger ?
The other things in the script are ok? Except the brackets.

Someone can help me?
It’s the updated script:

'1587221449188':
  alias: Open covers after rain
  sequence:
  - data:
      data_template:
        entity_id: "{% if is_state('sun.sun','above_horizon') and strptime((states.sensor.time.state),\"\
          %H:%M\") < strptime((\"14:00\"),\"%H:%M\") and (is_state('weather.byt','sunny')\
          \ or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy'))\
          \ %}\n    automation.ptykht_trysym_bbvqr\n{% elif strptime((states.sensor.time.state),\"\
          %H:%M\") >= strptime((\"14:00\"),\"%H:%M\") and is_state('sun.sun','above_horizon')\
          \ and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy')\
          \ or is_state('weather.byt','partlycloudy')) %}\n    automation.sgyrt_trysym_btshryym\n\
          {% elif is_state('sun.sun','below_horizon') and strptime((states.sensor.time.state),\"\
          %H:%M\") < strptime((\"23:00\"),\"%H:%M\") and (is_state('weather.byt','sunny')\
          \ or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy'))\
          \ %}\n    automation.ptykht_trysym_khh_ts\n{% elif strptime((states.sensor.time.state),\"\
          %H:%M\") >= strptime((\"23:00\"),\"%H:%M\") and strptime((states.sensor.time.state),\"\
          %H:%M\") <= strptime((\"23:59\"),\"%H:%M\") or strptime((states.sensor.time.state),\"\
          %H:%M\") > strptime((\"00:00\"),\"%H:%M\") and strptime((states.sensor.time.state),\"\
          %H:%M\") < strptime((\"06:07\"),\"%H:%M\") and (is_state('weather.byt','sunny')\
          \ or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy'))\
          \ %}\n    automation.sgyrt_trysym_blylh\n{% endif %}\n"
    service: automation.trigger

The configuration checking passed but when I try to run the script there is an error message: “failed to call service [‘data’][‘data_template’]”.

I don’t know how you can read that code block. At a glance it looks like you’re doing alot of strptimes. So i’d be willing to bet that it will cause problems when you get this working.

Your main issue is that you have data_template inside data. You have a template, so you should only have the data_template field, not the data field.

  alias: Open covers after rain
  sequence:
  - data_template:
      entity_id: "blah"
    service: automation.trigger

If you’re using the automation editor to create this, you have to switch it to yaml mode to change data to data_template because that field is only ‘data’.

It’s how I wrote the code:

 alias: Rainy weather
  sequence:
  - service: automation.trigger
    data:
      data_template:
         entity_id: >

{% if is_state('sun.sun','above_horizon') and strptime((states.sensor.time.state),"%H:%M") < strptime(("14:00"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_bbvqr
{% elif strptime((states.sensor.time.state),"%H:%M") >= strptime(("14:00"),"%H:%M") and is_state('sun.sun','above_horizon') and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_btshryym
{% elif is_state('sun.sun','below_horizon') and strptime((states.sensor.time.state),"%H:%M") < strptime(("23:00"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_khh_ts
{% elif strptime((states.sensor.time.state),"%H:%M") >= strptime(("23:00"),"%H:%M") and strptime((states.sensor.time.state),"%H:%M") <= strptime(("23:59"),"%H:%M") or strptime((states.sensor.time.state),"%H:%M") > strptime(("00:00"),"%H:%M") and strptime((states.sensor.time.state),"%H:%M") < strptime(("06:07"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_blylh
{% endif %}

but after I put it in the home assistant scripts editor it wrote it like what I sent before.
Now I don’t use the editor of home assistant but how can I switch it to yaml mode?

You don’t have to switch to yaml mode if you’re not using the editor. Remove the data: line…

 alias: Rainy weather
  sequence:
  - service: automation.trigger
    data_template:
       entity_id: >

{% if is_state('sun.sun','above_horizon') and strptime((states.sensor.time.state),"%H:%M") < strptime(("14:00"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_bbvqr
{% elif strptime((states.sensor.time.state),"%H:%M") >= strptime(("14:00"),"%H:%M") and is_state('sun.sun','above_horizon') and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_btshryym
{% elif is_state('sun.sun','below_horizon') and strptime((states.sensor.time.state),"%H:%M") < strptime(("23:00"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.ptykht_trysym_khh_ts
{% elif strptime((states.sensor.time.state),"%H:%M") >= strptime(("23:00"),"%H:%M") and strptime((states.sensor.time.state),"%H:%M") <= strptime(("23:59"),"%H:%M") or strptime((states.sensor.time.state),"%H:%M") > strptime(("00:00"),"%H:%M") and strptime((states.sensor.time.state),"%H:%M") < strptime(("06:07"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
                   automation.sgyrt_trysym_blylh
{% endif %}

It’s the error I get:

image

This is how I wrote it:

Indentation problem. The entire template should be moved to the right. It should start 2 characters to the right of where entity_id starts.

  - service: automation.trigger
    data_template:
       entity_id: >
         {% if is_state('sun.sun','above_horizon') and strptime((states.sensor.time.state),"%H:%M") < strptime(("14:00"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.ptykht_trysym_bbvqr
         {% elif strptime((states.sensor.time.state),"%H:%M") >= strptime(("14:00"),"%H:%M") and is_state('sun.sun','above_horizon') and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.sgyrt_trysym_btshryym
         {% elif is_state('sun.sun','below_horizon') and strptime((states.sensor.time.state),"%H:%M") < strptime(("23:00"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.ptykht_trysym_khh_ts
         {% elif strptime((states.sensor.time.state),"%H:%M") >= strptime(("23:00"),"%H:%M") and strptime((states.sensor.time.state),"%H:%M") <= strptime(("23:59"),"%H:%M") or strptime((states.sensor.time.state),"%H:%M") > strptime(("00:00"),"%H:%M") and strptime((states.sensor.time.state),"%H:%M") < strptime(("06:07"),"%H:%M") and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.sgyrt_trysym_blylh
         {% endif %}

Thank you! Everthing works great. I have some questions:

  1. Is there a difference between sprptime and states.sensor.time.state?
  2. Where can I see the rules of the language of the home assistant?
  3. In the last elif that I want to do a case that If the time is between 23:00 o’clock and until the sunrising in the morning, the covers will open according to the automation of the night. the problem is that after 23:59
    the time is 00:00 o’clock, 01:00 o’clock and etc. What I did will work in this times?

strptime turns it into a datetime object. You’re currently converting it into an incomplete datetime object. You don’t provide a date so things could resolve differently than you expect.

You should be fine using strings as long as you always provide leading zero’s in your comparison. I.e. don’t use "7:00" for 7am, use "07:00".

The home assistant documentation doesn’t cover the ‘rules’. For that you should be googling ‘Jinja’. I use this site.

You can do something like this:

{{ "23:00" <= states('sensor.time') <= "23:59" or "00:00" <= states('sensor.time') <= "06:07" }}

Thank you. I did something like what you do in the last elif. Is there a way to use above_horizon entity instead of writing the time in the end?

Another question: is brackets affect on the elif sentence? for example, if I want something that will happen before something else.

I tried to run the script again now but I noticed that it doesn’t matter what the time is, it activate the second elif always.

{% elif is_state('sun.sun','below_horizon') and states('sensor.time') < "23:00" and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.ptykht_trysym_khh_ts

it’s the full script:

'1587221449188':
  alias: Open covers after rain
  sequence:
  - service: automation.trigger
    data_template:
       entity_id: >
         {% if is_state('sun.sun','above_horizon') and states('sensor.time') < "14:00" and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.ptykht_trysym_bbvqr
         {% elif states('sensor.time') >= "14:00" and is_state('sun.sun','above_horizon') and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.sgyrt_trysym_btshryym
         {% elif is_state('sun.sun','below_horizon') and states('sensor.time') < "23:00" and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.ptykht_trysym_khh_ts
         {% elif states('sensor.time') >= "23:00" and states('sensor.time') <= "23:59" or states('sensor.time') >= "00:00" and is_state('sun.sun','below_horizon') and (is_state('weather.byt','sunny') or is_state('weather.byt','cloudy') or is_state('weather.byt','partlycloudy')) %}
               automation.sgyrt_trysym_blylh
         {% endif %}

What I am missing?

Someone can help me?