Templating, first steps

Hi there, I am trying to make my first template.

I have a robot mower, integrated into home assistant.
one of the entities tells how many hours the robot blades have been working, and I am interested in being notified when the blades work for a X time, lets say 24 hours / 1440min.

The entity is as follows

they have been on for 19905 min.

Well, let`s go, first I will try explain whats my idea

1- i have the Current On Time wich is 19905, so i think I have to “say” to the template, lets call it "declare" (is that correct?) that COT = 19905 (dont know if you have to specify the number or you can otherwise equal COT to the sensor itself

COT = sensor.dolly_blades_total_on_time

I will declare a second COT, wich will be updating every time the specified time has been completed

COT2 = 19905

2- I want to be notified when the blades had been working for 24 hours from now , 1440 minutes
so when COT = 19905+1440, then notify me and add a to do list entry

3- now COT2 must be 19905+1440, how do I do that?

4- return to step 2

Now, I am going to the template section on developer tools, and using the default provided template, start, let`s call it “coding” :blush:

{## Imitate available variables: ##}
{% set my_test_json = {
  "temperature": "25",
  "unit": "°C"
} %}
{% set COT = 
{{ states("sensor.dolly_blades_current_on_time", with_unit=True) }}
 %}


The temperature is {{ my_test_json.temperature }} {{ my_test_json.unit }}.


{% if is_state("sun.sun", "above_horizon") -%}
  The sun rose {{ relative_time(states.sun.sun.last_changed) }} ago.
{%- else -%}
  The sun will rise at {{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_local }}.
{%- endif %}

For loop example getting entity values in the weather domain:

{% for state in states.weather -%}
  {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
  {{ state.name | lower }} is {{state.state_with_unit}}
{%- endfor %}.


this results in the following error

TemplateSyntaxError: expected token ‘:’, got ‘}’

note for the admins

by the way, don`t know if this is the correct place for this kind of question.

I am going with the assumption you want to learn to use templating and help you with that, because there are ways to do calculations on how long something is used without needing to.

When you are already in a {% %} block, you cannot use a {{ }} block, nor do you need to. This is fixed in the next example, but be sure to try and understand the extra code I added:

{## Imitate available variables: ##}
{% set my_test_json = {
  "temperature": "25",
  "unit": "°C"
} %}
{% set COT = states("sensor.dolly_blades_current_on_time", with_unit=True) %}

The temperature is {{ my_test_json.temperature }} {{ my_test_json.unit }}.

The COT is now a string, with its unit inside it: {{ COT }} 
This makes it hard to get the number part and do calculations with it. 
States are always a string, so you need to convert them to numbers.
When the state is not a number, 0 is used as the default in the next example:

{% set COT = states("sensor.dolly_blades_current_on_time") | float(0) %}
The COT is now a float: {{ COT }} 
You can calculate with it: {{ COT + 10 }} 
And this is its unit: {{ state_attr("sensor.dolly_blades_current_on_time","unit_of_measurement") }}

Using json is possible, but since yaml is highly linked to Pyton, it is not the most obvious choice. In Python, a similar construct is a Dictionary. Does your device use json in its attributes, or is this your choice?

As it happens, I wrote a guide a while back on adding tasks for maintenance jobs. It might be of help to you or serve as inspiration:

1 Like

Create a template sensor helper with a state template like this:

{{ states('sensor.dolly_blades_current_on_time')|int(0) // 1440 }}

That will give you an integer number of days that the blades have run. // is the integer division operator, documented here. That page is the first link at the top of Developer Tools / Template.

image

Then set up an automation to trigger off a state change to that new sensor and send you a notification.

1 Like

Hi, first things first, thanks for your reply, maybe I miss explained some things,

I try once more.

In the code I copy/pasted, there is only a row I have created myself, wich is

{% set COT = 
{{ states("sensor.dolly_blades_current_on_time", with_unit=True) }}
 %}

all the other code is the defaul template offered by HA, I just left it there in case it would guide me through.

don`t know if I made myself clear this way?

I know, that is why I cut most of that out. Have you compared your code with mine, and looked what I put below to help you make the most out of your COT variable?

1 Like

Hi there @Troon , thaks for your help, wow, so just with this helper, I can get the on time for that blades…

seems so much easier this way than mine, Iĺl give it a try and come back.

thnak you

well, I retried because I read about all the JSON stuff, wich I really, in the first place, don`t need.

Iĺl give a try just to understand, though @Troon`s answer seems to be making all the task easier to get going, what do you think?

Depends whether you’re looking to learn in-depth templating (as per topic title) or sort your mower notification out…

1 Like

well, in the first place, as my signature says, I am curious, so want to learn, but try things out and watch them work, is like fresh air.

so, as per said by @Edwin_D , I just modified the code and left it this way

{## Imitate available variables: ##}
{% set my_test_json = {
  "temperature": "25",
  "unit": "°C"
} %}
{% set COT = states("sensor.dolly_blades_current_on_time", with_unit=True) %}

The temperature is {{ my_test_json.temperature }} {{ my_test_json.unit }}.

The COT is now a string, with its unit inside it: {{ COT }} 
This makes it hard to get the number part and do calculations with it. 
States are always a string, so you need to convert them to numbers.
When the state is not a number, 0 is used as the default in the next example:

{% set COT = states("sensor.dolly_blades_current_on_time") | float(0) %}
The COT is now a float: {{ COT }} 
You can calculate with it: {{ COT + 10 }} 
And this is its unit: {{ state_attr("sensor.dolly_blades_current_on_time","unit_of_measurement") }}

So step 1 would be

1- declare COT, variable?. {% set COT = states("sensor.dolly_blades_current_on_time") | float(0) %}
   declare COT2. {% set COT2 = 19905| float(0) %}

step 2

I think that now I can make something like

when COT-COT2 = 24 then notify
and set COT 2 = COT, isn`t it?

Yes, something like that. Of course, the template now outputs a lot of text for debugging and educational purposes. You’d need to remove that for the template to be of use. You will need to output just the value and store it somewhere for the automation to trigger on.

But first you’ll need to decide what the starting point of your usage measurement will be. Troon’s example will not look at when the task is completed, it will just allow you to generate a todo each xx time of use. If you are late with completing the task, the next one will come too soon. If that is fine by you, his template is as short and simple as it can get.

If you want to use task completion as your starting point, it will be harder. You could then take a look at the examples I wrote down. That is what I did there for three different ways of counting usage. It looks at task completion to pick the moment when you start counting. you could adopt it to incorporate your COT calculation.

1 Like

Of course, and until the template will be fully working I find it very helpful and let it there.

sorry dont understand what you mean with

I was taking a look at the manual, specifically to if statement and comparisons, i just got clear that COT-COT2==24, outputs (boolean?) true or false

now I think I could take the true output to start the notification part

but, how do have to write the code which makes something when the condition is met?

how do I start the notification + reset COT2 when COT-COT2==24 ???

{%if True%}{{COT-COT2==24}}{{ notify.mobile_app_mi14 }}{%endif%}

It sounds like you need Template Trigger.

If you store the usage count at which the task should be done in a sensor, you can trigger on when the real use count is higher using a numercal slate trigger goes above that value.

When the task is completed, you need to set the new target usage.

Hi, just re- reading all this stuff to get it. And I dont get what you mean by starting point of your usage measurement, could you explain in other terms? Is all about my english, sorry.

Well, I didnt state that I just only need to be notified that the blades need cleaning, that’s all. Not important stuff, but as I said, I came to learn, and for me is a good chance to do so.

I understood that @Troon 's is easier but I think the hard way will be more profitable for me.

Your usage count in the device keeps increasing. If you want to do something after 24 hours of use, you need to pick a point where to start counting, In your case 19905. So you want to do something at 19905+24=19929.

But if you create a template sensor in HA to do the calculation, it will do the calculation using the current usage count as a starting point all the time. If you use the device for one hour, the 19905 increases to 19906 and the sensor to do the calculation will go to 19905+24=19930. That is not what you want, because the target moves as fast as the usage count and you will never reach it.

So what I mean by picking a starting point, is that you need to freeze the starting point / end point so the target to do something remains at 19904+24=19928. And when you reach that number and it is time to start a new 24 our cycle, only then you will need to do the calculation again. Then you’ll use the current usage count (if you immediately do once the counter hits 19924, then the new target should be 19929+24=19953).

Troons version will work for the example above. It will work less well if you are 5 hours late to do what you needed to do. Because then your starting point would be 19929+5=19934 and the new target for the todo would be 19934+24=19958. You need to store either the starting point or the end point of the 24 hours somewhere. And then reset those when the new 24 hours counting starts again.

understood.

my idea creating COT2 was preciselly that, setting the start

in my setup, as you can see I set COT2 = 19905, which is equal to COT, today dolly will eat some grass, lets say 75 minutes.

well then COT will be 19980 and COT2 will still be 19905
so what I am looking is that 1440 minutes difference betwen them

is taht right?