Reading text and time from a python file and having it as a sensor in HA

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

Sorry to bother you again but can you tell me please why do you want to find the character (index) of the numbers? How does that help?

What I was trying to do was create a program to output 12:30. To do this, we need to locate text and find where that text begins.

This program will output 12:30, i checked it

str2 = "Manutd"
startnum = int(name.find(str2))
newname = name[startnum + 6 :startnum + 11]

print (newname)

with open('/home/homeassistant/.homeassistant/python_scripts/data.txt', 'w') as outfile:
    json.dump(newname, outfile)
1 Like

That works very well producing “12:30” as expected for Manutd. Thank you!!

I was wondering why does the data.txt produce “12:30” and not just 12:30 ? Wouldn’t this be an issue in value templating?

In my configuration.yaml. I have added the following:

  • platform: file
    name: Manutd
    file_path: /home/homeassistant/.homeassistant/python_scripts/data.txt
    unit_of_measurement: ‘hours’

But I am not getting the time i.e. in hours state. What am I doing wrong?

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.

Ok that works great.

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)

Right I have done that thanks for that.

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.

I seached the forum and found this

so maybe this might work, try it in your dev templating to see its output quickly

{{ strptime(states.sensor.manutd.state , “%H:%M”) }}

if it does, just change this line or perhaps change the sensor so it reads as time.

    value_template: '{{ now.time().strftime("%H:%M") == strptime(states.sensor.manutd.state , “%H:%M”) }}'

The above returns the following:

1900-01-01 12:30:00

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 had the wrong quotes, they need to be ' not ’

'{{ now.time().strftime("%H:%M") == strptime(states.sensor.manutd.state) , "%H:%M") }}'

I thought you had ‘ and not ’ in your previous message. I dont see any change mate and I get the same result as above.

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:

  • alias: Test
    initial_state: ‘on’
    trigger:
    platform: template
    value_template: ‘{{ states.sensor.test.state == states.sensor.time.state }}’
    action:
    • service: notify.me
      data:
      message: ‘Test’

However, I noticed a lag of 4-5 seconds with the sensor time component before the automation triggers.

So does that maudt sensor work? I don’t have a time sensor in text to try but this work so it appears to be correct format.

value_template: '{{ states.sensor.manutd.state == states.sensor.time.state }}'

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.

is there a way were I could trigger the automation 5 seconds earlier?

I have a dumb (and belated…) question: I thought you can’t import libraries into python scripts? How did you do this?

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.

1 Like