Automation, turn on switch Temperature trigger

I’m trying to create an automation that checks the temperature in a room, then, if below 21c turns on a switch attached to a heater, only if someone is home at a certain time. The issue is, if the temperature is below 21c when someone isn’t home, the trigger doesn’t re trigger when someone comes home. Would another automation be needed for when someone returns home?

This is my current automation.

automation:
  - id: heater_on
    alias: Heater On
    trigger:
      platform: numeric_state
      entity_id: sensor.wirelesstag_temperature
      below: '21'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: group.family
        state: 'on'
      - condition: time
        after: '18:00:00'
        before: '07:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.heater

try adding ‘person coming home’ as an additional trigger and ‘temp below 21’ as an additional condition… that way it should catch both situations

Did @sparkydave’s suggestion solve your problem? If so, you should mark his post as the solution.

I did have another thought, though. The way you wrote your conditions it looks like maybe there’s a third trigger you may want – i.e., at 18:00:00. In other words, it seems you want to turn on the heater if 1) the temperature drops below 21, or 2) someone comes home (when nobody was home), or 3) at 18:00:00, but only if the temperature is below 21, and someone is home, and the time is between 18:00:00 and 07:00:00 (when any of the three triggers occur.) So basically:

automation:
  - id: heater_on
    alias: Heater On
    trigger:
      - platform: numeric_state
        entity_id: sensor.wirelesstag_temperature
        below: 21
      - platform: state
        entity_id: group.family
        to: 'on'
      - platform: time
        at: '18:00:00'
    condition:
      - condition: numeric_state
        entity_id: sensor.wirelesstag_temperature
        below: 21
      - condition: state
        entity_id: group.family
        state: 'on'
      - condition: time
        after: '18:00:00'
        before: '07:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.heater

Note that you don’t need to explicitly use condition: and since that is the default.

One other suggestion. Depending on how often your HA instance restarts, and what you want to do with the heater when it does, you may want to consider adding a fourth trigger:

      - platform: homeassistant
        event: start

With this added, when HA starts, if all three conditions are met, the heater will be turned on.

2 Likes

You can create a sensor template called heater_on.

sensor:
  - platform: template
    sensors:
      heater_on:
        friendly_name: 'Heater On'
        value_template: >-
          {%- if (is_state("device_tracker.xxxx","home") or is_state("device_tracker.yyyy","home")) and (states.sensor.wirelesstag_temperature.attributes.state | int < 21))  -%}
             True
          {%- else -%}
            False
          {%- endif -%}
        entity_id:
          - device_tracker.xxxx
          - device_tracker.yyyy
          - sensor.wirelesstag_temperature

This sensor will become true when someone is at home and the temperature is below 21 regardless of time and will update if any entity changes state.
I divided your group into device trackers and use “or” in the template.

Then you should create an automation and check the time:

automation:
  - id: set_heater_on
    alias: Set Heater On
    trigger:
      platform: state
      entity_id: heater_on
      to: 'on'
    condition:
      - condition: time
        after: '18:00:00'
        before: '07:00:00'
    action:
      service: switch.turn_on
      entity_id: switch.heater

This should work.

3 Likes

Thanks for help everyone. Set up the triggers and all seems to work.