Help with new automation script - TH16 temperature sensor to control sonoff R3 at time intervals

Hi all.

Finally after months of just playing with many sonoff/smartlife sensors and smart lights/curtains from my app only I have created a new docker with home assistant and portainer.

Getting to grips with it.

Now one issue is I’m having is one specific automation I want to achieve.

Th16 temp probe downstairs.
I have an sonoff R3 switch triggering 3 contactors for underfloor heating.

I want an automation for the R3 to be triggered only when
(Temp >22 and <26) and time between 15:00 and 17:00)

If it rises to 26 or above then turn the R3 off. BUT also turn it back on again if the temp falls within that time frame.

I’ve only achieved it turning on and not off. I need it to be monitoring the data from the sensor.

I need a script that will read the realtime data at intervals from the TH16 and control the R3.
I suppose I could use the ‘auto’ mode on the TH16 on/off and use that as a trigger OR read the real time temps from the sensor in manual mode.

Spent hours trying to use automation.reload, automation.turnon every minute etc. automation with a script.

There must be a way to read the temps every interval and either keep the R3 turned on or off withing specified time frames.

something like my SQL knowledge
Case when Temp => 22 AND Time between 15:00 and 17:00 THEN switch.R3 ON
WHEN Temp Temp > 26 AND Time between 15:00 and 17:00 THEN switch.R3 OFF
ELSE Turn.R3 OFF END as ‘Heating Control’

In a loop reading the temperature data sent to HA. I can see it in the graphs so the TH16 is sending the data.

Many thanks in advance

Post the actual entity_ids of all the things that need to be monitored and controlled. This will help us create examples that are tailored to your system.

Hi Taras,

Sure here you are:

sonoff TH sensor: sensor.sonoff_1000223a99_temperature
sonoff R3 switch: switch.sonoff_1000907bc4

Thanks

I’ve seen people create templates and then use a trigger set at intervals with ‘/5’ for example.
Haven’t learnt about the templates yet.

Still playing around with different ideas possibly to read if the TH sonof is switched on (from the auto mode with temp range set) and then trigger the heating.
Or just to read the temperature that is stored in the HA database somehow.

Just did a test with a lamp with simple conditiions.

alias: test light interval
description: ‘’
trigger:

  • minutes: /1
    platform: time_pattern
    condition:
  • after: ‘13:35’
    before: ‘13:53’
    condition: time
    action:
  • data: {}
    entity_id: switch.51746580840d8e63e756
    service: switch.turn_on

Added the temp. seems to wotk when adjusting the temp range and times.
Need to add in the OFF now. New automation perhaps.

I’ll wait to see if the pros here have a better solution.

  • id: ‘1588091860855’
    alias: test light interval
    description: ‘’
    trigger:
    • minutes: /1
      platform: time_pattern
      condition:
    • after: ‘13:35’
      before: ‘13:58’
      condition: time
    • above: ‘20’
      below: ‘22’
      condition: numeric_state
      entity_id: sensor.sonoff_1000223a99_temperature
      action:
    • data: {}
      entity_id: switch.51746580840d8e63e756
      service: switch.turn_on

Made some progress testing with a light which might in turn work when the light switch is changed over to the heating swtich:

  • id: ‘1588091860855’
    alias: Heating ON
    description: ‘’
    trigger:
    • minutes: /1
      platform: time_pattern
      condition:
    • after: ‘13:35’
      before: ‘15:00’
      condition: time
    • above: ‘20’
      below: ‘26’
      condition: numeric_state
      entity_id: sensor.sonoff_1000223a99_temperature
      action:
    • data: {}
      entity_id: switch.51746580840d8e63e756
      service: switch.turn_on
  • id: ‘1588093381430’
    alias: Heating OFF
    description: ‘’
    trigger:
    • minutes: /1
      platform: time_pattern
      condition:
    • after: ‘13:00’
      before: ‘15:00’
      condition: time
    • above: ‘26’
      condition: numeric_state
      entity_id: sensor.sonoff_1000223a99_temperature
      action:
    • data: {}
      entity_id: switch.51746580840d8e63e756
      service: switch.turn_off

amended it to the heating switch. see what happens at 15:00hrs today! (MUST switch off before 17:00 as this is when out electricity tariff triples at peak time)

id: ‘1588091860855’
alias: Heating ON
description: ‘’
trigger:

  • minutes: /5
    platform: time_pattern
    condition:
  • after: ‘15:00’
    before: ‘16:55’
    condition: time
  • above: ‘20’
    below: ‘25.5’
    condition: numeric_state
    entity_id: sensor.sonoff_1000223a99_temperature
    action:
  • data: {}
    entity_id: switch.sonoff_1000907bc4
    service: switch.turn_on
  • id: ‘1588093381430’
    alias: Heating OFF
    description: ‘’
    trigger:
    • minutes: /5
      platform: time_pattern
      condition:
    • after: ‘15:00’
      before: ‘16:55’
      condition: time
    • above: ‘26.5’
      condition: numeric_state
      entity_id: sensor.sonoff_1000223a99_temperature
      action:
    • data: {}
      entity_id: switch.sonoff_1000907bc4
      service: switch.turn_off

Do really what to turn on the heat “when Temp => 22”? Don’t you want to turn on the heat “when Temp < 22”? Then turn it off “when Temp > 26”?

The way I just described it will heat the room to maintain a temperature in the range of 22 and 26.

- id: '1588091860855'
  alias: Heating ON
  trigger:
    platform: numeric_state
    entity_id: sensor.sonoff_1000223a99_temperature
    below: 22
  condition:
    condition: time
    after: '15:00'
    before: '16:55'
  action:
    service: switch.turn_on
    entity_id: switch.sonoff_1000907bc4

- id: '1588093381430'
  alias: Heating OFF
  trigger:
    platform: numeric_state
    entity_id: sensor.sonoff_1000223a99_temperature
    above: 26
  condition:
    condition: time
    after: '15:00'
    before: '16:55'
  action:
    service: switch.turn_off
    entity_id: switch.sonoff_1000907bc4

There’s no need to use a 5-minute Time Pattern Trigger. The Numeric State Trigger will fire when the temperature crosses the specified threshold(s).

2 Likes

Excellent yes very true.

So even if the temperature reaches >26 before the time threshold and switches off it will switch back on again if the temperature falls below 22? The trigger is reading the temperature data intermittently?

I thought once the trigger had run it will not run again until the beginning of the time threshold. Hence the time interval

Yes.

Here’s the difference between triggers and conditions.

  • Trigger: You ask someone to monitor the street and report to you whenever they see a car pass by.
  • Condition: Examine the reported car and proceed to the Action only if it’s red and the current time is between noon and 2:00 PM.
  • Action: Do something.

In your case, you want Home Assistant to monitor the temperature sensor and report whenever the temperature crosses certain thresholds. Then you check if the report was made within a desired time range. If the condition is satisfied, you proceed to the Action.


NOTE
It’s possible to combine the two automations (shown above) into one automation. However, until you’re comfortable understanding how those two work, it’s best not to muddle things with a more complex, single automation.

1 Like

This is great! Wasn’t able to achive any of this with the simple firmware on the TH device.

This will actually be used for a floor temp sensor.
Once the tiles start heating up I’ll want them to stay between 25-26c for example. Not the air temp.
As it takes very long the the underfloor heating to get to that temp I want to keep it withing a tight range.

So within 2 or 3 time periods in a day it will heat up to 26c say and if it drops to 24.5 or 25 then switch on again.

At least I have the basic concept sorted now thanks to your help and can just tweak it now.

Thanks

One more thing, these two automations are time-constrained to operate with a period of ~2 hours. That means you have consider what could happen at the start and end of the period (notably at the end of the period).

For example, let’'s say the heat is on at 16:54 because the temperature has not yet reached 26. It reaches 26 at 16:57, triggers the second automation but the switch won’t be turned off because it’s currently later than 16:55. So the switch remains on and continues to heat the room …

Sorry if i missed something, i didn’t read the whole topic.
Why not use the generic thermostat for this?

Hey! We’re far too busy sawing wood with hammers and hammering nails with saws to listen to crazy new ideas!

/s

1 Like

:rofl:

hmm odd my automation has failed to turn on 2 days now. Where am I going wrong there?

Got to 15:38 and still nothing. Temp was reading 23.1c

  • id: ‘1588091860855’
    alias: Heating On <23.7C
    trigger:
    • below: ‘23.7’
      entity_id: sensor.sonoff_1000223a99_temperature
      platform: numeric_state
      condition:
    • after: ‘15:30’
      before: ‘16:55’
      condition: time
      action:
    • entity_id: switch.sonoff_1000907bc4
      service: switch.turn_on
  • id: ‘1588093381430’
    alias: Heating Off >23.8c
    trigger:
    • above: ‘23.8’
      entity_id: sensor.sonoff_1000223a99_temperature
      platform: numeric_state
      condition:
    • after: ‘15:15’
      before: ‘17:00’
      condition: time
      action:
    • entity_id: switch.sonoff_1000907bc4
      service: switch.turn_off

Please format your code when sharing it in the community forum. You can do this one of two ways:

Select the code and click the </> icon in the editor’s menu.

OR

Type three consecutive backquotes ``` on a separate line before your code.
Type three more backquotes on a separate line after your code.

The Numeric State Trigger fires when the value crosses the threshold.

In your example, it will trigger the moment when the sensor’s temperature decreases below 23.7. If that happens outside the condition’s time range, the action will not be executed (i.e. switch is not turned on). If the temperature continues to fall below 23.7 it will no longer trigger the Numeric State Trigger because, as explained, it only happens when the value crosses the threshold.

What you’re experiencing is what I mentioned in my previous post concerning time-constrained automations:

you have to consider what could happen at the start and end of the period

I gave an example of what could happen at the end of the time period (the heating switch remains on). You’re observing what can happen at the start, namely the heating switch remains off.

1 Like

Sorry I’m used to thinking in SQL and this would have worked in some fashion.

Changed the trigger and condition around. Would this work?


- id: '1588091860855'
  alias: Heating On <23.7C
  trigger:
  - at: '15:00'
    platform: time
  condition:
  - below: '23.7'
    condition: numeric_state
    entity_id: switch.sonoff_1000223a99
  action:
  - entity_id: switch.sonoff_1000907bc4
    service: switch.turn_on
- id: '1588093381430'
  alias: Heating Off >23.9c
  trigger:
  - above: '23.9'
    entity_id: sensor.sonoff_1000223a99_temperature
    platform: numeric_state
  condition:
  - after: '15:00'
    before: '17:00'
    condition: time
  action:
  - entity_id: switch.sonoff_1000907bc4
    service: switch.turn_off

Also got an override on the device to switch off at 17:00 in any case

Please can I have some more advice on this set up?

Added these entries to the config file but where do I go from there?

Here’s one possible way to configure the Generic Thermostat for your needs.

  • It identifies the switch used to control the heater and the sensor that reports the temperature.
  • The minimum temperature that can be set is 22 and the maximum is 26.
  • The target temperature is set to 24.
  • The ambient temperature can deviate from the target temperature by as much as +/- 0.2 degrees before the heater is activated (so that’s a ‘deadband’ of 0.4 degrees).
  • Each time the heater is turned on, it’s for no less than 5 seconds.
  • Initially, the thermostat is off.

Feel free to adjust any of the parameters.

climate:
  - platform: generic_thermostat
    name: Downstairs
    heater: switch.sonoff_1000907bc4
    target_sensor: sensor.sonoff_1000223a99_temperature
    min_temp: 22
    max_temp: 26
    ac_mode: false
    target_temp: 24
    cold_tolerance: 0.2
    hot_tolerance: 0.2
    min_cycle_duration:
      seconds: 5
    initial_hvac_mode: "off"
    away_temp: 16
    precision: 0.1

All that’s left is to create two automations, one to enable the thermostat at 15:00 and the other to disable it at 16:55 (there is a way to combine them into a single automation but, for now, this is easier to understand).

  - alias: 'Heating Enabled'
    trigger:
      platform: time
      at: '15:00'
    action:
      service: climate.turn_on
      entity_id: climate.downstairs

  - alias: 'Heating Disabled'
    trigger:
      platform: time
      at: '16:55'
    action:
      service: climate.turn_off
      entity_id: climate.downstairs
2 Likes