Automation not doing what I expected

I have an automation that I put together that is designed to control a small heater in our basement when it gets to cold.

Here is the automation:

- id: "Basement heat on"
  alias: 'Basement heat on'
  trigger: 
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      below: 15
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.basement_power

- id: "Basement heat off"
  alias: 'Basement heat off'
  trigger: 
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      above: 15
  action:
  - service: switch.turn_off
    data:
      entity_id: switch.basement_power

Here is the switch:

  - platform: mqtt
    name: "Basement power"
    state_topic: "stat/sonoff/POWER1"
    command_topic: "cmnd/sonoff/POWER1"
    availability_topic: "tele/sonoff/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

The problem shows up if I turn the switch on manually, the automation never turns it off. The state changes are seen in states panel. I have tested the temperature component and if not manually activated they work properly. Am I correct in understanding the automation should over ride the manual activation? If not, how would I correct the errored state automatically?

The switch is a Sonoff Dual running Tasmota.

Thanks Matt

The triggers will only work when the number goes from 15 or higher to below 15 (turn_on) or 15 or lower to above 15 (turn_off).

If you manually turn it on when itā€™s already above 15, it wonā€™t ever cross that value, and so wonā€™t ever trigger. Is that what youā€™re seeing?

Maybe one like this to turn it off after a while in that case:

- id: "Basement heat off timer"
  alias: 'Basement heat off timer'
  trigger:
    - platform: state
      entity_id: switch.basement_power
      state: 'on'
      for:
        hours: 1
    - platform: state
      entity_id: switch.basement_power
      state: 'on'
      for:
        hours: 2
  condition: 
    - condition: numeric_state
      entity_id: sensor.pws_temp_f
      above: 15
  action:
  - service: switch.turn_off
    data:
      entity_id: switch.basement_power

That will trigger after the switch has been on for one, then two, hours and if the temperature is above 15 will turn the switch off.

Did not realize that. So, this will check the temp after 1 hour and 2 hours then turn it off if the value is crosses the threshold?

Gonna test it now.

So, the above generates this log entryā€¦

Invalid config for [automation]: [state] is an invalid option for [automation]. Check: automat
ion->trigger->0->state. (See /config/configuration.yaml, line 426). Please check the docs at https://home-assistant.io/components/automation/

I have also tried ā€˜ONā€™ to exactly match the MQTT payload. The status in the states panel is on, lower case. Do I need to use a to and from pair?

This seems to work! Of course I will adjust the time and temp for good values, not testing values.

- id: "Basement heat off timer"
  alias: 'Basement heat off timer'
  trigger:
    - platform: state
      entity_id: switch.basement_power
      from: 'off'
      to: 'on'
      for:
        minutes: 2
    - platform: state
      entity_id: switch.basement_power
      from: 'off'
      to: 'on'
      for:
        minutes: 3
  condition: 
    - condition: numeric_state
      entity_id: sensor.pws_temp_f
      above: 49
  action:
  - service: switch.turn_off
    data:
      entity_id: switch.basement_power

Thanks for your guidance. I think I am on the right track now.

Is your spacing correct?

image

Yeah, sorry, Iā€™d mistakenly used state when I should have used to

I should really build the opposite safe guard into the other state as well. Thanks for the tip. Can you tell me why you suggested 2 timer checks? I will still need the triggers to turn it on and off for everyday automationā€¦

Me? I went with a couple of examples of possible durations thatā€™d work. Thereā€™s other ways of doing it, including using a time trigger to run the automation every half hour:

- id: "Basement heat off timer"
  alias: 'Basement heat off timer'
  trigger:
    - platform: time
      minutes:'/30'
      seconds: '00'
  condition: 
    - condition: numeric_state
      entity_id: sensor.pws_temp_f
      above: 15
      for:
        minutes: 10
    - condition: state
      entity_id: switch.basement_power
      state: 'on'
  action:
  - service: switch.turn_off
    data:
      entity_id: switch.basement_power

Itā€™ll check every half hour if the temperature has been above the target for 10 minutes and the switch is on. Pick intervals and durations that suit (or no duration if that suits).

1 Like

Iā€™d recommend using this automation with the time trigger, and add the temp threshold trigger too. I canā€™t see any case where this would actually be better than your previous version, but it just strikes me as ā€œsaferā€ because it runs every half hour. I almost proposed the time trigger myself, but deleted my reply because I thought I was being nit-picky!

Thanks for being patient.
I am a relative newb at writing rules that must work. This is a backup to heat tape to prevent a pipe from freezing. I was hoping that there was a way to prevent worst caseā€¦ For example if wifi drops as the threshold is crossed from high to low, that once wifi comes back the automation would check for a temperature below some point and would fire the on again every 15 or 20 minutes while the temp is below the trigger point. It almost looks like the last example from Tinkerer might do that, am I correct?

The most recent example by @tinkerer will check every thirty minutes if the temperature has been above 15 for at least ten minutes, and if the heater is on. If both of those are true, it will turn off the switch. I think they may have had an error in their condition though, because to use two conditions you need to specify if itā€™s an ā€œandā€ case or an ā€œorā€ case.

This example does the same, but also will trigger as soon as the heater has been above 15 for ten minutes, and it will fire every 30 minutes. So it will fire as soon as itā€™s been warm for ten minutes, but will also check every 30 minutes just in case the system somehow missed the temperature threshold being crossed.

- id: "Basement heat off timer"
  alias: 'Basement heat off timer'
  trigger:
	- platform: time
	  minutes:'/30'
	  seconds: '00'
	- platform: numeric_state
	  entity_id: sensor.pws_temp_f
	  above: 15
	  for:
		minutes: 10
  condition:
	condition: and
	conditions:
	  - condition: numeric_state
		entity_id: sensor.pws_temp_f
		above: 15
		for:
		  minutes: 10
	  - condition: state
		entity_id: switch.basement_power
		state: 'on'
  action:
  - service: switch.turn_off
	data:
	  entity_id: switch.basement_power

So, my lack of skills with complex automations is starting to really show. I have written test automations for both on and off and have them passing yaml muster in configurator. But I am getting an error that does not make sense to me. I get the log entry once for each automation, it is clear to me that it does not like the ā€œforā€ lines 18 and 45. Looking thru the HA docs it seems that this is a valid option for state, could it not be for numeric_state? I have tried it with ticks on and off of the ā€œbelowā€ and ā€œaboveā€ values as well. I have also tried ticks on and off for the minutes value under the ā€œfor:ā€

This is exactly what I am trying to achieve.

2018-01-11 22:58:03 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->condition->0->conditions->0->for. (See /config/
configuration.yaml, line 430). Please check the docs at https://home-assistant.io/components/automation/
2018-01-11 22:58:03 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->condition->0->conditions->0->for. (See /config/
configuration.yaml, line 430). Please check the docs at https://home-assistant.io/components/automation/
- id: "Basement heat on timer"
  alias: 'Basement heat on timer'
  trigger:
    - platform: time
      minutes: '/15'
      seconds: '00'
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      below: '20'
      for:
        minutes: 5
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.pws_temp_f
        below: '20'
        for:
          minutes: 5
      - condition: state
        entity_id: switch.basement_power
        state: 'off'
  action:
    - service: switch.turn_on
      data:
        entity_id: switch.basement_power

- id: "Basement heat off timer"
  alias: 'Basement heat off timer'
  trigger:
    - platform: time
      minutes: '/15'
      seconds: '00'
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      above: '20'
      for:
        minutes: 5
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.pws_temp_f
        below: '20'
        for:
          minutes: 5
      - condition: state
        entity_id: switch.basement_power
        state: 'on'
  action:
    - service: switch.turn_off
      data:
        entity_id: switch.basement_power

Actually, the default is and, you donā€™t have to specify it.

The numeric state trigger does support for, but it looks like the condition doesnā€™t:

- id: "Basement heat on timer"
  alias: 'Basement heat on timer'
  trigger:
    - platform: time
      minutes: '/15'
      seconds: '00'
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      below: '20'
      for:
        minutes: 5
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.pws_temp_f
        below: '20'
      - condition: state
        entity_id: switch.basement_power
        state: 'off'
  action:
    - service: switch.turn_on
      data:
        entity_id: switch.basement_power
1 Like

Good to know @tinkerer, I always hate the and syntax anyways.

And Matt, apparently itā€™s not your lack of skill with automations, itā€™s ours! :rofl:

So, this automation works great! I have shortened timers to test for functionality. I have also commented the code and would like to post it up to help others in the future. I think I have the comments correctly defined.

Is the conditional numeric_state redundant since there is a trigger that checks the temp as well? I think it will look to see if the temp is above or below the set threshold for X minutes before falling to the conditions.

Also, multiple triggers work as an andā€™ed pair? If one is not met, then the automation never falls thru to the conditions? Can triggers be built in an orā€™ed configuration?

I really love this forum, I would not be nearly as far along without the help and support of the awesome members here!

Here is my commented code, hope it helps someone like me in the future!

- id: "Basement heat on timer test"
  alias: 'Basement heat on timer test'
  trigger:
    - platform: time
      minutes: '/3'
      seconds: '00'
# Trigger every X minutes, "/" causes repeating trigger
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      below: '24'
      for:
        minutes: 2
# Trigger based on WU temp below X and has been for Y minutes
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.pws_temp_f
        below: '24'
# Verify WU temp below X
      - condition: state
        entity_id: switch.basement_power
        state: 'off'
# Verify switch is really off
  action:
    - service: switch.turn_on
      data:
        entity_id: switch.basement_power
# Set switch to correct state "ON"

I am also posting another automation that may interest folks. The first one looks at the temp and turns on the switch. The second one looks for a button press (maybe an accident), a state transition and the temperature and then corrects the state of the switch. I can see a use for this as well, so I am posting it up as well.

With both automations you will need one for each desired state ABOVE and BELOW. Both have correct yaml and have been tested and seem to work as desired.

- id: "Basement heat on"
  alias: 'Basement heat on'
  trigger: 
    - platform: numeric_state
      entity_id: sensor.pws_temp_f
      below: 20
# Temp is low
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.basement_power
# Turn it on

- id: "Basement heat on guard"
  alias: 'Basement heat on guard'
#Guard against button press on Sonoff
  trigger:
    - platform: state
      entity_id: switch.basement_power
      from: 'on'
      to: 'off'
      for:
        minutes: 2
# Did switch transition from ON to OFF for a duration of 2 minutes?
    - platform: state
      entity_id: switch.basement_power
      from: 'on'
      to: 'off'
      for:
        minutes: 3
# Did switch transition from ON to OFF for a duration of 3 minutes?
  condition: 
    - condition: numeric_state
      entity_id: sensor.pws_temp_f
      below: 20
#Verify below WU temp threshold
  action:
  - service: switch.turn_on
    data:
      entity_id: switch.basement_power
# Turn it back on

Once again, many thanks for the education. Now if only I could get some form of automated backup working that does not depend upon a windows machine with power shell up and runningā€¦