Script to turn on fan(s) based on temp

I’m sure this a pretty novice question. I am writing a “goodnight” script and I want to have an If/then that will turn 0, 1, or 2 fans on depending on the temperature that my thermostat is reading. I’m sure its simple, but I am having a hard time figuring it out. All of my searches seemed to get me answers on automation triggers and conditions, but I want this to be part of a script that will be trigger by alexa when we say goodnight.

The part of the script with the issue:

- alias: Turn on fans
  service: switch.turn_on
  data_template:
    entity_id: >-
      {% if sensor.thermostat_temperature > 66 %}
         switch.master_outlet
         switch.master_ceiling_fan
      {% elif sensor.thermostat_temperature > 62 %}
         switch.master_outlet
      {% else %}
         none
      {% endif %}

No erros in check config

Error in log:

Error rendering data template: UndefinedError: ‘sensor’ is undefined

Thanks in advanced for any help.

  service: switch.turn_on
  data_template:
    entity_id: >-
      {% if states('sensor.thermostat_temperature') | flaot > 66 %}
         - switch.master_outlet
         - switch.master_ceiling_fan
      {% elif states('sensor.thermostat_temperature') | float > 62 %}
         switch.master_outlet
      {% else %}
         none 
      {% endif %}

You need to work out what to do if neither of your first two statements are true, ‘none’ is not an acceptable switch entity id and will generate errors.

One work-around could be to use the service homeassistant.turn_on instead of switch.turn_on and put a dummy input boolean (that you must create) in the ‘else’ case.

I wasn’t sure about the none as an valid entry. I saw a lot of people had issues with it, but I’m pretty sure somewhere in the HA documentation its used in an example.

So I made the changes as suggested and I’m getting an error in my check config now.

  service: switch.turn_on
  data_template:
    entity_id: >-
      {% if states('sensor.thermostat_temperature') | float > 66 %}
         switch.master_outlet
         switch.master_ceiling_fan
      {% elif if states('sensor.thermostat_temperature') | float > 62 %}
         switch.master_outlet
      {% else %}
         switch.master_ceiling_fan
      {% endif %}

I also tried it with the dashes as you had them and it didn’t make a difference. I added a switch into the else just for testing purposes. Once I get the script to function right I’ll make something else to be there.

Error:

Invalid config for [script]: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘states’) for dictionary value @ data[‘script’][‘goodnight’][‘sequence’][5][‘data_template’][‘entity_id’]. Got “{% if states(‘sensor.thermostat_temperature’) | float > 66 %}\n switch.master_outlet\n switch.master_ceiling_fan\n{% elif if states(‘sensor.thermostat_temperature’) | float > 62 %}\n switch.master_outlet\n{% else %}\n switch.master_ceiling_fan\n{% endif %}”. (See /config/configuration.yaml, line 37). Please check the docs at Scripts - Home Assistant

Opps. I left a - in. Try this:

  service: switch.turn_on
  data_template:
    entity_id: >
      {% if states('sensor.thermostat_temperature') | flaot > 66 %}
         - switch.master_outlet
         - switch.master_ceiling_fan
      {% elif states('sensor.thermostat_temperature') | float > 62 %}
         switch.master_outlet
      {% else %}
         none 
      {% endif %}

Using this: entity_id: > does not strip newline charachers. entity_id: >- does.
So if the first test evaluates as true you were getting:

  service: switch.turn_on
  data:
    entity_id:
      - switch.master_outlet  - switch.master_ceiling_fan

Instead of:

  service: switch.turn_on
  data:
    entity_id:
      - switch.master_outlet 
      - switch.master_ceiling_fan

I think this will work too:

  service: switch.turn_on
  data_template:
    entity_id: >-
      {% if states('sensor.thermostat_temperature') | flaot > 66 %}
         switch.master_outlet, switch.master_ceiling_fan
      {% elif states('sensor.thermostat_temperature') | float > 62 %}
         switch.master_outlet
      {% else %}
         none 
      {% endif %}

I’ve never seen this. I’d be interested if you could find it.

1 Like

Triggering this:

- alias: 'none tester'
  trigger:
  - platform: state
    entity_id: input_boolean.toggler
  action:
  - service: light.toggle
    data_template:
      entity_id: >-
        none

results in this:

Error while executing automation automation.none_tester. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

This doesn’t even pass Config Check:

- alias: 'none tester'
  trigger:
  - platform: state
    entity_id: input_boolean.toggler
  action:
  - service: light.toggle
    entity_id: none

Invalid config for [automation]: not a valid value for dictionary value @ data[‘action’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 98). Please check the docs at Automation - Home Assistant


EDIT

If you must use a ‘dummy entity’ then use a non-existent entity like switch.dummy or switch.none.

For example, this executes without producing any error or warning messages:

- alias: 'none tester'
  trigger:
  - platform: state
    entity_id: input_boolean.toggler
  action:
  - service: light.toggle
    entity_id: light.none

Be advised this was tested with version 0.91 which, like previous versions, simply ignores (fails quietly for) non-existent entities. There’s no guarantee that future versions will continue to operate this way.

1 Like

I came across the example of using “none” a few times and then forum posts about people having issue with it and being told they couldn’t use it. Of course now that I want to find it I can’t seem to.

Here is how my code sits now. I made a separate script just to test this one section of my goodnight script. Check Config says its good, but I get an error when I run it and now action on my switches.

test:
  alias: Test Script
  sequence:
    - alias: Turn on fans
      service: switch.turn_on
      data_template:
        entity_id: >
          {% if states('sensor.thermostat_temperature') | float > 66 %}
             - switch.master_outlet
             - switch.master_ceiling_fan
          {% elif states('sensor.thermostat_temperature') | float > 62 %}
             switch.master_outlet
          {% else %}
             switch.master_ceiling_fan
          {% endif %}

Error:

Error executing service <ServiceCall script.test (c:f8501800ebdb439b8730bf3b6ece5b82)>
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/homeassistant/core.py”, line 1147, in _safe_execute
await self._execute_service(handler, service_call)
File “/usr/local/lib/python3.7/site-packages/homeassistant/core.py”, line 1160, in _execute_service
await handler.func(service_call)
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/script/init.py”, line 114, in service_handler
context=service.context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/script/init.py”, line 172, in async_turn_on
kwargs.get(ATTR_VARIABLES), context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 131, in async_run
await self._handle_action(action, variables, context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 210, in _handle_action
action, variables, context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/script.py”, line 299, in _async_call_service
context=context
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/service.py”, line 89, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context)
File “/usr/local/lib/python3.7/site-packages/homeassistant/core.py”, line 1118, in async_call
processed_data = handler.schema(service_data)
File “/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled(, data)
File “/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 589, in validate_dict
return base_validate(path, iteritems(data), out)
File “/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py”, line 427, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: not a valid value for dictionary value @ data[‘entity_id’]

        entity_id: >
          {% if states('sensor.thermostat_temperature') | float > 66 %}
             switch.master_outlet, switch.master_ceiling_fan

This works. Thank you

I created the dummy input boolean for the else case and changed the service to homeassistant.turn_on and it did work anymore. I had to combine the two fans into a group and use that and then it worked.