Configure switch to turn on/off based on Temperature between time

Hello, I am fairly new to this and have a question that the community experts might be able to help with. I have configured the below in the automations.yaml file, however I would like for the switch to only be on between a certain time, say on at '07:00:00 and off at ‘19:00:00’ and for said temperature to turn the ac off/on between that time. I know that this would more than likely be a condition but I am having a hard time with formatting that template correctly. Any help would greatly be appreciated

- id: ChickenCoopAC_OffBasedOnTemperature
  alias: Turn off Chickencoop AC at 75 Degrees
  trigger:
    platform: numeric_state
    entity_id: sensor.chicken_coop_temperature
    below: 75
  action:
    service: switch.turn_off
    entity_id: switch.chickencoop_ac
- id: ChickenCoopRunAC_OnBasedOnTemperature
  alias: Turn on Chickencoop Run AC at 80 Degrees
  trigger:
  - platform: numeric_state
    entity_id: sensor.chicken_coop_temperature
    above: '80'
  action:
  - service: switch.turn_on
    entity_id: switch.chickencoop_run_ac
1 Like
- alias: Set chicken coop AC
  trigger:
    - platform: numeric_state
      entity_id: sensor.chicken_coop_temperature
      above: 80
    - platform: numeric_state
      entity_id: sensor.chicken_coop_temperature
      below: 75
    - platform: time
      at:
        - '07:00:00'
        - '19:00:00'
  condition:
    condition: time
    after: '07:00:00'
    before: '19:01:00'
  action:
    service: >
      {% if states('sensor.chicken_coop_temperature')|int > 79
        and now().hour in [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] %} 
          switch.turn _on
      {% else %} switch.turn_off {% endif %} 
    entity_id: switch.chickencoop_ac

If you’re on any version below 115 change service to service_template

Hello, thank you for your quick reply and for the automation. The community is awesome!
The automation turned off the switch however it did not turn on the switch “switch.chickencoop_ac”. The “sensor.chicken_coop_temperature” is now above 80 degrees and with in the condition of time but has not turned on the switch “switch.chickencoop_ac”. I am currently running version 0.115.2

Any suggestion would greatly be appreciated

Can you double check the template in the template editor?

I get the following in the template editor

switch.turn _on

This template listens for the following state changed events:

  • Entity : sensor.chicken_coop_temperature

And did the automation fire on numeric_state in your logbook?

Oh, I’ve spotted it, I accidentally put a space in switch.turn_on in the template, remove the space and it will work.

That worked! This has been solved

Really appreciate the community!!

1 Like

I had an interesting thought. Do you think it is possible to writhe this where instead of between time it is between sunrise and sunset?

- alias: Set chicken coop AC
  trigger:
    - platform: numeric_state
      entity_id: sensor.chicken_coop_temperature
      above: 80
    - platform: numeric_state
      entity_id: sensor.chicken_coop_temperature
      below: 75
    - platform: state
      entity_id: sun.sun
  action:
    service: >
      {% if states('sensor.chicken_coop_temperature')|int > 79
        and is_state('sun.sun', 'above_horizon') %} 
          switch.turn_on
      {% else %} switch.turn_off {% endif %} 
    entity_id: switch.chickencoop_ac

It’s working! Thank you very much

1 Like

Im trying to figure out how to create an automation that will turn my sons heater on (input_boolean) based on the temperature being less than or equal too 67 degrees Fahrenheit using ecobee temp sensor in his room. Also have it turn off at 70 degrees Fahrenheit and only if presence is detected and only between the hours of 8pm - 4am and only in the winter months based on outside temp which I have smart weather station for that entity as well. Im sure with all you brilliant minds in here this is probably easy for you all but for me im lost. Thank you in advanced