Temp and contact sensor automations

Hi legends.

I am new to this and am now trying to build some basic automation around temperature and contact sensors.

I am wanting the heater to turn off if the door has been left open for X amount of time or if the temp reaches above X

And I am wanting it to turn on of the door has been closed for X amount of time and if the fhe temp is below X

I thought I had it sorted but seems likes it still isn’t triggering even if the condktons are met.

I will post the details once I’m on my PC

Thanks

Taking your logic exactly as written, assuming the time is 5 minutes and making up some temperatures with a bit of hysteresis gap:

trigger:
  - platform: state
    entity_id: binary_sensor.door_contact
    to: "off"
    for: "00:05:00"
    id: "off"
  - platform: numeric_state
    entity_id: sensor.temp
    above: 19
    id: "off"

  - platform: state
    entity_id: binary_sensor.door_contact
    to: "on"
    for: "00:05:00"
    id: "on"
  - platform: numeric_state
    entity_id: sensor.temp
    below: 18
    id: "on"
condition:
  - or:
    - condition: trigger
      id: "off"
    - and:
      - condition: state
        entity_id: binary_sensor.door_contact
        state: "on"
        for: "00:05:00"
      - condition: numeric_state
        entity_id: sensor.temp
        below: 18
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.heater

Triggers off any of the possible things. The conditions then check: either we’re turning off (which only requires one of the things to be true; or both of the “and” requirements are OK for turning on.

Usual caveat that numeric state triggers only fire when the boundary is crossed. If it’s already “hot” at an HA restart, the action may not run as you might expect.

I will give that a run!

This is what i currently have set for the turning it on:

alias: Polly heater turn on
description: heater turns on when the door is closed and temp is below 16
trigger:

  • platform: device
    type: turned_off
    device_id: 3e0c48f59ecb4567362f38260a412f5d
    entity_id: 903fec1836021cdeee4be488d040e082
    domain: switch
    condition:
  • condition: and
    conditions:
    • type: is_not_open
      condition: device
      device_id: 05815f4b6362dfc8e99e060d892b98a4
      entity_id: 58210fa8849fb68bc1a6e236a70f3a15
      domain: binary_sensor
      for:
      hours: 0
      minutes: 0
      seconds: 0
  • condition: and
    conditions:
    • type: is_temperature
      condition: device
      device_id: eb08ce86521ee135fc268a660b230e76
      entity_id: 67a630cfbafa4e9e212658a08c56a9af
      domain: sensor
      above: 0
      below: 19
      action:
  • type: turn_on
    device_id: 3e0c48f59ecb4567362f38260a412f5d
    entity_id: 903fec1836021cdeee4be488d040e082
    domain: switch
    mode: single

Always better to use entity-based triggers and conditions (state, numeric state etc) rather than the complicated device triggers.

Also, please format code properly for the forum: either use the </> button or type three backticks before and after the code:

```
code goes here
```

Thansk fella!

I will for next time.

I have tried your code and i think its working but I do get an error in the trace when testing:

Error: Template rendered invalid service: switch.turn_

alias: Polly room automation test
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.polly_door_sensor_door
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "off"
  - platform: numeric_state
    entity_id:
      - sensor.temperature_humidity_sensor_temperature_2
    above: 19
    id: "off"
  - platform: state
    entity_id:
      - binary_sensor.polly_door_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "on"
  - platform: numeric_state
    entity_id:
      - sensor.temperature_humidity_sensor_temperature_2
    below: 14
    id: "on"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: "off"
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.polly_door_sensor_door
            state: "on"
            for:
              hours: 0
              minutes: 3
              seconds: 0
          - condition: numeric_state
            entity_id: sensor.temp
            below: 14
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.polly_room_heater_switch

When you say “when testing”, do you mean “manually executing”? That’s expected behaviour: if you manually execute it, none of the triggers fires, and trigger.id is not set.

Also, I think you need to replace my sensor.temp placeholder in the final condition with the real entity ID.

my oversight on the sensor.temp. this is changed.

I tried closing the door in the room and waiting to see if it turned on, but no go.
i will see how it runs overnight. thank you for your help!

It will only turn on if it’s been closed for 3 minutes and it’s below 14°C. It should have generated a trace showing the trigger and being stopped by a condition.

I changed the temp to a more reasonale 19c as it was floating around 17ish. looking at the traces it seems to have triggered correctly.

i have set up the same automation in another room and have received an error in the traces:
Executed: April 17, 2024 at 7:58:36 PM

Error: Template rendered invalid service: switch.turn_

alias: Master bedroom temp tester
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.master_bedroom_door_sensor_door
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "off"
  - platform: numeric_state
    entity_id:
      - sensor.master_bedroom_temp_humidity_sensor_temperature
    above: 21
    id: "off"
  - platform: state
    entity_id:
      - binary_sensor.master_bedroom_door_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "on"
  - platform: numeric_state
    entity_id:
      - sensor.master_bedroom_temp_humidity_sensor_temperature
    below: 19
    id: "on"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: "off"
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.master_bedroom_door_sensor_door
            state: "on"
            for:
              hours: 0
              minutes: 3
              seconds: 0
          - condition: numeric_state
            entity_id: sensor.master_bedroom_temp_humidity_sensor_temperature
            below: 19
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.master_bedroom_heater_plug

Again, you must have manually executed it. This error:

Error: Template rendered invalid service: switch.turn_

is indicating that trigger.id is empty, as it’s the output of this:

  - service: switch.turn_{{ trigger.id }}

and trigger.id cannot be empty if one of the triggers fired, as they all have ids against them.

Are you perhaps thinking that you need to “execute” or “run” the automation to enable it once created?

ok, makes sense now, i get a bit impatient lol. both automations should now turn off when they reach their turn off trigger?

The heater switch should be turned off when the door has been left open for 3 minutes (assuming off == open) or when the temperature rises above the threshold.

That action will run even if the switch is already off: it’ll do nothing (can’t turn it more off :smiley: ) but will generate a trace.

I have checked the latest trace. it ha triggered, but ossibly the opposite way?
the door is closed and the temp is below the threshold and the switch turned off.

sorry for so much back and forth!

{
  "trace": {
    "last_step": "action/0",
    "run_id": "e4cc6f024ba1666e3247638290329b96",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-04-17T10:19:53.982575+00:00",
      "finish": "2024-04-17T10:19:54.100277+00:00"
    },
    "domain": "automation",
    "item_id": "1713347838734",
    "trigger": "state of binary_sensor.master_bedroom_door_sensor_door",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-04-17T10:19:53.982728+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.master_bedroom_temp_tester",
              "state": "on",
              "attributes": {
                "id": "1713347838734",
                "last_triggered": "2024-04-17T09:58:36.438534+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Master bedroom temp tester"
              },
              "last_changed": "2024-04-17T09:57:24.114995+00:00",
              "last_reported": "2024-04-17T09:58:36.441741+00:00",
              "last_updated": "2024-04-17T09:58:36.441741+00:00",
              "context": {
                "id": "01HVNQWWMPQXM9C459EBB82RF3",
                "parent_id": "01HVNQWWMN6DCJEGPK6598M52S",
                "user_id": null
              }
            },
            "trigger": {
              "id": "off",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "binary_sensor.master_bedroom_door_sensor_door",
              "from_state": {
                "entity_id": "binary_sensor.master_bedroom_door_sensor_door",
                "state": "on",
                "attributes": {
                  "device_class": "door",
                  "friendly_name": "Master bedroom door sensor Door"
                },
                "last_changed": "2024-04-17T10:15:32.026047+00:00",
                "last_reported": "2024-04-17T10:15:32.026047+00:00",
                "last_updated": "2024-04-17T10:15:32.026047+00:00",
                "context": {
                  "id": "01HVNRVWDTNZD7FQMADYH45M5W",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "binary_sensor.master_bedroom_door_sensor_door",
                "state": "off",
                "attributes": {
                  "device_class": "door",
                  "friendly_name": "Master bedroom door sensor Door"
                },
                "last_changed": "2024-04-17T10:16:53.980046+00:00",
                "last_reported": "2024-04-17T10:16:53.980046+00:00",
                "last_updated": "2024-04-17T10:16:53.980046+00:00",
                "context": {
                  "id": "01HVNRYCEWZ5MDBFN1Y6BC5TQW",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": 180
              },
              "attribute": null,
              "description": "state of binary_sensor.master_bedroom_door_sensor_door"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-04-17T10:19:53.982843+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "condition/0/conditions/0": [
        {
          "path": "condition/0/conditions/0",
          "timestamp": "2024-04-17T10:19:53.982929+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-04-17T10:19:53.984232+00:00",
          "changed_variables": {
            "context": {
              "id": "01HVNS3W7YTQ4ZAE5E75KP3AFD",
              "parent_id": "01HVNRYCEWZ5MDBFN1Y6BC5TQW",
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "switch",
              "service": "turn_off",
              "service_data": {},
              "target": {
                "entity_id": [
                  "switch.master_bedroom_heater_plug"
                ]
              }
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1713347838734",
      "alias": "Master bedroom temp tester",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "binary_sensor.master_bedroom_door_sensor_door"
          ],
          "to": "off",
          "for": {
            "hours": 0,
            "minutes": 3,
            "seconds": 0
          },
          "id": "off"
        },
        {
          "platform": "numeric_state",
          "entity_id": [
            "sensor.master_bedroom_temp_humidity_sensor_temperature"
          ],
          "above": 21,
          "id": "off"
        },
        {
          "platform": "state",
          "entity_id": [
            "binary_sensor.master_bedroom_door_sensor_door"
          ],
          "to": "on",
          "for": {
            "hours": 0,
            "minutes": 3,
            "seconds": 0
          },
          "id": "on"
        },
        {
          "platform": "numeric_state",
          "entity_id": [
            "sensor.master_bedroom_temp_humidity_sensor_temperature"
          ],
          "below": 19,
          "id": "on"
        }
      ],
      "condition": [
        {
          "condition": "or",
          "conditions": [
            {
              "condition": "trigger",
              "id": "off"
            },
            {
              "condition": "and",
              "conditions": [
                {
                  "condition": "state",
                  "entity_id": "binary_sensor.master_bedroom_door_sensor_door",
                  "state": "on",
                  "for": {
                    "hours": 0,
                    "minutes": 3,
                    "seconds": 0
                  }
                },
                {
                  "condition": "numeric_state",
                  "entity_id": "sensor.master_bedroom_temp_humidity_sensor_temperature",
                  "below": 19
                }
              ]
            }
          ]
        }
      ],
      "action": [
        {
          "service": "switch.turn_{{ trigger.id }}",
          "target": {
            "entity_id": "switch.master_bedroom_heater_plug"
          }
        }
      ]
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HVNS3W7YTQ4ZAE5E75KP3AFD",
      "parent_id": "01HVNRYCEWZ5MDBFN1Y6BC5TQW",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Master bedroom temp tester",
      "message": "triggered by state of binary_sensor.master_bedroom_door_sensor_door",
      "source": "state of binary_sensor.master_bedroom_door_sensor_door",
      "entity_id": "automation.master_bedroom_temp_tester",
      "context_id": "01HVNS3W7YTQ4ZAE5E75KP3AFD",
      "when": 1713349193.983198,
      "domain": "automation"
    },
    {
      "when": 1713349194.608292,
      "state": "off",
      "entity_id": "switch.master_bedroom_heater_plug",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Master bedroom temp tester",
      "context_message": "triggered by state of binary_sensor.master_bedroom_door_sensor_door",
      "context_source": "state of binary_sensor.master_bedroom_door_sensor_door",
      "context_entity_id": "automation.master_bedroom_temp_tester"
    }
  ]
}

Maybe I got the contact switches the wrong way around. I’d ass-u-med they were off when the door is open.

Just change the two to: statements for the door sensors the other way around, as well as the single state: "on" to state: "off" in the condition.

looks like got is working as intended wit hthe below code. thank you so much for your help!

alias: Master bedroom temp tester
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.master_bedroom_door_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "off"
  - platform: numeric_state
    entity_id:
      - sensor.master_bedroom_temp_humidity_sensor_temperature
    above: 21
    id: "off"
  - platform: state
    entity_id:
      - binary_sensor.master_bedroom_door_sensor_door
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: "on"
  - platform: numeric_state
    entity_id:
      - sensor.master_bedroom_temp_humidity_sensor_temperature
    below: 19
    id: "on"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: "off"
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.master_bedroom_door_sensor_door
            state: "off"
            for:
              hours: 0
              minutes: 3
              seconds: 0
          - condition: numeric_state
            entity_id: sensor.master_bedroom_temp_humidity_sensor_temperature
            below: 19
action:
  - service: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.master_bedroom_heater_plug

1 Like

I’ll advise you to switch to a generic thermostat instead of automation.

It makes it much simpler for you.
All the normal automation will be done automatically.
You don’t need to worry about restarts or automation reloads that break your automation.
You will get a GUI to set the temperature instead of changing in the automation.
The only thing you need to add is an automation that triggers on the door and switch off the generic thermostat entity