How to format sensor containing date/time to be used in a time/date based automation

Hey,

I’'m stuck and need some pointers. The documentation is confusing (me) and I’ve tried to find the answers in different threads. No luck. I think I’m quite confused on the different time objects and formats.

My goal is to to have an automation that turn on a wall-plug during the three cheapest consecutive hours between 9pm and 5am (charging an electric bicycle).

The electricity price is made available in the afternoon, therefore I have a time based trigger that intends to create a sensor containing the date/time to turn on the wall-plug.

Following this, I intended to have an automation that triggers on this date/time and perform its task (turn on the wall-plug)

My configuration currently looks like this:

  - trigger: 
    - platform: time
      at: '17:30:00'
    sensor:
      - name: "Nordpool EO4 Cheapest Hours During the Night"
        unique_id: nordpool_eo4_chapest_night_hours_time
        state: >
          {% set nordpoolSensor = "sensor.nordpool_kwh_se4_sek_3_095_0" %}
          {% set priceData = namespace(numbers=[]) %}
          {% for i in state_attr(nordpoolSensor,'raw_today') %}
              {% set priceData.numbers = priceData.numbers + [i.value] %}
          {% endfor %}{% for i in state_attr(nordpoolSensor,'raw_tomorrow') %}
              {% set priceData.numbers = priceData.numbers + [i.value] %}
          {% endfor %}
          {% set three_consecutive_hours = namespace(numbers=[]) %}
          {% for n in range(21,29) %}
              {% set three_hour_sum = (priceData.numbers[n]+priceData.numbers[n+1]+priceData.numbers[n+2])|round(2) %}
              {% set three_consecutive_hours.numbers = three_consecutive_hours.numbers + [three_hour_sum] %}
          {% endfor %}
          {% set min_three_hour = min(three_consecutive_hours.numbers) %}
          {% set least_expensive_hours = three_consecutive_hours.numbers.index(min_three_hour) + 21  %}
          {% if least_expensive_hours > 23 %}
             {% set least_expensive_hours = least_expensive_hours - 24 %}
             {% set t = (now().replace(hour=least_expensive_hours, minute=0, second=0).timestamp() + 3600*24) %}
          {% else %}
             {% set t = (now().replace(hour=least_expensive_hours, minute=0, second=0).timestamp()) | timestamp_local %}
          {% endif %}
          {{t}}

This will give me the following output (which with the current input is correct, the three cheapest hours starts tomorrow at 1am local time):

Resultattyp: string
2022-09-23T01:00:00.950538+02:00

I fail to use this in my automation and seek guidance how to format the output to make it usable in an automation.

…or… any other tricks to achive my goal is obviously appreciated.

Thanks /Marcus

Add this to the configuration of your Trigger-based Template Sensor:

        device_class: timestamp

Then you will be able to reference your timestamp sensor in an automation’s Time Trigger:

alias: example
trigger:
  - platform: time
    at: sensor.nordpool_eo4_cheapest_hours_during_the_night
condition: []
action:
  ... your actions ...
1 Like

Thanks, now the sensor looks much better than before:

image

However, it did not show up in the dropdown when creating the automation.

Had to enter it using YAML mode:

Tomorrow we’ll see if it triggers…
After that I’ll have some protective code around the Nordpool ‘raw_tomorrow’ data which may or may not be there all the time.

Again, thanks!

/Marcus

1 Like

That’s due to a limitation of the Automation Editor in visual mode.