I have a set of template binary sensors that change value based on time_pattern triggers. These binary sensors have been working, code untouched, for years. Recently, potentially after a version upgrade (not sure, only noticed the problem recently since these sensors are only used when I am away), they stopped working. If I restart Home Assistant, each of the sensors will work once and then never change value again. The sensors are defined in my template.yaml. Here’s an example of one.
However, that did not improve things. I did see this post that looked like a similar issue but I was not able to resolve my problem with the infiormation there.
Does anyone have any ideas how I might be able to address this problem?
it’s because your template is wrong.
try using`something like that (sorry slightly modified using seconds but hours&minutes since I’m not too good in waiting for hours
I don’t believe that’s the issue. Remember these sensors have been working for literally years with that code. I would have expected to see problems long before this if type mismatches were the issue.
I suspect a recent change in the Home Assistant core is what is causing my grief.
The time pattern you are using will trigger every second (the hours always remain divisible by 1 even though the seconds and minutes change). Try this to trigger every hour:
- trigger: time_pattern
minutes: 0
seconds: 0
If that does not work, remove your template sensor from the package and put it in your configuration.yaml file under template:
watch for the last minutes since there’s a gap where HA was restarted.
I play with this problem for pure fun, so be patient I honor your thoughts about a fine template, but reality shows differnt.
Nevertheless since keeping braincells alive I’m always interested in a solution which works with the original templates. To be aware of difffernt solutions is always a benefit.
So this is just in case you’re willing to test it on your machine.
All what’s changed to save time is turning hours and minutes to seconds so things do happen without waiting for too long.
I set up a few variations using a 10 minute interval, and the issue seems to be present… after restarting HA or the integration they work one time again then stop.
I think you probably hit on something fairly esoteric. You’re using delay_on and auto_off correctly in a static sense, seemingly. But I am not sure that the definition of these parameters is consistent with your expectation given the constant on for the state. This could explain why a change to the implementation resulted in a change to the behavior you’re seeing
The more explicit way to do this is probably with a timer.
I am using a number of sensors configured similarly to the one in my original post to control the lights in my house when I am away. The sensors basically provide randomized on/off intervals for the lights. There are associated automations which use a number of these sensors to mimic a reasonably realistic light pattern versus simple static timer based logic.
Has worked well for quite a while. Some of my neighbours never even realize when I am away
Yep, noticed that myself and I would have bet a tenner it’ll fail … since if you do things completely wrong the log says it expects HH:MM or HH:MM:SS or even HH:MM:SS.F and HH:M:SS is something different. But it accidently did work.
Tried with another idea but that one failed
delay_off: "{{ '00:00:' + "%2i" | format (range(1,15)|random|int) }}"
but while typing these lines I bet it it must read
delay_off: "{{ '00:00:' + "%2s" | format (range(1,15)|random|int) }}"
to serve the trailing 0 upon need.
EDIT: and I now know why “%i” did fail. It created something such as " 1" means 11: 1:11 is definatly invalid while 11:1:11 is accepted.