startnum is a variable I made up to hold the character position of Munutd. You can actually write all the changes like this with no new variables except the new found text.
So add this line
newname = name [name.find('Manutd') +1, 5]
before this one and change this one too
with open('/home/homeassistant/.homeassistant/python_scripts/data.txt', 'w') as outfile:
json.dump(newname, outfile)
right so when I run my output script after adding the line you mentioned above I get an error:
Traceback (most recent call last):
File “football.py", line 15, in <module>
newname = name [name.find('Manutd') +1, 5]
TypeError: string indices must be integers, not tuple
I think you are saving the data.txt as json. Just save normally and it won’t have the ". Also, I would modify the program to create text files for each team you want, and then you can point the HA sensor to the corresponding text.
How about showing from the python file for all the teams? would I have to have separate python scripts for each team and separate text files for each team?
I would just output all the teams you want in the same python script. Something like this, rename the text file for each team and point HA sensor to that file. I would rename the variable and file names to correspond with the teams, i didn’t so you can follow how to repeat for more teams. you will also want to put the python script in a shell command and run it went you want the text file to update every month.
str2 = "Manutd"
startnum = int(name.find(str2))
newname = name[startnum + 6 :startnum + 11]
str3 = "nextteam" #note the # of char in the team name
startnum3 = int(name.find(str3))
newname3 = name[startnum3 + 8 :startnum3 + 11] #adjusted the 6 to 8 for team char length
print (newname)
with open('/home/homeassistant/.homeassistant/python_scripts/data.txt', 'w') as outfile:
json.dump(newname, outfile)
print (newname3)
with open('/home/homeassistant/.homeassistant/python_scripts/data3.txt', 'w') as outfile:
json.dump(newname3, outfile)
I have 2 things to overcome, one is to show the date like below:
How can I achieve this? as its not a string and the date updates every day I believe. So the code you gave above I cannot use like we did to find the times of certain teams playing.
Secondly, I have done the following to activate the automation according to the time of the state of the entity. However it doesn’t fire the automation for some reason.
- alias: United Time
initial_state: 'on'
trigger:
platform: template
value_template: '{{ now.time().strftime("%H:%M") == states.sensor.manutd.state }}'
action:
- service: notify.me
data:
message: 'Manutd Playing Now'
Any ideas?
Many thanks for your help buddy.
UPDATE:
Ignore the first part of the obstacle. I need the automation to work but I have a feeling that HA is not recording the time from the text file as time rather its taking it as a number.
Is there anyway I could resolve this? because my automation is testing the current time against the sensor value of ‘Manutd’ and it doesn’t trigger unfortunately.
and when I use value_template: '{{ now.time().strftime("%H:%M") == strptime(states.sensor.manutd.state , “%H:%M”) }}’ it does not work because of the date in the state of sensor.manutd
I should work, does states.sensor.manutd.state give you a text with only ##:## ? I highlighted were the quotes might be wrong as it appear wrong in your last post.
yeah I realised what you were saying but in my config I have got the correct quotes and the automation still does not fire. Did you try it in your dev template ?
I added a time sensor component and got my automation working. This is my automation:
This works but like I mentioned there is a lag of 4-5 seconds before the action is triggered. I cannot work it out why.
But this : '{{ now.time().strftime("%H:%M") == strptime(states.sensor.manutd.state) , "%H:%M") }}' does not work I think its because of the date i.e. 1900-01-01 as the trigger does not match with the now time.
I run the script using cron independent from HA…the output of the script is then saved as a text file and integrated with HA using the file sensor component.