Script Template Assistance

Writing a quick script for a new Dyson fan I picked up, but running into issues with the template, what am I doing wrong here?

bedroom_fan_night:
    alias: 'Bedroom Fan Night'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: 2
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true
      - service: climate.set_fan_mode
        data:
          entity_id: climate.bedroom
          fan_mode: "Focus Mode"
      - service: climate.set_temperature
        data_template:
          entity_id: climate.bedroom
          temperature: "{{ states('input_number.sleep_temp' ) |int }}"
          operation_mode: "{{ states('sensor.thermostat_mode') }}"
      - service: fan.dyson_set_night_mode
        data:
          entity_id: fan.bedroom
          night_mode: true

If I load up the Template Editor in the UI, both templates render just fine.

Guess it would help if I described whatā€™s broken hu?! (thanks @petro)!
When the script gets run, all service entries run, like they should, except the one containing the template, the script literally skips it.

Cheers

Whatā€™s the exact problem?

The yaml format looks good and the templates look good. So pretty much need more information on

1 Like

Hmm, it looks good. Turn on logging and watch the home assistant logs and verify that itā€™s not getting called. Check for errors too. When I say watch the logging, iā€™m refering to the home-assistant.log file inside the config directory. Donā€™t bother looking at the logs in the UI.

Try

      - service: climate.set_temperature
        data_template:
          entity_id: climate.bedroom
          temperature: >
            {{ states('input_number.sleep_temp' ) |int }}
          operation_mode: "{{ states('sensor.thermostat_mode') }}"

I am assuming there is no issue with the input_number.sleep_temp and sensor.thermostat_mode

Thanks guys, Iā€™ll give both of those a try. Hereā€™s what the templates render to, in the template editor:
ā€œ{{ states(ā€˜sensor.thermostat_modeā€™) }}ā€
ā€œ{{ states(ā€˜input_number.sleep_tempā€™ ) |int }}ā€

ā€œHeatā€
ā€œ65ā€

Think I found my problemā€¦

This Works
climate.set_operation_mode
{
ā€œentity_idā€: ā€œclimate.bedroomā€,
ā€œoperation_modeā€: ā€œheatā€
}

This Does Not
climate.set_operation_mode
{
ā€œentity_idā€: ā€œclimate.bedroomā€,
ā€œoperation_modeā€: ā€œHeatā€
}

As listed above, the template returns ā€œHeatā€, not ā€œheatā€!
Anyone know the Jinja2 for converting case from upper to lower?

Then again, this also Works! (WTF)
climate.set_temperature
{
ā€œentity_idā€: ā€œclimate.bedroomā€,
ā€œtemperatureā€: ā€œ65ā€,
ā€œoperation_modeā€: ā€œHeatā€
}

{{ "Heat".lower() }}

or

{{ "Heat" | lower }}

in your case:

"{{ states('sensor.thermostat_mode') | lower }}"
1 Like

Iā€™ve tried separating the 2 template options, to different steps, but that still didnā€™t work.

  bedroom_fan_night:
    alias: 'Bedroom Fan Night'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: 2
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true
      - service: climate.set_fan_mode
        data:
          entity_id: climate.bedroom
          fan_mode: "Focus Mode"
      - service: climate.set_operation_mode
        data_template:
          entity_id: climate.bedroom
          operation_mode: "{{ states('sensor.thermostat_mode') |lower }}"
      - service: climate.set_temperature
        data_template:
          entity_id: climate.bedroom
          temperature: "{{ states('input_number.sleep_temp' ) |int }}"
      - service: fan.dyson_set_night_mode
        data:
          entity_id: fan.bedroom
          night_mode: true

Going to add some more steps, with notifications, to see where it breaks.

Have you tried just calling the service from the service tab?

EDIT: Donā€™t use templates in this, they wonā€™t work. Just use a normal json dict:

Service Data (JSON, optional)

{"entity_id":"climate.x", "operation_mode":"Heat"}

Yes, this does work (see 4 comments above).

This Works - and is done from the service tab
climate.set_operation_mode
{
ā€œentity_idā€: ā€œclimate.bedroomā€,
ā€œoperation_modeā€: ā€œheatā€
}

Lets see which ones fail, and which work:

  bedroom_fan_night_1:
    alias: 'Bedroom Fan Night 1'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: 2

  bedroom_fan_night_2:
    alias: 'Bedroom Fan Night 2'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: 2
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true

  bedroom_fan_night_3:
    alias: 'Bedroom Fan Night 3'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: 2
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true
      - service: climate.set_operation_mode
        data_template:
          entity_id: climate.bedroom
          operation_mode: "{{ states('sensor.thermostat_mode') |lower }}"

  bedroom_fan_night_4:
    alias: 'Bedroom Fan Night 4'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: 2
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true
      - service: climate.set_operation_mode
        data_template:
          entity_id: climate.bedroom
          operation_mode: "{{ states('sensor.thermostat_mode') |lower }}"
      - service: climate.set_temperature
        data_template:
          entity_id: climate.bedroom
          temperature: "{{ states('input_number.sleep_temp' ) |int }}"

Iā€™m convinced there is something wrong with the component, or my specific fan (even though it works fine via the app).

The fan seems to do the specified action in the script, but is randomly and instantly reverted. For instance, I set it to cool, auto, and oscillate (which it did), then via a script I set it to a speed of 2 (and left everything else the same), It switched to 2, then immediately switched back to Auto.

Iā€™m noticing the same behavior with every other version of the scripts Iā€™m throwing at it.

Hate to revive something so old, but itā€™s still revellent.

Absolutely everything works in this script, as expected, except for the ā€œclimate.set_temperatureā€

  bedroom_fan_night_testing:
    alias: 'Bedroom Fan Night TEST'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: "2"
      - delay: '00:00:01'
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true
      - delay: '00:00:01'
      - service: climate.set_temperature
        data_template:
          entity_id: fan.bedroom
          temperature: "65"
          operation_mode: "Heat"
      - delay: '00:00:05'
      - service: dyson.set_night_mode
        data:
          entity_id: fan.bedroom
          night_mode: true

Iā€™ve also tried breaking out climate.set_temperature & climate.set_operation_mode, but the script still skips them, and continues on to dyson.set_night_mode.

If I open the actual component, and set the temp & mode, it works just fine.
40%20PM

Also, if I try and fire the climate.set_temperature & climate.set_operation_mode from Dev/Services, it fails as well (literally nothing happens, no logs, nothing).

Any ideas?

Youā€™re using a service for the climate domain on a device in the fan domain. Sure you donā€™t have the correct entity_id?

EDIT: all your other posts call out climate.bedroom for your climate.set_temperatures

Thank you, that was it actually! Thought I was going nuts.

It seems this fan makes 2 different devices. fan.bedroom and climate.bedroom

fan.bedroom controls the fan, and climate.bedroom controls the heating, cooling, temp & operation modes. Wish that was better documented (Maybe Iā€™ll go fix the docs so others donā€™t get stuck).

1 Like

yeah it tripped me up too because it doesnā€™t really spell that out

For others with the Dyson Pure Hot+Cool:
(assuming your fan is named bedroom, for instance)

Use fan.bedroom for the following:
fan.oscillate
fan.set_direction
fan.set_speed
fan.toggle
fan.turn_on
fan.turn_off
dyson.set_night_mode

Use climate.bedroom for the following:
climate.set_temperature
climate.set_operation_mode
climate.turn_on
climate.turn_off
climate.set_fan_mode

Iā€™m going to add a note in the component documentation as well.

My script for turning on the fan works, which is great. But Iā€™ve run into an odd delema.
If you pass a temperature value to climate.set_temperature it automatically sets the operation_mode to heat, no matter if you manually set it to cool or not.

Currently Iā€™m passing the temperature value to the fan as a template from my thermostat sleep temperature. As well as the operation_mode from my thermostat. This way thru out the year, when the script fires the temperature and operation_mode match that of my thermostat.

Works great in the winter, because it turns on the heat. But in the summer it also turns on the heat (even though cool is passed).

Iā€™m wondering if I could use an if statement.
So IF the thermostat_mode is set to heat, call the service climate.set_temperature, with the temperature matching my thermostat temperature, and mode.
Else IF thermostat_mode is set to cool, call climate.set_operation_mode with a mode of cool, and speed of auto.

Hereā€™s my current working script:

  bedroom_fan_night:
    alias: 'Bedroom Fan Night'
    sequence:
      - service: fan.set_speed
        data:
          entity_id: fan.bedroom
          speed: "2"
      - delay: '00:00:01'
      - service: fan.oscillate
        data:
          entity_id: fan.bedroom
          oscillating: true
      - delay: '00:00:01'
      - service: climate.set_temperature
        data_template:
          entity_id: climate.bedroom
          temperature: "{{ states('input_number.sleep_temp' ) |int }}"
          operation_mode: "{{ states('sensor.thermostat_mode') | lower }}"
      - delay: '00:00:05'
      - service: dyson.set_night_mode
        data:
          entity_id: fan.bedroom
          night_mode: true