Template help, now().strftime('%H.%M')

I’m trying to come up with a template using helpers to switch between day and night rate tariff in the energy dashboard for imported energy. Start time for peak is 08:00 and end time is 23:00. Using my template below it just stays at peak rate at all times. Any ideas where I’m going wrong?

name: 'Energy Import Cost'
        unique_id: "energy_import_cost"
        unit_of_measurement: 'EUR/kWh'
        state: >
          {% set tariff = { "Day": 0.3836, "Night": 0.1840 } %}
          {% set time = { "month": (now().strftime('%m') | int), "hour": (now().strftime('%H') | int), "weekday": (now().weekday() | int ) } %}
          {%if ( 'input_datetime.end_charge_time' > now().strftime('%H.%M') < 'input_datetime.start_charge_time') %}
            {{ tariff.Day }}
          {% else %}
            {{ tariff.Night }}
          {% endif %}  
template:
  - sensor:
      - name: 'Energy Import Cost'
        unique_id: "energy_import_cost"
        unit_of_measurement: 'EUR/kWh'
        state: >
          {% set tariff = { "Day": 0.3836, "Night": 0.1840 } %}
          {% if today_at(states('input_datetime.start_charge_time')) < now() < today_at(states('input_datetime.end_charge_time')) %}
            {{ tariff.Night }}
          {% else %}
            {{ tariff.Day }}
          {% endif %}  
1 Like

Thank you very much, works perfect!

1 Like

Another way to do the same thing using an Immediate If.

template:
  - sensor:
      - name: 'Energy Import Cost'
        unique_id: "energy_import_cost"
        unit_of_measurement: 'EUR/kWh'
        state: >
          {% set is_night = today_at(states('input_datetime.start_charge_time')) < now() < today_at(states('input_datetime.end_charge_time')) %}
          {{ iif(is_night, 0.1840, 0.3836) }}
1 Like