Failed when trying to deploy python script

Good companions. I’m new to Home Assistant and I get lost easily. I am trying to implement a python script to HA to change the value of a sensor on action of a button in lovelace. The script is this:

I have HA in a docker, and I have saved that script in this folder:

/usr/share/hassio/homeassistant/python_scripts

Then in configuration.yaml I insert this code to call it as a service:

python_script:

  - service: python_script.set_state
    data:
    entity_id: sensor.sensor_log
    state: '0'

But it gives me this error when validating HA:

Invalid config for [python_script]: expected dict for dictionary value @ data['python_script']. Got [OrderedDict([('service', 'python_script.set_state'), ('data', None), ('entity_id', 'sensor.sensor_log'), ('state', '')])]. (See /config/configuration.yaml, line 96).

What am I doing wrong? Thank you very much partners.

your indentation is wrong on your service call, yaml is very strict about indentation.

  - service: python_script.set_state
    data:
      entity_id: sensor.sensor_log
      state: '0'
1 Like

petro, it keeps giving me problems when making the indentation:

Invalid config for [python_script]: expected dict for dictionary value @ data['python_script']. Got [OrderedDict([('service', 'python_script.set_state'), ('data', OrderedDict([('entity_id', 'sensor.sensor_log'), ('state', '')]))])]. (See /config/configuration.yaml, line 96).

python_script:

  - service: python_script.set_state
    data:
      entity_id: sensor.sensor_log
      state: '0'

The service call does not go in the python script section, it goes in a scripts sequence or automation action section.

1 Like

Many thanks !!! This opens up a lot of the world ahead of me, it’s wonderful. Everything can be accessed with a python code. HA is wonderful.