Is there a way to get a binary_sensor for whether daylight saving time is applicable or not

Can anyone offer some guidance as to how I can create a binary_sensor indicating whether daylight saving time is currently in use or not? Thanks

Yes, I provided an example in this post.

For your requirement, create a Template Binary Sensor using this for its value_template:

{{ now().timetuple().tm_isdst > 0 }}

Because the template uses the now() function, the template sensor has nothing to monitor (it won’t trigger periodically). Therefore you need to specify an entity that triggers, for example, once a day (that’s sufficient for reporting DST changes). sensor.date updates itself at the start of every new day so that’ll work well here.

binary_sensor:
  - platform: template
    sensors:
      is_dst:
        entity_id: sensor.date
        value_template: "{{ now().timetuple().tm_isdst > 0 }}"

Ensure you have sensor.date defined elsewhere in your configuration.

sensor:
  - platform: time_date
    display_options:
      - 'date'
5 Likes

Perfect, thank you!

I know this is a little old, but I foresee a possible issue here. sensor.date will update at midnight every night, but daylight savings time starts around 2 or 3 in the morning. This will mean that the is_dst binary_sensor will be incorrect for most of a day twice a year (and likely at the most important time as you get up in the morning and look at whatever thing it was that uses that value)

1 Like

If that’s a concern then you can schedule an automation to trigger at 03:05:00 and have it call the homeassistant.update_entity service for binary_sensor.is_dst (documentation).

I am using this suggestion to create a binary sensor that tells if current time is DST or not.
Here is my sensor’s definition:

- platform: template
  sensors:
    daylight_saving_time:
      entity_id: sensor.time
      value_template: >-
        {%- if now().timetuple().tm_isdst | int > 0 -%}
          on
        {%- else -%}
          off
        {%- endif -%}

Although I am including the sensor.time as a monitored one (and it is included in the configuration YAML, so it exists), my sensor is always rendered as “off”.
If I evaluate the if condition on the template dev tool, it evals as true. Moreover, I created a sensor with just the now().timetuple().tm_isdst | int expression, and it is currently calculated as 1.

What am I missing here that is preventing the sensor from having the “on” value?

Create a Template Binary Sensor and then this is all you need to make it report if DST is in effect:

  - platform: template
    sensors:
      is_dst:
        entity_id: sensor.time
        value_template: "{{ now().timetuple().tm_isdst > 0 }}"

The problem in your template is the use of on which has special meaning and is interpreted as a variable. Delimit it with either single or double-quotes to make it clear you want it handled as a string.

1 Like

New attempt with the following code, but same result (off).

- platform: template
  sensors:
    daylight_saving_time:
      entity_id: sensor.time
      value_template: >-
        {%- if now().timetuple().tm_isdst | int > 0 -%}
          "on"
        {%- else -%}
          "off"
        {%- endif -%}

It is as if the condition is evaluated as false, considering that I am getting “off” as the sensor value.

I’m not sure you are getting what Taras is telling you.
You only need what he gave you you don’t need your multiline template symbol, all of the space removal characters or the complexity.

1 Like

Ok, I think now I get it.
I assumed that a binary sensor had either a “on” or “off” value (literals, just like an enum).
At first I thought Tara suggested to create a normal sensor that would get a true / false value, but I now understand that true = on and false = off for a binary sensor in hassio.
Sorry about the confusion :slight_smile:

A binary sensor is either true or false
That is translated in the front end to show on or off
If you change the device class it can show open or closed etc
But it’s still just a true or a false

Have a look at : -


For the options available
1 Like

If you used that code to create a Template Sensor then it would work. However, it you are using it to create a Template Binary Sensor then it won’t because of what you already discovered about true/false in a template.

1 Like