Newbe to python.....help needed

Hi,

This is the first time I have tried to run a python script in HA so please go easy!

I am trying to get Bin / Waste Collection script working. I have added the python script in /config/python_scripts/ folder and used the following code in configuration.yaml

sensor:
  - platform: command_line
    name: "Bin collections"
    command: "python3 /config/python_scripts/cheshire_east_council.py"
    scan_interval: 86400

but I get this error in the log and nothing shows under Bin collections

Logger: homeassistant.components.sensor
Source: core.py:865
Integration: Sensor (documentation, issues)
First occurred: 9:21:52 (2 occurrences)
Last logged: 9:21:52

  • Error adding entities for domain sensor with platform command_line
  • Error while setting up command_line platform for sensor

Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 316, in async_add_entities await asyncio.gather(*tasks) File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 507, in _async_add_entity await entity.add_to_platform_finish() File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 531, in add_to_platform_finish self.async_write_ha_state() File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 296, in async_write_ha_state self._async_write_ha_state() File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 408, in _async_write_ha_state self.hass.states.async_set( File “/usr/src/homeassistant/homeassistant/core.py”, line 1179, in async_set state = State( File “/usr/src/homeassistant/homeassistant/core.py”, line 865, in init raise InvalidStateError( homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.bin_collections. State max length is 255 characters

As I said I have not used a python script in HA before so I might have over looked something basic.

Could anyone help?
Regards
James

State max length is 255 characters

indicates that the state of the sensor is not set properly. What does your script return to HA?

Regards

Nothing is being outputted at the moment, but this is a sample output of what it should look like

{
“bins”: [
{
“BinType”: “Empty Standard Garden Waste”,
“collectionDate”: “30/01/2020”
},
{
“BinType”: “Empty Standard Mixed Recycling”,
“collectionDate”: “30/01/2020”
},
{
“BinType”: “Empty Standard General Waste”,
“collectionDate”: “06/02/2020”
},
{
“BinType”: “Empty Standard Mixed Recycling”,
“collectionDate”: “13/02/2020”
},
{
“BinType”: “Empty Standard Garden Waste”,
“collectionDate”: “13/02/2020”
},
{
“BinType”: “Empty Standard General Waste”,
“collectionDate”: “20/02/2020”
},
{
“BinType”: “Empty Standard Mixed Recycling”,
“collectionDate”: “27/02/2020”
},
{
“BinType”: “Empty Standard Garden Waste”,
“collectionDate”: “27/02/2020”
},
{
“BinType”: “Empty Standard General Waste”,
“collectionDate”: “05/03/2020”
},
{
“BinType”: “Empty Standard Garden Waste”,
“collectionDate”: “12/03/2020”
}
]
}

Regards

An entity’s state value is limited to storing a string of no more than 255 characters in length. The sample you posted exceeds that limit and explains why you received an error message.

Change your python script to store the data in an attribute (name the attribute whatever you want). An entity’s attribute can store far more than 255 characters and not only as a string but other types like integer, float, list, dictionary, etc. That would be beneficial for your needs because the sample data is a dictionary containing a list of dictionaries. Its complex data structure will be preserved in an attribute.

While not a direct answer (since it has been answered already), you might consider looking at the custom add-on called Garbage Collection (that you can also install via HACS). It’s a really nice little add-on and does the job you are trying to code quite well - so well that I use it for a number of other recurring events. It also does things like check for holidays and lets you define how to deal with trash collection if it’s a holiday week (i.e., my collection backs off by a day if there is a holiday that week).

Just food for thought.

Thanks, I get the problem now, but I have not idea how to fix it as I have no experience of python code :frowning:
I only need the first three dates so it could be reduced to under 255 characters by stopping the loop after three but again I’ve got no idea how to do this in python.

While not a direct answer (since it has been answered already), you might consider looking at the custom add-on called Garbage Collection (that you can also install via HACS). It’s a really nice little add-on and does the job you are trying to code quite well - so well that I use it for a number of other recurring events. It also does things like check for holidays and lets you define how to deal with trash collection if it’s a holiday week (i.e., my collection backs off by a day if there is a holiday that week).

I’ve seen this however our bin dates chop and change, I will keep in in mind :grinning:

Regards

- platform: command_line
  name: "Bin collections"
  command: "cat /config/scripts/bins.txt"
  value_template: "dummy value"
  json_attributes:
    - bins

I don’t have your script: my command just retrieves a copy of what you pasted above, with the quotes fixed. In future, please format code using the </> button.

That gives:

and then…

In your first post you said you are using a python script, so my assumption was that you know python. However, I was mistaken; you are simply using a python script created by someone else.

The link you posted leads to python code that doesn’t use any external data (like the ‘sample output’ you posted). To better understand how all of this works, I would probably need to read the very long thread (> 300 posts). As fascinating as garbage collection scheduling may be, I don’t have the time to do it.

Hopefully someone else can help you solve it. Good luck.

Thank you to Troon and Taras I’ve now got it working :grinning:

Regards

1 Like