My first script with variables

Could someone help me writing my first script with variables ?

I want a script turning on a certain aircon with a certain temperature.

I create a button

type: button
tap_action:
  action: call-service
  service: script.switch_airco_x_op_y_graden
  service_data: {}
  target: {}
name: Airco x op y graden

The script with fix aircon and fix temperature

alias: 'Switch : Airco x op y graden'
sequence:
  - device_id: 67455609e3c9d29e44adb86b16e0e855
    domain: climate
    entity_id: climate.airco_gang
    type: set_hvac_mode
    hvac_mode: heat
  - service: climate.set_temperature
    data:
      temperature: 20
    target:
      entity_id: climate.airco_gang
mode: single

OK, I start with the temperature. I modify the script as this

I save it and go back to visual editor…and it’s gone ?

Back in Yaml

So, that’s my first problem with this script…

Occasionally there are issues like this when switching between the visual editor and the yaml editor. However, what you created in the yaml editor was not a valid entry because you didn’t define your temperature variable and templates need to be enclosed in quotes or a multi-line quote symbol (> or |).

alias: 'Switch: Airco x op graden'
variables:
  temperature: '{{ states("input_number.desired_temp")}}'
sequence:
  - device_id: 67455609e3c9d29e44adb86b16e0e855
    domain: climate
    entity_id: climate.airco_gang
    type: set_hvac_mode
    hvac_mode: heat  
  - service: climate.set_temperature
    data:
      temperature: '{{ temperature }}'
    target:
      entity_id: climate.airco_gang
mode: single
1 Like

Thanks.
But why this ‘{{ states(“input_number.desired_temp”)}}’ ?

I want to call the script from a button, where the temperature is mentioned

type: button
tap_action:
  action: call-service
  service: script.switch_airco_x_op_y_graden
  service_data:
    temperature: 20
  target: {}
name: Airco x op y graden

Now I see it, it’s the default value used if ommited.

Next…

alias: 'Switch : Airco x op y graden'
variables:
  temperature: '25'
  entity_id: 'climate.airco_gang'
sequence:
  - device_id: 67455609e3c9d29e44adb86b16e0e855
    domain: climate
    entity_id: '{{ entity_id }}'
    type: set_hvac_mode
    hvac_mode: heat
  - service: climate.set_temperature
    data:
      temperature: '{{ temperature }}'
    target:
      entity_id: '{{ entity_id }}'
mode: single

I get the error Message malformed: Entity ID {{ entity_id }} is an invalid entity ID for dictionary value @ data[‘entity_id’]

And what with the device_id ?

Sorry, about that… I misunderstood what you were trying to do.

When creating scripts that accept variables, it is usually a good idea to set up the fields to make it easy to test using the Services tool.

If you want to be able to use different entity_ids (as you set it up in your most recent example), you should not use a device action, instead use a call service so you don’t have to worry about all the device variables.

alias: 'Switch : Airco x op y graden'
description: ' Set airco mode to heat and set temperature'
fields:
  temperature:
    description: "The temperature to set the airco at"
    default: 25
    example: "21.2"
  entity_id:
    description: "The climate entity you want to control"
    default: "climate.airco_gang"
    example: "climate.airco_gang"
sequence:
  - service: climate.set_temperature
    target:
      entity_id: "{{ entity_id }}"
    data:
      temperature: "{{ temperature }}"
      hvac_mode: heat
mode: single

Thank you very much, I’m beginning to understand the concept :+1:

type: button
tap_action:
  action: call-service
  service: script.switch_airco_x_op_y_graden
  service_data:
    temperature: '20'
    entity_id: climate.airco_gang
  target: {}
name: Airco x op y graden

alias: 'Switch : Airco x op y graden'
description: ' Set airco mode to heat and set temperature'
variables:
  temperature:
    description: The temperature to set the airco at
    default: '25'
    example: '21'
  entity_id:
    description: The climate entity you want to control
    default: climate.airco_gang
    example: climate.airco_gang
sequence:
  - service: climate.turn_on
    target:
      entity_id: '{{ entity_id }}'
  - service: climate.set_hvac_mode
    target:
      entity_id: '{{ entity_id }}'
    data:
      hvac_mode: heat
  - service: climate.set_temperature
    data:
      temperature: '{{ temperature }}'
    target:
      entity_id: '{{ entity_id }}'
mode: single

Let say I want to modify the temperature of 2 aircons (climate.airco_living_links and climate.airco_living_rechts) with this button, how can I do that ?

I don’t have a way to test this, but entity_ids will normally accept a list as a valid input… so try something like:

type: button
tap_action:
  action: call-service
  service: script.switch_airco_x_op_y_graden
  service_data:
    temperature: '20'
    entity_id: 'climate.airco_living_links, climate.airco_living_rechts'
name: Airco x op y graden

Yes, works that way !

Now I tried your suggestion of testing the script in the editor, but I get an error (Template rendered invalid entity IDs: {‘description’: ‘The climate entity you want to control’, ‘default’: ‘climate.airco_gang’, ‘example’: ‘climate.airco_gang’}) ?

Schermafbeelding 2021-12-27 081142

And another thing : how can I easily see where a script is used (I have to replace the old script by this one) ?

You need to use fields instead of variables (See post #6)

Still an error :thinking:

alias: 'Switch : Airco x op y graden'
description: Set airco mode to heat and set temperature
fields:
  temperature:
    description: The temperature to set the airco at
    default: '25'
    example: '21'
  entity_id:
    description: The climate entity you want to control
    default: climate.airco_gang
    example: climate.airco_gang
sequence:
  - service: climate.turn_on
    target:
      entity_id: '{{ entity_id }}'
  - service: climate.set_hvac_mode
    target:
      entity_id: '{{ entity_id }}'
    data:
      hvac_mode: heat
  - service: climate.set_temperature
    data:
      temperature: '{{ temperature }}'
    target:
      entity_id: '{{ entity_id }}'
  - service: climate.set_fan_mode
    data:
      fan_mode: auto
    target:
      entity_id: '{{ entity_id }}'
mode: single

Quotes do not change anything :frowning:

alias: 'Switch : Airco x op y graden'
description: Set airco mode to heat and set temperature
fields:
  temperature:
    description: The temperature to set the airco at
    default: 25
    example: 21
  entity_id:
    description: The climate entity you want to control
    default: climate.airco_gang
    example: climate.airco_gang
sequence:
  - service: climate.turn_on
    target:
      entity_id: '{{ entity_id }}'
  - service: climate.set_hvac_mode
    target:
      entity_id: '{{ entity_id }}'
    data:
      hvac_mode: heat
  - service: climate.set_temperature
    data:
      temperature: '{{ temperature }}'
    target:
      entity_id: '{{ entity_id }}'
  - service: climate.set_fan_mode
    data:
      fan_mode: auto
    target:
      entity_id: '{{ entity_id }}'
mode: single

No heating this morning…

Even when I copy / paste the code of your post, I get the error of entity_id…

alias: 'Switch : Airco x op y graden'
description: ' Set airco mode to heat and set temperature'
fields:
  temperature:
    description: "The temperature to set the airco at"
    default: 25
    example: "21.2"
  entity_id:
    description: "The climate entity you want to control"
    default: "climate.airco_gang"
    example: "climate.airco_gang"
sequence:
  - service: climate.set_temperature
    target:
      entity_id: "{{ entity_id }}"
    data:
      temperature: "{{ temperature }}"
      hvac_mode: heat
mode: single

I don’t understand why it suddenly doesn’t work anymore ?

Meanwhile everything is OK, but for some reason sometimes it doesn’t (aircon is still off).

So I want to execute the script a number of times till my aircon is on.

But I don’t know what I have to set as condition (in bold) :

alias: 'Switch : Airco x op y graden'
description: Set airco mode to heat and set temperature
fields:
  temperature:
    description: The temperature to set the airco at
    default: 22
    example: '21'
  entity_id:
    description: The climate entity you want to control
    default: climate.airco_gang
    example: climate.airco_gang
sequence:
  - repeat:
      sequence:
        - service: climate.turn_on
          target:
            entity_id: '{{ entity_id }}'
        - service: climate.set_hvac_mode
          target:
            entity_id: '{{ entity_id }}'
          data:
            hvac_mode: heat
        - service: climate.set_temperature
          data:
            temperature: '{{ temperature }}'
          target:
            entity_id: '{{ entity_id }}'
        - service: climate.set_fan_mode
          data:
            fan_mode: auto
          target:
            entity_id: '{{ entity_id }}'
      until:
        - condition: template
          value_template: '{{ not is_state(**entity_id**, 'off') or repeat.index >= 2 }}'
mode: single

@Didgeridrew Could you take a look at it please ?