How to trigger automation BEFORE set time?

Ah yes, exactly! :slight_smile: Is it possible to put in action part of automation, or should I just create another automation that handles turning off the heater if current time is alarm clock time?

It is possible to have it in the same automation, but I would suggest creating a second automation.

Will do, it will keep this simpler I think :slight_smile: Thank you a lot!

One thing though: I have read somewhere that it’s not “best practice” to have automations to trigger every minute (or check conditions more like), right? But to me most important thing is that I get this beast finally working, it’s not as this automation was clogging my Raspberry, even if (and when) I will create another of these so that both of our car heaters are covered :slight_smile:

It would be “better” to have an automation that only triggers 3 hours, 2 hours and 1 hour before the clock, but I can’t think of an easy way to have this in one automation, you could add a trigger for each of these and keep the conditions, but I think this would make it more complex than it needs to be. And anyway this should not affect your performance at all.

That’s what I thought as well. I rather have it easy than more complex than it’s necessary :slight_smile: Earlier I just had three automations, now I get by with two (one for turning on the heater, and another one to turn it off). That’s fine by me, especially since the code is “cleaner” than what I had before! :slight_smile:

Ah ‘Glasshopper’ (1970’s TV reference you may not get) you give me pride in your accomplishments but also humour at your naivety. This too can be done with a service template.
You prove there is life in the old Mutt yet :rofl:
Let me get to a workstation …

Though, what I’d suggested to Gilean was that as we are triggering every minute to test, we even out the granularity of the response, save fuel/energy and do it dynamically.
Ie
We don’t consider any case before 180 mins before set time, we turn off at set time and at any time between we calculate if the heater needs switching.
160 - (Temp * -16) = minutes after the 3hrs before (sounds weird)
So let’s say it’s -10 degrees
160 - (-10 * - 16) = 0 so switch now (ie set time - 180 + 0) 180 mins of heat
-5 degrees : -
160 - (-5 * - 16) = 80 so switch after 80 mins (ie set time - 180 + 80) 100 mins of heat
0 degrees
160 - (0 * - 16) = 160 so switch after 160 mins (ie set time - 180 +160) 20 mins of heat
1 degree
160 - (1 * - 16) = 176 so switch after 176 mins (ie set time - 180 +176) 4 mins of heat
2 degrees
(160 - (2 * - 16) = 192 so is outside time to switch - ignored, 0 mins of heat

This is dynamic, done minute by minute so will adapt to changing weather and assuming linear heat gain should always achieve the same final temp at ‘set time’

Edit: note the ‘funny’ formula is because even at 0 degrees he wanted ‘some heating’ (must be a namby pamby southerner’ :rofl: )
This will also eliminate two of the three evaluations but add a little complication as we are also switching off

1 Like

Definitely too young to get this reference :rofl:

I know that it is possible to do this. To be honest, I just didn’t want to spend more time and make it more complex, because (I think) OPs needs were covered with my sample code. If I were at home right now, I’d probably try to figure it out, however at work it’s a bit hard (and I should be working anyway :rofl:) to do this.

I really like your suggestion of making this whole thing more dynamic and I can’t wait to see your solution :slight_smile:

I like the word “dynamic” :slight_smile:

Well, considering that I live in Finland, I think it’s safe to say I am not “southerner” except from reindeer’s point of view :joy: Anyway, the official reason I want heater still work up until +2C is that “car people” say it’s good for your car, actually you should heat your car always if it’s under +5C but… The unofficial reason is that I also have Defa interior heater that takes care of ice from windshield and moisture if it’s near or just above zero. :slight_smile:

Understood.
If you read burnings note it’s also true that you can ‘overcomplicate’ things.
So just stop if things are going too far.
You have to keep and maintain this stuff after all.
But I think given the course of the above thread you know when to call it a day.
This is now an interlectual excersise for burning as he’s relatively new to this (hard to believe isn’t it ? And I’ve already told him that I’ll be seeking his advice soon :rofl: )
In the UK there is a bridge over the Tyne (River) it was put up for the millennium. It is said that on its southern end it has 8 coats of protective paint. On the northern side, it has ‘none’ “cos its not a pufta” (‘pufta’ is a derogatory term, in this case used to mean ‘namby pamby’ but it ‘can’ be used in a homophobic context also, so thinking about this I should have found another term but this use is authentic ‘geordie’).
Geordies (people from this region) are renown for being tough and often go out in sub-zero temperatures ‘without a coat’

Edit: I also believe, that if we give you a formula and an implementation, you will understand it well enough to tweak the formula to your needs given some time working with it and judging the resultant temperature.

2 Likes

all this is doing is adding strings together safely.

an alternative would have been to use + signs.

(now().date() | string returns the date in this format 2020-30-1
now().date() | string ~ 'T' just adds T at the end for 2020-30-1T
now().date() | string ~ 'T' ~ states('sensor.alarm_clock2_time') we just keep adding to it, ending up with 2020-30-1T08:00
now().date() | string ~ 'T' ~ states('sensor.alarm_clock2_time') ~':00' adding more, ending with a format that as_timestamp can parse naturally: 2020-30-1T08:00:00.

You’d get the same thing with

now().date() | string + 'T' + states('sensor.alarm_clock2_time') + ':00'
3 Likes

Ahha! Now that’s informative post, thank you! I had no idea that as_timestamp needed such format for it to work! Some other solutions have been converting time to integer, then to Unix timestamps, then do the math and then converting back to “normal” time format. I think my old solution used that method but it was very complex…

Yah this is probably one of the easier ways to get a usable datetime objects. But you gotta know the format.

The most astonishing part was that he apparently wrote up that code without testing, and it was the only code that worked straight out of box (except for minor typo with sensor name but that one I could fix myself :smiley: ). I mean, it’s not that I don’t appreciate people helping me because I really do, but for someone who doesn’t understand code much, it can be quite overwhelming to see various different methods of achieving things, and when none of them work because of some minor in syntax and I cannot see it (and template editor is just saying “unknown error” or something like that) :slight_smile:

That is true, except if the code has lots of conversions and () | {* characters, then it’s different story… And unfortunately for this automation, there just has to be those, more or less :slight_smile: For example, in that Burningstone’s conditions, there is [0:5] that I have no idea why and what that does… But still, that code is simple enough for me to adjust it as needed :slight_smile:

That’s one of mine,
It allows you to compare times as strings rather than as timestamps

EDIT: this ONLY works with times in the sameday or if you test for a wrap around (over midnight) when comparing two values

It takes the first 5 characters of the string, maybe this isn’t needed, however I didn’t know if your alarm clock sensor has this “00:00:00” or this “00:00” format.

1 Like

To be precise it takes the beginning of the string from position 0 _ upto but not including position 5
(in effect the first 5)
BUT
you can also do it in reverse with the end of the string, this is useful if your time also has date as this will work with both
[-8:-3]
so
{{ ‘12:34:45’ [-8:-3] }} returns 12:34

Edit This last was robbed kindly donated from pnbruckner

2 Likes

Jinja (the language used for templates in home assistant) is a template engine for Python, so most of things will work the same/similar as in Python.

The [0:5] is called string slicing in python, you can read up on it here in case you are interested.

1 Like

It’s things like these that make me wish there was somewhere a website with lots of (Jinja/Python/YAML) code and user could click anywhere and you could see a small tooltip explaining what that thing does. For example, your explanation with this example would have been perfect for that :slight_smile:

Yeah, but it would need someone like @petro to do it, and his contention is that it’s just jinja wrapped python (true) but it would be nice to have a site with standard examples for various template usages.
Instead “these are just things we pluck from the hedgerows of experience”.
And I also think that petro has enough on his plate :rofl:

And you’d be amazed at how differently some people attack the same problems with different approaches, and we learn new wrinkles all the time

1 Like

you don’t need to include the zero [:5] is the same

Jinja is based on python, don’t know if it’s a wrapped version of it.