Odd binary sensor behavior

Hi, I have this binary sensor

binary_sensor:
  - platform: tod                                                               # TIME OF DAY - On if it's dark outside
    name: nuit
    after: sunset
    after_offset: '00:30:00'
    before: sunrise
    before_offset: '-00:30:00'

that started to misbehave lately. I think it’s since I upgraded to 114.4 but I’ve really only noticed it in the last few days. It is correctly set at 30 minutes after sunset but the 30 minutes before sunrise isn’t triggered. Like now, it’s 10:17 am and the sensor still thinks it’s night. It, of course, creates havoc with all my automation that requires it.

The ‘sun’ sensor is working fine, like for now, it’s says ‘Sun’, but not that binary sensor.

image

Has something changed in 114.4 that created that problem? A reboot ‘fixes’ it until the following night.

Thanks.

Quick update, I removed the before_offset: '-00:30:00' from the sensor and now it works correctly (albeit not at the time I want) so is there something wrong with assigning a negative offset now? I want the sensor to be ‘on’ up to 30 minutes before sunrise.

I’ve noticed oddities with TOD sensors having both a before_offset and after_offset set together at the same time since 0.114 released.

I’ve just coded mine to have an offset for either before or after and then a static time and it seems to resolve it.

Not sure I follow what you did. Can you give more details please?

- platform: tod
  name:  Mode Manager - Early Morning
  after: sunrise
  after_offset: '-00:45'
  before: '08:00'

- platform: tod
  name: Mode Manager - Morning
  after: '08:00'
  before: '12:00'

- platform: tod
  name: Mode Manager - Afternoon
  after: '12:00'
  before: sunset
  before_offset: '-02:30'

- platform: tod
  name: Mode Manager - Evening
  after: sunset
  after_offset: '-02:30'
  before: '20:30'

This is what I had to do to get them to fire “correctly”. If I set both before_offset and after_offset, then it usually fails to update properly with nothing showing up in the logs.

Thanks, since it’s just one and only 30 minutes before sunrise, I’ve took it off and wait for the code to be fixed. Since in a unsupported configuration (running on Raspbian), I can’t really open an incident on github. Are you using a supported configuration?

Yeah, I can post a bug report today.

1 Like

Were you to open an issue with HA?

binary_sensor stayed ‘stuck’ this morning, even without an offset :frowning: So I did something similar to you but didn’t want to adjust all the automations that rely on that ‘nuit’ (night) binary_sensor. So here’s what I did:

  - platform: tod                                                               # TIME OF DAY - On if it's dark outside
    name: everning_dark
    after: sunset
    after_offset: '00:30:00'
    before: '3:00:00'

  - platform: tod                                                               # TIME OF DAY - On if it's dark outside
    name: morning_dark
    after: '2:00:00'
    before: sunrise
    before_offset: '-00:30:00'

  - platform: template
    sensors:
      nuit:
        value_template: "{{ is_state('everning_dark', 'on') or is_state('morning_dark', 'on') }}"

I’ll see in the following days if it works.

Same behavior on my side with tod, it remain now always in “on” state. It had worked without issue since it was first introduced a couple of months back.

Running latest version 0.115.2. Just found out the problem yesterday when I saw my exterior lights still open in the middle of the day. Cannot confirm exactly when (which version) it stopped working.

Very simple config

binary_sensor:
 - platform: tod
    name: Night
    after: sunset
    after_offset: '-00:02:00'
    before: sunrise
    before_offset: '00:02:00'

Will get a look if an Issue have officially been open in HA Core Tracker.

In the mean time, what I posted on the 20th has worked well for me.

Hi, were you able to see if an issue had been open yet?

In my case I do not have any “offset” values - anyway it doesn’t work:

There are 2 sensors - “standard tod” and self-made:

  - platform: tod
    name: Night
    after: sunset
    before: sunrise

  - platform: template
    sensors:
      night_reworked:
        friendly_name: 'Night (reworked)'
        icon_template: mdi:power-sleep
        value_template: "{% if is_state('sun.sun', 'above_horizon') -%}
                         {{ 'off' }}
                         {%- else -%}
                         {{ 'on' }}
                         {%- endif %}"

Here is the card with graphs:

type: history-graph
entities:
  - entity: binary_sensor.night
  - entity: binary_sensor.night_reworked
  - entity: sun.sun
hours_to_show: 12
refresh_interval: 0

изображение

So the “tod” sensor just does not work.

1 Like