Turn on device based on workday, time and temperature

Hi.

I want to turn on a heating panel only if it is a workday, between 6:00 am and 9:00 pm and the temperature is < 22 ºC. Also, I want to turn it off if the temperature is => 22ºC.

I started with this, but not sure how to include the other conditions :confused:

automation:
  alias: Turn on heater on workdays
  trigger:
    platform: time
    at: '06:00:00'
  condition:
    condition: state
    entity_id: 'binary_sensor.workday_sensor'
    state: 'on'
  action:
    service: switch.panel_on
    entity_id: switch.heater

The first part would be to determine what you want the trigger to be. It sounds like the temp dropping below 22 should be the trigger. If that sounds right to you, do a numeric state trigger with below 22, and add two conditions: (1) it being a workday and (2) the time is between 06:00 and 21:00. You can keep your time trigger in there in case the temp is already below 22 when it hits 06:00. Then make a second automation with a numeric state of above 22 that turns it off.

This is a automation that I wrote as a alarm that lets you pick any day of the
week on or off.
Overkill for what you are asking but you should be able to adapt it to work for you.

Add this Input Boolean into your configuration.yaml

input_boolean:    
  monday:
    name: 1_Monday
#    initial: off
  tuesday:
    name: 2_Tuesday
#    initial: off
  wednesday:
    name: 3_Wednesday
#    initial: off
  thursday:
    name: 4_Thursday
#    initial: off
  friday:
    name: 5_Friday
#    initial: off
  saturday:
    name: 6_Saturday
#    initial: off
  sunday:
    name: 7_Sunday
#    initial: off



- id: '1535751230858'
  alias: Alarm Clock Work Days
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == (states.input_datetime.only_time.attributes.timestamp
      | int | timestamp_custom(''%H:%M'', False)) }}'
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Mon") and (states.input_boolean.monday.state=="on")}}'
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Tue") and (states.input_boolean.tuesday.state=="on")}}'
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Wed") and (states.input_boolean.wednesday.state=="on")}}'
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Thu") and (states.input_boolean.thursday.state=="on")}}'
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Fri") and (states.input_boolean.friday.state=="on")}}'
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Sat") and (states.input_boolean.saturday.state=="on")}}'
    - condition: template
      value_template: '{{ (now().strftime("%a") == "Sun") and (states.input_boolean.sunday.state=="on")}}'
  action:
  - data:
      brightness_pct: '20'
    entity_id:
    - light.sengled_e12n14_0309cda5_1
    - light.sengled_e11g13_03092ed4_1
    service: light.turn_on
  - data:
      entity_id: media_player.ccaudio_media
      volume_level: '0.44'
    service: media_player.volume_set
  - data_template:
      message: Time to get out of bed! Todays temperature will be a high
        of {{states.sensor.y_temperature_max.state}}  and a low of {{states.sensor.y_temperature_min.state}}
        Weather conditions will be {{states.sensor.owm_forecast.state}}
    entity_id: media_player.ccaudio_media
    service: tts.google_say
  - delay: 00:00:15
  - data:
      entity_id: media_player.runeaudio_mpd
      volume_level: '0.60'
    service: media_player.volume_set
  - data:
      entity_id: media_player.runeaudio_mpd
    service: media_player.media_play
  - delay: 00:04:00
  - data:
      entity_id: media_player.runeaudio_mpd
      volume_level: '0.55'
    service: media_player.volume_set
  - data:
      entity_id: media_player.roku_yn0063324324
    service: media_player.turn_on
  - data:
      entity_id: media_player.roku_yn0063324324
      source: AT&T TV
    service: media_player.select_source

Probably two automations would be simplest. One for on, one for off.

automation:
- alias: Turn on heater on workdays
  trigger:
    - platform: state
      entity_id: sensor.your_temperature_sensor_here
      below: 22
    - platform: time
      at: '06:00:00'
  condition:
    - condition: state
      entity_id: binary_sensor.workday_sensor
      state: 'on'
    - condition: time
      after: '05:59:59'
      before:'09:00:00'
    - platform: state
      entity_id: sensor.your_temperature_sensor_here
      below: 22
  action:
    service: switch.turn_on
    entity_id: switch.heater

- alias: Turn on heater off workdays
  trigger:
    - platform: state
      entity_id: sensor.your_temperature_sensor_here
      above: 22
    - platform: time
      at: '09:00:00'
  condition:
    - condition: state
      entity_id: binary_sensor.workday_sensor
      state: 'on'
    - condition: time  # in case of temperature trigger, only switch off during these times
      after: '05:59:59'
      before:'09:00:02'
  action:
    service: switch.turn_off
    entity_id: switch.heater
1 Like

Don’t forget about the additional time trigger at 06:00 in the first automation to turn the switch on if it’s already below 22 at that time (if that’s what OP needs which I’m assuming is the case).

1 Like

Fixed.

Thanks for the reply. Well try it and let you know.

BTW, will this be valid as a trigger? https://esphome.io/components/sensor/index.html#on-value

Let’s say the sensor value changes from 22 to 21.9, then on-value will trigger the automation (with its conditions). Something I read yesterday and will also explore.

on_value:
      then:
        - light.turn_on:  <<< Replace this with "exec automation"??

No. That is for esphome not home assistant.

I had this working fine for almost two months, but recently I noticed the heating panel was not turning off a 9pm. Here’s the code:

- id: '1578511179677'
  alias: Panel Oficina - ON
  trigger:
  - platform: time
    at: '06:00:00'
  - platform: numeric_state
    entity_id: sensor.temperatura_oficina
    below: 21.5
  condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: time
    after: '05:59:59'
    before: '21:00:00'
  - condition: numeric_state
    entity_id: sensor.temperatura_oficina
    below: 21.5
  action:
  - service: switch.turn_on
    entity_id: switch.panel_oficina

- id: '1578511179678'
  alias: Panel Oficina - OFF
  trigger:
  - platform: numeric_state
    entity_id: sensor.temperatura_oficina
    above: 21.5
  - platform: time
    at: '21:00:00'
  condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: time
    after: '05:59:59'
    before: '21:00:00'
  action:
  - service: switch.turn_off
    entity_id: switch.panel_oficina

Any hint on where to start looking for errors?

You don’t have an initial state set for the automation, so I’d check that the automation is still on

Is this what you mean?

image

As per the docs:

When you create a new automation, it will be enabled unless you explicitly add initial_state: false to it or turn it off manually via UI/another automation/developer tools.

I’ll add

initial_state: true

just in case