Command_line with shell comand, sensor and template

Hallo forum,
apologies, a total yaml newbee here…

I have the latest HA running on a Pi 4 with docker. It scanned my network and several things were found and integrated. Looks good.

Now I want to do some command line stuff (shell commands) and I stumbled over yaml (which seems a strange “language” …)

I have a little “Balkonkraftwerk” with a Deye micro inverter which data is available in the local network. I found some code snippets in the net and tried to copy that but I admit I don’t understand the concept of separated “sensor”, “template” and all the requirements that come with it. Why do I have to put things in the configuration.yaml and most stuff goes into the home-assistant_v2.db ?

Anyway, for testing I just read the temperature value from a zigbee sensor via curl and use it as an energy value. This code is now in my configuration.yaml:

# Command line sensors
command_line:
  - sensor:
      name: test
      unique_id: sensor_test
#      command: echo 12.43
      command: curl -s http://192.168.1.51:8080/api/A2F0894703/sensors/8 | grep -oP '(?<=\"temperature\":)\w+'
      unit_of_measurement: 'kWh'
      device_class: energy
      value_template: "{{value}}"
      scan_interval: 60

template:
  - sensor:
      name: "Test_Value"
      state: "{{ states('sensor.sensor_test') if states('sensor.sensor_test') | float(default=0) }}"
      unit_of_measurement: kWh
      unique_id:  test_value

Finally the sensor “test” works (shown with a value in the dashboard sensor section) but the template “Test_Value” shows “unbekannt” in the dashboard.

Is this the correct “new” style?
Why do I need a template at all?
Why can’t a template include a command: to be executed?
Why is in the sensor section a value_template at all? Could that value be handled by the template section? And also the state.
Single or doubled quotes? no quotes on command: ?

In general I am missing some “real world” example code in the ha documentation.

Thanks for some hints :slight_smile:

You are using the wrong entity id. When the sensor configuration is loaded for the very first time, the entity id it is assigned will be a slugified version of the name. So the the entity id of the first sensor will be sensor.test, or if such an entity already existed it will be sensor.test_2 etc.

The unique id is only used on the backend, it is not used anywhere a human is expected to read it. Therefore best practice is that the unique id should be a UUID or similar to make sure they actually are unique. If you change the entity id of your sensor in the GUI, the unique id is what makes sure it is still tied to the correct part of your configuration etc.