Passing variables to scripts

So, my first time passing variables to scripts is a bit frustrating! I think I’m nearly there, but not quite. Essentially I want to pass an entity ID and hvac_mode to a script to turn a radiator on and off from a switch

The entity ID part seems to be working, but whatever I try it always fires nothing, or always fires the first condition in my script only.

Here’s what I have:

Script:

alias: Script - Heating
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ hvac_mode == heat }}"
        sequence:
          - service: climate.set_temperature
            metadata: {}
            data:
              hvac_mode: heat
              temperature: 22
            target:
              entity_id: "{{ radiator }}"
      - conditions:
          - condition: template
            value_template: "{{ hvac_mode == turn_off }}"
        sequence:
          - service: climate.set_hvac_mode
            metadata: {}
            data:
              hvac_mode: "off"
            target:
              entity_id: "{{ radiator }}"
fields:
  hvac_mode:
    name: hvac_mode
  radiator:
    selector:
      entity: {}
    name: radiator

Switch:

    heating_test:
        unique_id: id_hsw_0000
        turn_on:
         service: script.script_heating
         data:
           radiator: climate.tado_smart_radiator_thermostat_va3632810752
           variables:
             hvac_mode: heat
        turn_off:
         service: script.script_heating
         data:
           radiator: climate.tado_smart_radiator_thermostat_va3632810752
           variables:
             hvac_mode: turn_off
        friendly_name: "Heating Switch Test"
        value_template: "{{ is_state('climate.tado_smart_radiator_thermostat_va3632810752', 'heat') }}"

With this the first condition (turn on) is fired for both the on and off, so it doesn’t appear to be picking up the hvac_mode variable correctly.

I’ve tried various combinations of quotes, with and without the “variables” option in the switch config and nothing seems to work. If I pass hvac_mode not as a variable (i.e. in the same way I’m passing radiator) then it doesn’t fire at all.

Any suggestions?

I would try this:

heating_test:
        unique_id: id_hsw_0000
        turn_on:
         service: script.script_heating
         data:
           radiator: climate.tado_smart_radiator_thermostat_va3632810752
           hvac_mode: heat
        turn_off:
         service: script.script_heating
         data:
           radiator: climate.tado_smart_radiator_thermostat_va3632810752
           hvac_mode: turn_off
        friendly_name: "Heating Switch Test"
        value_template: "{{ is_state('climate.tado_smart_radiator_thermostat_va3632810752', 'heat') }}"

This is not correct.

change it to

          - condition: template
            value_template: "{{ hvac_mode == 'heat' }}"

and

          - condition: template
            value_template: "{{ hvac_mode == 'turn_off' }}"

Then use what @Hellis81 said.

1 Like

Didn’t notice that.

Brilliant, thanks to both of you. I assumed it would be a syntax error somewhere but finding it isn’t always as simple as it sounds!

Thanks guys