Newbie here, trying my hand at configuration.yaml and messing it up!

Hi all
Very green newbie here. I’m getting on ok as far as my install, nabu casa, and using visuual editor
I have myenergi solar, BMW Connected Drive, Sonoff/eweLink, Tuya, Sonos integrations working

But there are a couple of things bugging me that I think can be solved by creating entities/sensors in the configuration.yaml file.

To this end I have installed the Studio Coder Server and can open and edit my configuration.yaml file…
Now to the problems:

  1. I was messing around looking at samples of entities and sensors that folks had posted as solutions to other folks’ problems, amd I added a couple of chunks of code. I even made a ‘pressure sensor’ with a value of 5. Great I thought. Then I deleted that. But the entity is still there! How do I get rid of it? I searched this forum for that and I found folks writing about ‘lovelace’ and I’m totally lost. So if you can tell me how to get rid of enetities that I created and then deleted from my configuration.yaml file that would be great!
  2. Secondly, I am trying to make sensor entities which are based on ones coming from the myenergi integration with my myenergi solar inverter control system.
    The existing entity which measures powet to and from the electricity grid can be both positive and negative, positive indicating power drawn from the grid, and negaitive indicating export to the grid. I would like to have a dashboard which shows the export as a positive number. How do I do that? (The existing entity’s id is sensor.myenergi_home_solar_power_grid)
  3. Lastly I’d like to be able to make a new entity which uses a formula acting on one or more entities - for example I’l like to be able to add two together and then divide by a third one to get a percentage value, and I’d like to be able to multiply the value of another by a constant value like 1.3.

Any help gratefully received!

Did you restart Home Assistant after you deleted the “pressure sensor” entity?

Home Assistant must load the modified configuration.yaml file and that happens at startup (it can also be reloaded via Developer tools → YAML).

Sounds as if you’re ahead of the game! :grin:

“Lovelace” is an old name for the dashboards. It’s never quite gone away :roll_eyes:. You can use templates in some dashboard cards, but if you’re working on your configuration.yaml, posts about that are probably not relevant to what you’re doing.

If you’re trying to create a sensor entity, the best place to do it is in Developer Tools | Template, which is a sort of sandbox for experiments. When you’ve got it right you can copy it to configuration.yaml. The value of the sensor can then be displayed in a dashboard.

If you haven’t already, you’ll need to read up on templating.

Entities removed from configuration.yaml should disappear after a restart.

1 Like

Yeah I did. Multiple times. Bugger is still there!

Where are you seeing it?

In the list of entities under settings/devices…

When you created the sensor entity’s configuration, did you configure it with the unique_id option?

If you did then the entity’s configuration is recorded in a hidden config.entity_registry file. Simply deleting its configuration from configuration.yaml will not eliminate it.

  1. Go to Settings → Device and Services → Entities
  2. Find the deleted “pressure sensor” entity in the list of all entities.
  3. Check if it has an icon displayed in its Status column.
  4. Click the icon and a popup should offer a means of deleting the entity (it will be removed from the entity registry).

No I didn’t do that unique id stuff. In fact in the entity list when I click the cog wheel it says it isn’t unique….
Told ya I was messing it up! :grinning:

You’re going to have to tell us a lot more about this sensor’s configuration. Either post its configuration or, if it’s no longer available, explain as much as possible about what you created.

Errr… you can forget the problem re the phantom entity …. it has gone.

I swear it was there, even after 2 or 3 restarts.

Or maybe I’m going nuts.

OK so what I’ve learned is “I need to get into templating …,

It’s time for bed here…, I’ll get back to you if I get stuck again tomorrow.

Thanks for the help so far.

I think Home Assistant is just great by the way.

I’m controlling charging my hybrid vehicle, my water heating, solar export with it … I’m addicted :grinning:

3 Likes

I got the absolute value entity working!

As advised by @jackjourneyman I played with the template editor under developer tools.

I got {{states.sensor.myenergi_charger_grid_ct2.state|int|abs}} to output what I wanted… then I made a template Helper and put this in it and it works a treat!

Is that the best way to do it?

I note that I didn’t have to open and edit configuration.yaml, which is fine by me but maybe there’s a more efficient/elegant solution?

1 Like

Absolutely.

Template helpers are a fairly recent innovation to make templates accessible through the UI - you can do it either way, but complex templates still have to be done in configuration.yaml.

Never mind elegance - does it work? :grin:

Refer to the warning at the bottom of the States section of the Templating documentation regarding the optimal way to reference an entity’s state value. HINT: It is not states.sensor.foo.state)

In addition, when you use a filter like int or float you should configure them with default values. If they are given a value they can’t convert to integer or floating point they will use the default value. Without a default in that situation, they generate an error (i.e. the template fails).

1 Like

Most of the time it is just UI cache. Refresh browser view and they are gone in such cases in most cases.

1 Like

Yup it works !!

1 Like

Ok I’ll take a look at that info. Thanks v much :+1:

1 Like

Hi @123

does {{states(‘sensor.myenergi_charger_grid_ct2’)|int(0)|abs}} look right?

thanks!

Yes.

For future reference, you can test your template in the Template Editor (Developer Tools → Template).

It’s a great tool for experimenting with a template and confirming it works properly before you use it in Template Sensor or Template Condition or Template Trigger, etc.

NOTE

You may notice that if you post a template in the forum but don’t format it as being code, the forum’s software converts standard quotes into “fancy” quotes.

Compare the opening and closing quotes here:
{{ states(‘sensor.myenergi_charger_grid_ct2’)|int(0)|abs}}

To the quotes here:

{{ states('sensor.myenergi_charger_grid_ct2')|int(0)|abs}}

Don’t make the mistake of copying a template example containing “fancy” quotes into Home Assistant (it will fail to work).

2 Likes

Yes I tried it first in the template editor, which is great fun.

Now I want to work on a more complicated one which will be ‘percentage green energy’ (P)

(The visual in the myenergi app has this but the integration doesn’t support it as an entity)

I have a number of power entities

gridPower (g)
panelPower (p)
batteryDischargePower (b)

Logically it’s

If g < 0 then P = 100% (as it means the system is exporting power)
else
P = (p+b)/(g+p+b) x 100%

I need to play with the template editor to see if I can do this.

Here’s one way to do it. It uses Jinja2 variables to help make the formula easier to read.

Change the entity_ids to match yours.

{% set g = states('sensor.grid_power') | float(0) %}
{% set p = states('sensor.panel_power') | float(0) %}
{% set b = states('sensor.battery_discharge_power') | float(0) %}
{{ 100 if g < 0 else (p+b)/(g+p+b) * 100 }}

EDIT 1

Correction. Added missing |
(See jackjourneyman’s post below).

EDIT 2

Correction. Replace x with *

2 Likes