Use multiple sensor attributes to send notification

Hello Guys, I’m pretty new in HA and templating. I have a sensor created by HASC integration, which gives me multiple attributes with time “time_on_1” “time_off_1”, “time_on_2” “time_off_2”, “time_on_3” “time_off_3” - its Peak and off-peak times for electricity

I want to use this times to send me notification X minutes before each “on” time. I was able to read this time with template, but just one by one, and I don’t know how to use it in automations.

{{ state_attr('binary_sensor.nizky_tarif', 'data')[0].CAS_VYP_1}} 

Here is how the atrributes of sensor looks like:

data:
  - primaryKey: 3177
    ID: 3177
    VALID_FROM: 1616968800000
    VALID_TO: 4070905200000
    DUMP_ID: 29
    POVEL: A1B5DP6
    KOD: '83'
    KOD_POVELU: '170'
    SAZBA: D25d
    INFO: sazba+TUV+AKU
    PLATNOST: Po - Pá
    DOBA: '8'
    CAS_ZAP_1: '2:00'
    CAS_VYP_1: '5:30'
    CAS_ZAP_2: '13:25'
    CAS_VYP_2: '15:25'
    CAS_ZAP_3: '21:20'
    CAS_VYP_3: '23:50'
    CAS_ZAP_4: null
    CAS_VYP_4: null
    CAS_ZAP_5: null
    CAS_VYP_5: null
    CAS_ZAP_6: null
    CAS_VYP_6: null
    CAS_ZAP_7: null
    CAS_VYP_7: null
    CAS_ZAP_8: null
    CAS_VYP_8: null
    CAS_ZAP_9: null
    CAS_VYP_9: null
    CAS_ZAP_10: null
    CAS_VYP_10: null
    DATE_OF_ENTRY: 1616410728000
    DESCRIPTION: 2021_jaro_sever
  - primaryKey: 3178
    ID: 3178
    VALID_FROM: 1616968800000
    VALID_TO: 4070905200000
    DUMP_ID: 29
    POVEL: A1B5DP6
    KOD: '83'
    KOD_POVELU: '170'
    SAZBA: D25d
    INFO: sazba+TUV+AKU
    PLATNOST: So - Ne
    DOBA: '8'
    CAS_ZAP_1: '3:45'
    CAS_VYP_1: '6:55'
    CAS_ZAP_2: '14:35'
    CAS_VYP_2: '16:55'
    CAS_ZAP_3: '21:05'
    CAS_VYP_3: '23:35'
    CAS_ZAP_4: null
    CAS_VYP_4: null
    CAS_ZAP_5: null
    CAS_VYP_5: null
    CAS_ZAP_6: null
    CAS_VYP_6: null
    CAS_ZAP_7: null
    CAS_VYP_7: null
    CAS_ZAP_8: null
    CAS_VYP_8: null
    CAS_ZAP_9: null
    CAS_VYP_9: null
    CAS_ZAP_10: null
    CAS_VYP_10: null
    DATE_OF_ENTRY: 1616410728000
    DESCRIPTION: 2021_jaro_sever
friendly_name: nizky tarif
icon: 'mdi:power'
device_class: '' 

Can you guys help?
Thanks

Let’s say X = 10 minutes for this example:

trigger:
  - platform: template
    value_template: "{{ (as_timestamp(now()) - 60*10)|timestamp_custom('%-H:%M) == state_attr('binary_sensor.nizky_tarif', 'data')[0].CAS_VYP_1 }}"
  - platform: template
    value_template: "{{ (as_timestamp(now()) - 60*10)|timestamp_custom('%-H:%M) == state_attr('binary_sensor.nizky_tarif', 'data')[0].CAS_VYP_2 }}"
  - platform: template
    value_template: "{{ (as_timestamp(now()) - 60*10)|timestamp_custom('%-H:%M) == state_attr('binary_sensor.nizky_tarif', 'data')[0].CAS_VYP_3 }}"

If you want to make the 10 minutes variable you can easily substitute an input_number for the “10” in each template. e.g.

value_template: "{{ (as_timestamp(now()) - 60 * states('input_number.minutes_before')|int )|timestamp_custom('%-H:%M) == state_attr('binary_sensor.nizky_tarif', 'data')[0].CAS_VYP_1 }}"

Working like a charm, thank you! Is there any way how to use counter or imput_number in "CAS_VYP_1 "? Something like

CAS_VYP_[counter.counter.nizky_tarif] 

Thanks

What are you trying to do with the counter?

I´m trying to sent notification, with end of the off peak time period. I dont want to do multiple automations, I want after every automation run to inrease counter (1-3) to use it as variable in CAS_VYP (time_off) attribute, as there are 3 time period during a day.

You haven’t answered my question. Why do you want to use a variable?

Do you want some sort of indication in the notification which time period is starting?

Yes, I want to see in the notificacion which period is starting and when its ending.

Here is my notification settings:

service: notify.janyshomebot

data:
  message: >-
    Nízký tarif začíná za 10 minut. Končí ve {{state_attr('binary_sensor.nizky_tarif', 'data')[0].CAS_VYP_3}}

Hmm. Tricky. Let me think about it.

I don’t think we can use the trigger object as that will be now()

FWIW, the triple Template Triggers can be reduced to one:

trigger:
  - platform: template
    value_template: >
      {% set t = (now().timestamp() - 600)|timestamp_custom('%-H:%M) %}
      {% set nt = state_attr('binary_sensor.nizky_tarif', 'data')[0] %}
      {{ t in [nt.CAS_VYP_1, nt.CAS_VYP_2, nt.CAS_VYP_3] }}

Efficient as ever Taras.

Do you have any hints on how to get the end time for the notification?

The time range appears to be

CAS_VYP_n -> CAS_ZAP_n+1 (except when n=3, then it rolls back to zero).

I’m having difficulty understanding the OP’s additional requirement.

data is a list containing two items (or at least there are only two in the example). The trigger is using the zeroth item. Each item contains a dictionary. The dictionary contains several keys, beginning with the letters CAS, containing time values.

Depending on whether the trigger matched the time in CAS_VYP_1, or CAS_VYP_2, or CAS_VYP_3, the goal is to use CAS_ZAP_n+1 in the notification?

Yes, except for CAS_VYP_ 3, which appears to turn off at CAS_ZAP_1.

Though I may have this incorrect.

There are 3 time period during a day with off-peak electricity prices. Each period start at CAS_ZAP_1-3 and ends at CAS_VYP_1-3. The periods are not subsequent. (CAS_ZAP means in czech TIME_ON, CAS_VYP means TIME_OFF)

I’m trying to achieve one automation which send me notification 10 minutes before off-peak period start and in the text is end of this period.

I can make it easily with 3 automations but I just want to know if is it possible to do it in just one :slight_smile:

Then we are using the wrong attributes for the trigger. VYP is off time not on time. At the moment you will be notified ten minutes before off peak finishes, not before it starts, as per your original request.

I got this and used the ZAP attribute :slight_smile: