Trying to understand template helpers

I know I may offend a lot of people for asking this basic question, but I am stuck and I don’t know how to proceed.
I am trying to create a helper in Home Assistant of the Template type and with the subtype as number.
I watched numerous YouTube videos about templates, YAML and Jinja , but I simply can’t get the right syntax under my skin. I tried to copy a simple template from Dev tools into the template “field”, but not even that works. Please forgive me and provide me with a simple example of what to enter in the “State template” field.

There’s no reason why any one should be offended by your legitimate question. If your sensor is a number, then the template should return only the number. The unit, etc. are defined in the entity itself.
So, for instance, this should work. The template should do the calculation for you and return the result:

{{ 25 / 5 }}

The template you have used defines a variable called my_test_json but doesn’t output anything.

If you put that template into Dev Tools / Template, it won’t output anything:

If you add a line like this, from the example:

{% set my_test_json = {
  "temperature": 25,
  "unit": "°C"
} %}
{{ my_test_json.temperature }}

it will return 25:

Try the two links at the top of the Dev Tools / Template page. Those are the “real” references. For example, look at the end of the very first section of the Jinja link:

Your attempt only contains a Statement, but no Expresssion — which is what I added.

Explain what you want to achieve, and you’ll get help here :slight_smile: .

Speak for yourself. I’ve only just managed to calm myself down! :grin:

1 Like

Feel free to be offended by my remark. I can handle it :rofl:

1 Like

ps. I’m offended that your answer is sooo much better. Please refrain from outdoing me in the future :wink:

Thank you so much!
However it doesn’t help that much.
I am basically asking for a simple example on how to use the Helper, Template, type: “Number” ?
I don’t understand the error: “required key not provided @ data[‘set_value’]. Got None” .

The reason for an error is given in our answers. The template should return a number, because it is meant to create a number helper. Your template does not return a number, but defines a json variable without returning any value. Hence the error that there is no value to set the helper to.

Both our examples give a way to return a number. The example you gave does not make clear what you intend to accomplish. So we cannot give any better examples of working templates that fit your goal without a bit more context. For instance, where should the value in the number helper come from?

Note that for a number template entity, you should also add an action. Without that you will also get errors. A number helper has a UI to enter a number, therefore you should provide an action to specify where that number should go. If it is your intent to just have an input field and HA should remember the value, create an input_number instead. If it is your intent to have just a number and no way to edit it, create a helper template sensor instead.

It’s complaining that you haven’t set an action yet. In other words, the error is unrelated to the template you entered in the “State template” field.

When I tried to create a Template Number helper, before I even entered any information it already displayed the error message. That seems like a bug to me. It should complain only if I try saving the helper but overlook to add an action.

1 Like

Your last sentence just nailed my problem. I tried to create a template
sensor, but thought it would be nicer to use the “helper” function in HA.
Now I have another problem. I can not delete the faulty “number helper” from my list of other helpers :frowning:

You can still create one from the gui, it is just a different helper. But it is weird you cannot delete the wrong one. Did you create one in yaml? Those need to be removed there and reboot (not just reload template sensors).

It wasn’t until I wrote it that I realized how confusing the helper names are. There are three variants to choose from, and the names kind of steer you in the wrong direction most of the time.

I can manage, edit and delete any helpers that are created without errors. But this one I put a screendump of has “errors” and therefore I don’t have an option to delete it.
I know I can directly edit the system file that stores all helpers and sensors, but that is tricky and may cause a disaster if I do it wrong.

Where do you still see it? Because I don ot think you can save a helper that has serious errors. What do you see when you cannot delete it?

I see this in the menu for helpers…

Have you tried a full refresh, or closing the tab and opening it again?? I sometimes see that too lately because the view checks it too soon, and then does not check again

Oh yes, I tried both. I may need to reboot HA, but I am running a 5 days timer right now so I guess I have to wait and try a full reboot/restart in a couple of days.

If it is a timer helper and you checked the ‘resture timer on reboot’ it will survive the reboot. Not sure what happens if you turn it on on a running timer though.

For things taking that long, a sensor with the expected end time might also be a good solution, you cannot always prevent reboots. I’d go nuts if I had to wait 5 days for my changes :rofl: If Every reboot would be 5 days between, I would have died of old age before I’d be where I am now.

You are right that a restart will not affect the timer. But I am not sure about a full reboot of HA.
I am running some rock tumbling for 7 days and using a timer and a utility meter to keep track of energy costs.

If the utility meter source count is outside HA (like in a DSMR meter) the utility meter will not miss anything, it will just read the new value from the meter after the reboot. Pulse counters is another matter though.

Not much causes a disaster in Home Assistant if you have a backup to restore.

There is an old thread here where some other users were experiencing issues removing template helpers, admittedly they were not ‘malformed’, but some things do hang around until you remove the entity from the recorder (Developer Tools → Actions → Purge Entities)

Select your broken template sensor entity
If not selectable try one of the following

  • type its entity_id into the box and press ENTER
  • click the GO TO YAML MODE button and edit the action YAML
action: recorder.purge_entities
data:
  entity_id:
    - sensor.my_broken_template_sensor

Thank you for all your efforts.
You last solution does not work because the helper was not recognized as an entity because of errors in the entered data fields (like in my example screenshot).
I think I need a working example of a valid template statement to enter into the required field called "State template * ". Then my helper will be recognized as “valid” and then I can easily delete it again.

PS: I did a full reboot of HA. I didn’t loose my timer :slight_smile: but at the same time my faulty helper stayed there :frowning: