How to trigger automation depending time and sensor-value

I’m confused about an automation for my little rabbits :woozy_face:

My rabbits live outside and have a heat lamp for very cold nights.
I try to turn on the lamp wiht an automation under following conditions:

  • lamp should turn on only between 19 pm and 7 am
  • lamp should tunr on only if temparature is under 5 degrees for 10 minutes.

so I have this automation:

  alias: Wärmemlampe Hasenstall automatisch einschalten
  description: Wärmelampe nach 19:30 Uhr und unter 5° einschalten
  trigger:
  - platform: numeric_state
    entity_id: sensor.shelly_ht_hasenstall_temp
    below: '5'
    for: 00:10:00
  condition:
  - condition: time
    after: '19:30'
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.mystrom_hasenstall
  mode: single

questions:
am I right, that with this code

  • lamp will not turn on, when temperature falls below 5° before 19:30 (7.30pm)
  • lamp will not turn on, when temperature falls below 5° after 00:00

how could I do it correctly? :thinking:

PS. at 7am I have another automation that turns off the lamp

Hey,

yes, your assumptions are correct.
The trigger is the thing that fires the automation, so every time the temperature drops below 5°, the automation fires.
However, as you correctly pointed out, the action will only execute if the condition is met.
There are other ways to to the thing you want to achieve, for example you could have the same trigger and condition - but have a wait action after turning on the lamp. You can set the wait template so that it will wait until a temperature threshold is reached (or a timeout is reached) and then turn it off.

If you want to do the whole thing more professionally you can even write a generic thermostat which will give you a nice UI item as well.
In that case Home Assistant will take care of the heating. Using services like climate.turn_on and/or climate.turn_off you can restrict it to certain times as well.

Hey, ja, deine Vermutungen sind korrekt.
Der Trigger löst die Automatisierung aus, also jedes mal wenn die Temperatur unter 5° fällt, wird die Automatisierung ausgelöst.
Allerdings, wie du bereits angemerkt hast, die action Befehle werden nur ausgeführt, wenn auch die condition korrekt ist (also nur ab 19:30).
Es gibt auch andere Wege das zu erreichen, bspw. ein wait template, das kannst du nach der ersten action einfügen, sodass Home Assistant wartet bis eine gewisse Temperatur erreicht wird und dann abschaltet.

Wenn du das ganze noch etwas professioneller gestalten willst, kannst du auch ein generic thermostat verwenden, das ist dann wie eine richtige Heizungskontrolle, Home Assistant schaut sich die Temperatur an und schaltet automatisch die Heizung an und aus. Mit Services wie climate.turn_on und climate.turn_off kannst du das ganze mit Automatisierungen auch zeitlich einschränken :slight_smile:

1 Like

Look at the problem from another perspective.
Set the trigger and conditions , so that you test for a positive, i.e. the lamp turns On, when:

trigger =

  • time between 1900 to 0700
  • temp <5C for 10 minutes

also I dont understand your statements:

am I right, that with this code

  • lamp will not turn on, when temperature falls below 5° before 19:30 (7.30pm)

  • lamp will not turn on, when temperature falls below 5° after 00:00

this contradicts your original requirement?

confused!

Yes, what you wrote is what i want, logical:

but how do I implement this in HA?

with my actual code, I asume that it doesn’t work , that’s why my statement :wink:

Thanks for quick reply and hints :grinning:
Interesting, I didnt knew generetic thermostat, will have a look!

You can just add a before: '07:00' to your condition.

Time condition windows can span across the midnight threshold if both after and before keys are used.

Time condition

1 Like

your trigger may be correct.

in condition, try:

condition:
    - condition: time
      after: '19:00:00'
      before: '07:00:00'
     - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun

assuming you want to do this every day. Remove days not required, if needed.

Your trigger is another issue.
With your current config, HA will test when sensor.shelly_ht_hasenstall_temp changes and only for a duration of 10 mins. I am not sure this is what you want. You will only get a trigger when temp is < 5C and only every 10 mins. The resolution is one trigger at most every 10 minutes (providing temp < 5C) . This may be ok? I dont know what other influences you have that can alter the ambient temp - if it is just the external weather then you will be ok I guess.

Thanks :smiley:

This is now my code, I think this should work:

  alias: Wärmemlampe Hasenstall automatisch einschalten
  description: Wärmelampe nach 19:30 Uhr und unter 5° einschalten
  trigger:
  - platform: time
    at: '19:30'
  - platform: numeric_state
    entity_id: sensor.shelly_ht_hasenstall_temp
    below: '5'
    for: 00:10
  condition:
  - condition: and
    conditions:
    - condition: time
      after: '19:30'
      before: '07:00'
    - condition: numeric_state
      entity_id: sensor.shelly_ht_hasenstall_temp
      below: '5'
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.mystrom_hasenstall
  mode: single

I don’t think it will.

your time condition is not correct.

All times are relative to midnight. So your condition says that the time has to be (between 1930 and midnight) and also (between midnight and 0700) at the same time. That’s not possible.

you need to make those two time conditions separate and then ‘or’ them:

condition:
   - condition: numeric_state
      entity_id: sensor.shelly_ht_hasenstall_temp
      below: '5'
  - condition: or
    conditions:
    - condition: time
      after: '19:30'
    - condition: time
      before: '07:00'

It will, as I said 3 posts above:

Time condition windows can span across the midnight threshold if both after and before keys are used.

Time condition

1 Like

hmm…i wonder when that changed?

Maybe before 2017? https://github.com/home-assistant/home-assistant.io/blob/481320128f6c46b624415ae0d55b09d760aa4df0/source/_docs/scripts/conditions.markdown

1 Like

well, I’ll be…

I guess that’s been one of those “common knowledge” things that “everybody just knew” forever. At least it was for me.

Thanks for clearing that up.