Explanation on how automation works

Hi there, It took me 4 days to make this work, and I think it does, but there are some questions I still have about how does this work

I have a robot lawn mower, Dolly.

one of her entities is blades_total_on_time, which i want to take advance of in order to remind me they need cleaning/mainteinance.

Screenshot 2024-10-01 at 23-18-44 Developer Tools – Home Assistant

As you can see, sensor.dolly_blades_total_on_time is giving time in minutes, but it acummulates minutes since first start.

Well, I first tried a template, but someone in this forum gave the idea of an automation and I have cerated one, which I think would do the job, I am still waiting for Dolly to do her job :grinning:

the automation looks like this

alias: NotificaciĂłn de uso de cuchillas
description: ""
trigger:
  - platform: template
    value_template: >
      {% set COT = states('sensor.dolly_blades_total_on_time') | float %} {% set
      COT2 = states('input_number.cot2') | float %} {% set result = COT - COT2
      %} {{ result >= 1440 and result < 1441 }}
condition: []
action:
  - data:
      message: Han pasado 24 horas de uso de las cuchillas. DeberĂ­as limpiarlas.
    action: notify.mobile_app_mi14
  - data:
      entity_id: input_number.cot2
      value: "{{ states('sensor.dolly_blades_total_on_time') | float }}"
    action: input_number.set_value
  - data:
      title: "Tarea: Limpieza de cuchillas"
      message: Se han registrado 24 horas de uso de las cuchillas. Recuerda limpiarlas.
    action: persistent_notification.create
mode: single
{% set COT = states('sensor.dolly_blades_total_on_time') | float %}

This sets a variable named COT which will contain the total on time for the blades

{% set
      COT2 = states('input_number.cot2') | float %}

Well, for this one, have to say that in the firs place I set COT2 = to a number, the same number sensor.dolly_blades_total_on_time was showing at that moment, but HA automatically changed this to the actual code and created a input number entity , the point was to make COT and COT2 equal so that when COT kept increasing, and the COT-COT2 op reached a fixed number (1440, that is 24 hours)({{ result >= 1440 and result < 1441 }}) The automation will send a notification.

Besides the notification, when the 1440 difference occur COT2 is equaled to COT again.

and here comes my questions.

1- how often is the system making the math?
2- I don´t know, because I am not a coder… The automation is read by the system as we read it?, I mean, the system starts in the upper side and left to right, and so on?

because if it does, then there is something I can´t understand and it is that

1 creates COT
2 creates COT2
3 creates result
4 when 1440<=result<1441
5 then notifies and makes COT2 = COT

and then?, goes back to step 1?
if it does?
what happens with {% set COT2 = states('input_number.cot2') | float %} step?

I think you are imagining auomations as something that sits there and runs all the time, but that’s not really what’s happening. I’ll try to explain.

Home Assistant uses an event-based architecture. What this means is that when various things happen (the system starts, the state of an entity changes, etc), an event is fired (also know as being published). Other parts of the system can set up listeners (aka subscribers) for the specific events they are interested in.

When you create a template trigger, what’s happening behind the scenes is that HA adds listeners for state changes to the entities you reference in the template. In your case, sensor.dolly_blades_total_on_time and input_number.cot2. When one of these values changes, that triggers an event which causes this template to be evaluated. If the template evaluates to true then the automation is triggered and starts to run the action steps. (If you had defined any conditions, they would be checked in between the automation being triggered and the action steps running.)

Your variables COT and COT2 are scoped to the context of your template, meaning that once the template is evaluated, they are discarded and not available any more. That’s ok, though, because you only need them to do the math and check if it’s been long enough to need to do maintenance.

Your template though, is more complicated than it needs to be. There’s no reason to put an upper bound on how high above 1440 the difference is because any amount higher than that is when you want this automation to run. So your template could be just:

{{ states('sensor.dolly_blades_total_on_time') - states('input_number.cot2') >= 1440 }}

Once this becomes true, the actions will run which will change input_number.cot2 to be equal to the sensor value, so states('sensor.dolly_blades_total_on_time') - states('input_number.cot2') will equal zero and the template will no longer be true. Even if you didn’t change the value of the input_number, template triggers only fire when the result of the template changes from false to true. If they stay true, that doesn’t result in more repeated running of the automation.

This isn’t accurate, by the way. The second action in your automation sets input_number.cot2 to whatever value is in sensor.dolly_blades_total_on_time. This has nothing to do with the COT and COT2 variables you created in your template, which, as I said previously, only are available while that template is being evaluated.

I hope this helps clear up some confusion!

1 Like

hey there, thaks so much, your answer is very clarifying

so, every time sensor.dolly_blades_total_on_time or input_number.cot2 change, the template is being evaluated until the math results in true.

and when that happens, and the actions are triggered… the automation starts again? and so on?

Yes

When the template’s evaluation changes from false to true the action sequence is executed. Once all the actions have been completed the automation doesn’t do anything until the next time the template’s evaluation changes from false to true.

1 Like

well, that´s what I meant.
thank you all.