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

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