How to trigger automation BEFORE set time?

shit found the other typo

          {% set temp = states('sensor.sonoffth3_am2301_temperature') | float %}
          {% set time = as_timestamp((now().date() | string ~ 'T' ~ states('sensor.alarm_clock2_time') ~':00') | timestamp_custom('%H:%M') %}
          {{ states('sensor.time') == time and temp > -10 }}

EDIT: Aparently you found that already

They are VERY good, itā€™s just that you probably donā€™t understand how they are laid out. I help people only using the docs for just about everything. Once in a while, Iā€™ll have to go to the template editor. But I donā€™t use any of these integrations, I donā€™t use the built in automations, and I have very few devices outside switches and lights. Yet I help all sorts of people by just reading the docs.

Jinja is near identical to python aside from filters. And filters are advanced. There arenā€™t many jinja tutorials but there is about 90 million python tutorials. Iā€™d start with basic python tutorials. Learn the basics, then take a look at jinja.

1 Like

Their official documentation is goodā€¦ but it will take time, especially to remember that even if itā€™s very similar to python, it is not python.
Donā€™t think youā€™ll need both things unless youā€™re not a coder and still are very keen to master it :wink:

I can believe that, but I also guess that you speak Python and/or Jinja :smiley: For someone who does that, it is probably quite easy to see from HA docs what needs to be done.

Well, I am mainly interested in home automation, NOT in codingā€¦ Although I understand that I have to code something if (and when) I chose to go to Home Assistant, and I am still thinking I will continue with that when I get to build our new house, instead of going to much expensive KNF or ABBFree@Home -route (or whatever, those are quite easy to use but also limiting in what they can do)ā€¦ Itā€™s been over 20 years since last time I did more coding and even then it was with (Object)Pascal, and I have never done anything with Pythonā€¦

But I still think that since Lovelace automation tool is quite good already, it would ONLY need to add negative delay to be even better and if there was such an option already, this whole thread would not have been necessary :smiley:

Then you should move to node red instead. Node-red is for those who donā€™t want to code, but like understand/think in flow charts.

Actually, you just need to understand yaml to understand the non-template documentation (integrations, etc). The documentation for templates is built like an API documentation. They tell you what the methods are. They do not tell you how to use them because you need to understand code.

I used to think this for a long timeā€¦

ā€¦but now Iā€™m fully in agreement with this

ā€¦although I probably would have stopped short of putting ā€œveryā€ in capitals :wink:
The layout is not the best, but once you get used to it you (usually) know where to look.

1 Like

Ultra good?

2 Likes

Open a new Feature Request!

Node-red sounds what would suit me better, that is true, but originally when I chose my home automation software (maybe two years ago or so), I chose Home Assistant because I wanted to choose software that would be very much still in developement and have big (and helpful) community because that would mean that if something was not yet possible in that software, it probably would be sooner or later, and if have a problem, there would be an active community that could help me. So far it seems that I chose right :slight_smile: Not sure about in what situation Node-red was back then, but I donā€™t remember really considering that back then. But I did consider Domoticz, but because fanbase for that was declining, I chose HASS instead.

EDIT: Oh, there is Node-Red Add-on to Hassio, I should look into it! :slight_smile:

Good idea, I think I will do just that!

ā€œSpectacularā€ given the rate of change and the diversity of developers and the native languages thereof

Edit: and the fact it doesnā€™t cost us a penny !

"Open a new Feature Request"

Did you even think about this before posting this suggestion ?

(Gilean, sorry I replied to the wrong person)

1 Like

If you ask meā€¦ what should I had to think about? Iā€™m a bit lostā€¦ 8()

You suggested that delay be able to employ negative values.
Delay ONLY goes in action sections of automations or sequence sections of scripts.
If you apply it there where you follow any given trigger, how would apply an action two hours earlier?
If you want to apply a minus two hours to a 10:00 time set, why not just set it for 08:00 in the first place.
Gilean, clearly is new to these sorts of issues.
Iā€™ve seen your posts, you clearly have quite a good understanding.
This is what I mean about thinking about it.

Well, the OP is pretty adamant that negative delay is the thing that will save the world him from a lot of unnecessary work, and it doesnā€™t seem that we were very successful in explaining him the opposite.
For him it would be great to try opening such a FR as in the process he could a) understand things better or b) bring us a new feature.
Win-win! :wink:

p.s maybe your post will do the job
p.s.s Iā€™m not always 100% serious, itā€™s too boring :wink:

Okay, new day again for me, trying to understand some moreā€¦ But itā€™s hard because I donā€™t understand the syntax and winter will be probably over before I get to study this enough :confused: For example, I have no idea what ~ ā€˜Tā€™ ~ below actually doesā€¦

Anyway, I would need to set ā€œtimeā€ variable so that itā€™s three hours before sensor.alarm_clock2_time. I have looked at previous examples but they have built a bit differently than this ā€œsimplerā€ code and I cannot figure how to do the math here so itā€™s in right format etcā€¦

EDIT: The whole template is here:

        value_template:  >-
          {% set temp = states('sensor.sonoffth3_am2301_temperature') | float %}
          {% set time = as_timestamp(now().date() | string ~ 'T' ~ states('sensor.alarm_clock2_time') ~':00') | timestamp_custom('%H:%M') %}
          {{ states('sensor.time') == time and temp > -10 }}

How about something like this:

- alias: 'Turn Car on'
  initial_state: on
  trigger:
    platform: time_pattern
    minutes: '/1'
    seconds: '0'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: >
          {% set time = (as_timestamp(now()) - 180 * 60) | timestamp_custom('%H:%M') %}
          {% set temp = states('sensor.sonoffth3_am2301_temperature') | float %}
          {{ time == states('sensor.alarm_clock_3_time')[0:5] and temp < -10 }}
      - condition: template
        value_template: >
          {% set time = (as_timestamp(now()) - 120 * 60) | timestamp_custom('%H:%M') %}
          {% set temp = states('sensor.sonoffth3_am2301_temperature') | float %}
          {{ time == states('sensor.alarm_clock_3_time')[0:5] and temp > -10 and temp < -5 }}
      - condition: template
        value_template: >
          {% set time = (as_timestamp(now()) - 60 * 60) | timestamp_custom('%H:%M') %}
          {% set temp = states('sensor.sonoffth3_am2301_temperature') | float %}
          {{ time == states('sensor.alarm_clock_3_time')[0:5] and temp > -5 and temp < 2}}
  action:
    - service: switch.turn_on
      entity_id: switch.sonoffpow3_2

This should trigger every minute and turn on the switch only if
(time = alarm clock - 3 hours AND temp < -10)
OR
(time = alarm clock - 2 hours AND temp > -10 AND temp < -5)
OR
(time = alarm clock - 1 hour AND temp > -5 AND temp < 2)

Is this what you want? Please note I didnā€™t test this as Iā€™m not at home right now.

2 Likes

Thank you Burningstone, you are a star! This looks like working code except shouldnā€™t it do the math ( 60*60 etc) from sensor.alarm_clock3_time and not from current time and then in the third row compare current time to ā€œtimeā€ variable? Tried to do so but didnā€™t succeedā€¦ :confused:

For example if now is 10:59, and I set sensor.alarm_clock3_time as 12:00 (to see if magic happens within minute), I can see from template editor that ā€œtimeā€ variable is 9:59.

Yeah you are right, mistake from my side. Just change the - 180 * 60 to + 180 * 60 for the 3 conditions, then it should work.

1 Like

Great, now the template editor returns ā€œtrueā€ when the time is exactly one hour before sensor.alarm_clock3_time (since temperature is -1.4 C atm) :slight_smile:

One more thingā€¦ Now I know that delay is in action part of automation, but how the heater will turn off three, two or one hours after it has turned on depending on situation?

Hmmm, wouldnā€™t it be easier to just turn off the heater when current time == alarm clock time? If I understand correctly, this is the behaviour you want?