Temp Sensor Automation (simple)

New to this and I’m struggling with getting this automation to work properly. I basically want a smart plug to power up for a given number of minutes anytime the temperature is below a given threshold between the hours of 0600 and 1800.

So I have the following automation:

alias: New Automation - temps testing
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.foobar
    below: 99
condition:
  - condition: time
    before: "18:00:00"
    after: "06:00:00"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.plug_1
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.plug_1
mode: single

I’ve created a Command Line Sensor so I can modify sensor.foobar and immediately see results:

sensor:
  - platform: command_line
    name: foobar
    command: "cat /tmp/sensor"
    scan_interval: 5

The problem with this is once the value of sensor.foobar is below 99, the switch.plug_1 is on for 15 seconds but then it never comes back on again. My expectations are clearly off. I tried using a state sensor in the trigger but that led to it always being true.

Order, and choosing between trigger and condition can be really confusing. Yes, I will read the docs again.

What can I do to keep turning the switch on between 0600 and 1800 when the temp is under 99?

So this works but it’s entirely dependent upon the temp sensor changing. While I know that’s likely to happen, not all sensors are going to work this way. This seems bulky, error prone and definitely not more widely applicable. I’m hoping for good suggestions as to how to improve!

alias: New Automation - temps testing
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.atc_b2db_temperature
condition:
  - condition: time
    before: "18:00:00"
    after: "13:10:00"
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.foobar
        below: 99
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.plug_1
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.plug_1
mode: single

What you are experiencing is a common challenge. The solution is simple; add a Time Trigger and a Numeric State Condition.

If the temperature decreases below 99 before 06:00:00, the switch will not be turned on because it’s too early. However, now that the Numeric State Trigger has triggered, it won’t trigger again until the temperature first rises above 99 and then decreases below it. If this doesn’t happen between 06:00 and 18:00, the automation will not be triggered on that day.

To mitigate this, we add a Time Trigger for 06:00:01 and a Numeric State Condition that checks if the temperature is below 99.

alias: New Automation - temps testing
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.foobar
    below: 99
  - platform: time
    at: "06:00:01"
condition:
  - condition: time
    before: "18:00:00"
    after: "06:00:00"
  - condition: numeric_state
    entity_id: sensor.foobar
    below: 99
action:
  - service: switch.turn_on
    target:
      entity_id: switch.plug_1
  - delay:
      seconds: 15
  - service: switch.turn_off
    target:
      entity_id: switch.plug_1
mode: single
1 Like