Please help with Temperature sensor automation

I’m really new to HA, and I can’t seem to figure out what I’m doing wrong with this automation:

- id: '95'
  alias: ac auto on
  trigger:
    - entity_id: sensor.aeotec_trisensor_temperature_measurement
      above: '74'
      platform: numeric_state
    - entity_id: sensor.attic_sensor_temperature_measurement
      above: '74'
      platform: numeric_state
    - entity_id: sensor.family_room_sensor_temperature
      above: '74'
      platform: numeric_state
    - entity_id: sensor.washer_temperature
      above: '74'
      platform: numeric_state
  condition: []
  action:
    - service: switch.turn_on
      data_template:
        entity_id: >-
          {% if "{{ trigger.entity_id }}" == 'sensor.aeotec_trisensor_temperature_measurement' %}
            switch.master_bedroom_ac
          {% elif "{{ trigger.entity_id }}" == 'sensor.attic_sensor_temperature_measurement' %}
            switch.kelseys_room_ac
          {% elif "{{ trigger.entity_id }}" == 'sensor.family_room_sensor_temperature' %}
            switch.family_room_ac
          {% endif %}

Though it passes the config check, I keep getting this error:

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

Try this:

      data_template:
        entity_id: >-
          {% if trigger.entity_id == 'sensor.aeotec_trisensor_temperature_measurement' %}
            switch.master_bedroom_ac
          {% elif  trigger.entity_id == 'sensor.attic_sensor_temperature_measurement' %}
            switch.kelseys_room_ac
          {% elif trigger.entity_id == 'sensor.family_room_sensor_temperature' %}
            switch.family_room_ac
          {% endif %}

The error changed to:

2019-05-21 20:20:39 ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: 'trigger' is undefined

How did you trigger it?

With the developer tools?

Under Configuration > Automations.

Yeah that’s not going to give you an entity_id that triggered the automation. Hence the error.

If you change the template to this then you should be able to test it:

      data_template:
        entity_id: >-
          {% if trigger.entity_id == 'sensor.aeotec_trisensor_temperature_measurement' %}
            switch.master_bedroom_ac
          {% elif  trigger.entity_id == 'sensor.attic_sensor_temperature_measurement' %}
            switch.kelseys_room_ac
          {% else %}
            switch.family_room_ac
          {% endif %}

This should switch on switch.family_room_ac if the trigger entity id is unknown.

Or leave the template as is and go and blow a heater or hair-dryer at one of your sensors.

So, if I use:
trigger.entity_id
{ trigger.entity_id }
{{ trigger.entity_id }}
they all fail a config check. I have to put it in quotes, though that still generates the error (both single and double quotes). I also used the developer tools to trigger the automation, and still got the error.

Try

{{ trigger.to_state.entity_id }}

Just for fun, here’s another way to perform the action using a dictionary instead of if-elif branching:

- id: '95'
  alias: ac auto on
  trigger:
    - platform: numeric_state
      entity_id: sensor.aeotec_trisensor_temperature_measurement
      above: '74'
    - platform: numeric_state
      entity_id: sensor.family_room_sensor_temperature
      above: '74'
    - platform: numeric_state
      entity_id: sensor.attic_sensor_temperature_measurement
      above: '74'
  condition: []
  action:
    - service: switch.turn_on
      data_template:
        entity_id: >-
          {% set map = {"aeotec_trisensor_temperature_measurement": "master_bedroom_ac", 
                        "attic_sensor_temperature_measurement": "kelseys_room_ac",
                        "family_room_sensor_temperature": "family_room_ac"} %}
          switch.{{ map[trigger.to_state.object_id] if trigger.to_state.object_id in map.keys() else 'unknown' }}

In your original example, the trigger is monitoring 4 sensors. However, action only handles 3 of them so I removed sensor.washer_temperature from trigger.

1 Like

The will turn on the first switch, but not the others. @123 code works, so I’m ditching my dodgy code. Thank you so much for trying to help me.

Thank you, your code works well!