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.

Hi, sorry I am back with this thread.

After long time of being put aside, I yesterday realised that my input number goes back to the starting point every day and I donĀ“t know why

IĀ“ll attach a graphic where some changes are spotted, biut are manual changes made by me, after that, every day at 3am they go back to 20019

where do I have to look for to find what is causing this come back to initial value?

as you can see, at 21:28:33 I set the value to 23352, nonetheless the value returned to 20019 at 03:00, also this is not shown in the history

note: every day at 03:00 a back up is made by proxmox, maybe it has to do with a reboot?

well, I have disabled the shutoff for the backup and the number hasnĀ“t changed, so it was going back to 20019 on every reboot.

This makes me wonder

1 why to 20019
2 not very reliable that on reboot it resets itself, is this avoidable?

Did you give it an ā€˜initial: 20019ā€™ anywhere in the config for the number?

I donā€™t use the UI for this stuff but in the yaml it would look something like the above.

if so then you need to remove that line since it will set that as the value on every restart.

1 Like

sure, you are totally right, defined in the configuration yaml.

how can I set the initial value taken it from a sensor, I have tried this but, doesnĀ“t work

alias: reset COT
description: on reboot
triggers:
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - data_template:
      entity_id: sensor.dolly_blades_total_on_time
    action: input_number.set_value
mode: single

this are the deviceĀ“s atribute states.

Screenshot 2024-10-01 at 23-18-44 Developer Tools ā€“ Home Assistant

Iā€™m not entirely sure what you are trying to do but the automation wonā€™t work because you are defining the fields wrong:

you havenā€™t actually said what the entity_id of the input_number is so Iā€™ll just make something up. So assuming you want the number to get changed to the state of the sensor on every restart then it would be:

actions:
  - action: input_number.set_value
    entity_id: input_number.something
    data:
      value: "{{ states('sensor.dolly_blades_total_on_time') | float }}" # ( or | int)
1 Like

nice, done, well, sorry I havenĀ“t introduce the process to you, though it is above.

I am trying to get a notification when the blades of my mower have been runnig for 24 hours, just to make a maintenance, but it is hard for non coders. I thought it was working until I realised it was reseting itself every day, and the point is what you mentioned above, on restart, which happens every night for backupreasons, it resetted itself, so the automation wouold never happen.

letĀ“s seee now that I have changed the way HA gets its backup.

let me show you my automation, what do you think?

alias: NotificaciĆ³n de uso de cuchillas
description: ""
triggers:
  - 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 }}
    trigger: template
conditions: []
actions:
  - data:
      message: Han pasado 24 horas de uso de las cuchillas. Necesitan limpieza
      title: ATENCIƓN!!
    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
  - action: notify.mobile_app_iphone_de_pilar
    data:
      message: Han pasado 24 horas de uso de las cuchillas. Necesitan limpieza
      title: ATENCIƓN!!
  - action: notify.mobile_app_iphone_de_marcos
    data:
      title: ATENCIƓN!!
      message: Han pasado 24 horas de uso de las cuchillas. Necesitan limpieza
mode: single

If you already have a sensor that holds the value then why are you creating an additional input_number to hold the same value?

and if you remove the "initial: " setting from the input_number config then the restart automation is unnecessary.

1 Like

I want to be notified every 24 hours, i thibk i need to compare values for them to know 24 hours have passed. What other way do I have to get this?

Sorry I misunderstood the aim of what you needed to do. I didnā€™t scroll up to see the initial conversation from over 2 months ago. TBH, I didnā€™t know that what you were asking now was related to the first post from that long ago.

Youā€™re trying to get notified every time the run hours of the blades reaches 24 hours of run time.

So in that case I think you are using the easiest method.

But are you sure that the sensor will update literally every second?

your only triggering the automation if the difference between the input number and the sensor is exactly 1440 (result is greater than or = 1440 but less than 1441 is only true from 1440.0 to 1440.99).

maybe you should remove the ā€œ<1441ā€ part and then it will trigger whenever the result is greater than or equal to 1440

1 Like

well, left it this way

alias: NotificaciĆ³n de uso de cuchillas
description: ""
triggers:
  - 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 }}
    trigger: template
conditions: []
actions:
  - data:
      message: Han pasado 24 horas de uso de las cuchillas. Necesitan limpieza
      title: ATENCIƓN!!
    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
  - action: notify.mobile_app_iphone_de_pilar
    data:
      message: Han pasado 24 horas de uso de las cuchillas. Necesitan limpieza
      title: ATENCIƓN!!
  - action: notify.mobile_app_iphone_de_marcos
    data:
      title: ATENCIƓN!!
      message: Han pasado 24 horas de uso de las cuchillas. Necesitan limpieza
mode: single

letĀ“s see what happens.

1 Like