Running a python script to get a value for a sensor


I’m trying to create a sensor that gets its value from a python script.
I have not been able to find a way to define the sensor in the config.yaml file.

I have tried this

    - platform: command_line
      name: average_temperature_bedroom
      command: "python3 /scripts/"

It returns the following error:

Invalid config for [template]: [platform] is an invalid option for [template]

I have tried the old template style without success. I can’t find an example of a new style template that uses the platform: command_line in the documentation.

I aim to create a sensor that uses a python script to set its value.
I know how to average temperature using templates, and it’s not the solution I am looking for.

Any suggestions are welcome. Thank you for your help.


command: “python3 /scripts/”
value_template: “{{ value }}”

And in your script maybe:

is there a way to do it if the output of the python script is json where, for example, the outputs will be “temperature_outside”, “temperature_inside”, “temperature_under_the_ground” so that these values are written to three sensors, but the script is only run once and not 3 times?

Last time I made a temperature sensor project, I used a cron job to ran a python script. The result gets written to a database and I just ask home assistant to do SQL queries only.

This can probably be simpilified if you dont want to store the data twice. Maybe by asking home assistant to run the script once, the script writes to a text file and the other two sensors just reads the files. That way you can avoid sending everything 3 times.