Result of - platform: command_line as payload?

I have stored some fixed information in a file below my config (using a subdir first). The file is more or less static, as only manually updated.
What I try to achieve is to get a single line from file (not the last one, hence file sensor will not help) and send this line back with a MQTT message.
The sequence schould be this:
1 receive a topic with the required line number
2 get the line from the file by using the line number received
3 send back a topic with the txt data of the line

By now I could achive to select the line by sed and display it in a text sensor
This works well!

sensor:
- platform: command_line
  name: cmdline_test
  command: "sed -n ''2p'' /config/owinet/sensor_0.txt"

Next I tested the MQTT trigger also with success :slightly_smiling_face:
as long I do not use -platform command_line for the value.

- alias: "get cmd line"
  trigger:
    - platform: mqtt
      topic: "owinet/getline"
  action:
    service: mqtt.publish
    data:
      topic: owinet/eeprom/sensor/set
#      payload: "{{ trigger.payload * 2}}"   #doubles the received string !
      payload: 
        - platform: command_line
          name: cmdline_test
          command: "sed -n ''2p'' /config/owinet/sensor_0.txt"

My questions:

  1. How to get the result from -platform: commandline as a payload value ?
  2. How to create the command with a parameter set by the received number ?
    Guess I have to use some value-template.

Hope someone can help as my knowlede for HA is still poor (but improving day by day :sweat_smile:)

I believe the command line sensor really on supports on/off type payloads.

How about using the command line sensor to write the output to another file, and read that with the file sensor. So (untested):

command: "sed -n ''2p'' /config/owinet/sensor_0.txt > /config/owinet/result.txt"

Similarly, when you receive the MQTT message with the line number, you could write the whole sed command with the required line number to a shell script file, and then change the command line sensor to run that shell script file.

Probably:

payload: "{{ states('sensor.cmdline_test') }}"