How to combine two tod binary sensors?

Hi there,

I’m scratching my head with this problem for a while, not sure how to solve it efficiently: I have two sets of off-peak hours for electricity per day in my country (France), it goes from 3h04 to 8h04 then from 12h04 to 15h04.

- platform: tod
  name: Off-Peak Hours 1
  after: '03:04'
  before: '08:04'
- platform: tod
  name: Off-Peak Hours 2
  after: '12:04'
  before: '15:04

I’m now trying to combine both into one new sensor that I could use in my graphs below:


Should I redo a new sensor from scratch and implement the time logic condition? Or is there a way of combining both?



On my phone, so can’t type extensive answers now.

Do you only want to use this for display purposes?

You could keep these sensors and create a third template binary sensor that’s only off when both these tod sensors are off and otherwise on (fairly easy to do).

Or, you could ditch all of those and make a single trigger-based template sensor that triggers on the time boundaries you have, but will require a lot more logic in the template value field (but not crazy complicated).

A third option is to have an input boolean set with an automation (using the same triggers as in option 2) and wrap the input boolean in a sensor (but this is not strictly needed, since I think you can plot the input boolean directly, depending on which card you’re using).

It looks like you’re using the custom mini graph card, so your boolean values should be converted to 0 and 1.

If the two existing sensors are useful, just create a Template binary sensor that combines them.

  - binary_sensor:
      - name: "Off-Peak Combined"
        state: >
          {{ is_state('binary_sensor.off_peak_hours_1', 'on') or
          is_state('binary_sensor.off_peak_hours_2', 'on') }}

If they are not so useful…

  - binary_sensor:
      - name: "Off-Peak Combined"
        state: >
          {{ today_at("03:04:00") <= now() < today_at("08:04:00") or
          today_at("12:04:00") <= now() < today_at("15:04:00")}}
1 Like

Thanks it works like a charm :wink: