Hey all,
I’m doing some automations based around Octopus energy events, using this blueprint as my own template (found here: https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/blueprints/octopus_energy_octoplus_join_saving_session.yml):
blueprint:
name: Octopus Energy - Join Saving Session
description: Automatically joins saving sessions when they become available
domain: automation
author: BottlecapDave
input:
saving_session_event_entity:
name: Saving session events
description: The saving session event entity to join (e.g. event.octopus_energy_{{ACCOUNT_ID}}_octoplus_saving_session_events)
selector:
entity:
filter:
- domain:
- event
integration: octopus_energy
multiple: false
mode: single
variables:
saving_session_event_entity: !input saving_session_event_entity
trigger:
- platform: state
entity_id: !input saving_session_event_entity
condition:
- condition: template
value_template: "{{ state_attr(saving_session_event_entity, 'available_events') != None and state_attr(saving_session_event_entity, 'available_events') | length > 0 }}"
action:
- service: octopus_energy.join_octoplus_saving_session_event
data:
event_code: "{{ state_attr(saving_session_event_entity, 'available_events')[0]['code'] }}"
target:
entity_id: !input saving_session_event_entity
- service: persistent_notification.create
data:
title: Joined Saving Sessions
message: >
{% set event = state_attr(saving_session_event_entity, 'available_events')[0] %}
{% set event_start = event['start'] %}
Joined a new Octopus Energy saving session. It starts at {{ event_start.strftime('%H:%M') }} on {{ event_start.day }}/{{ event_start.month }} for {{ event.duration_in_minutes | int }} minutes.
Using that I made my own automation that creates a calendar event and then sends a notification similar to the one in the blueprint:
alias: Create Octopus calendar events
description: ""
triggers:
- trigger: state
entity_id:
- binary_sensor.octopus_energy_a_xxxxxxxx_octoplus_saving_sessions
attribute: next_joined_event_start
id: Saving session
- trigger: state
entity_id:
- >-
binary_sensor.octopus_energy_a_xxxxxxxx_octoplus_free_electricity_session
attribute: next_event_start
id: Free electricity
conditions:
- condition: template
value_template: "{{ trigger.to_state.attributes[trigger.attribute] is not none }}"
actions:
- variables:
start: "{{ trigger.to_state.attributes[trigger.attribute] }}"
end: |
{% if trigger.id == 'Saving session' %}
{{ trigger.to_state.attributes['next_joined_event_end'] }}
{% else %}
{{ trigger.to_state.attributes['next_event_end'] }}
{% endif %}
- action: calendar.create_event
metadata: {}
data:
summary: "{{ trigger.id }}"
start_date_time: "{{ start }}"
end_date_time: "{{ end }}"
target:
entity_id: calendar.octoplus_events
- action: notify.telegram_us
metadata: {}
data:
message: >-
Joined a new Octopus {{ trigger.id | lower }} event on {{ start.day
}}/{{ start.month }} from {{ start.strftime('%H:%M') }} till {{ end.strftime('%H:%M') }}.
mode: single
The calendar bit works great, but the notification fails with the error Error rendering data template: UndefinedError: 'str object' has no attribute 'strftime'
. I’m using the exact same logic as the blueprint to create my message so I’m struggling to understand why it’s failing?
I tried a bunch of things including defining the time variables separately, but it just moved the error to that section. Eventually I bounced the problem off an AI, it suggested using as_datetime
when defining the variables, but that made no difference (later testing shows they’re already datetime objects). Eventually it suggested using as_timestamp
instead, which worked as per the below.
So yeah, what I’m hoping to get an understanding of is why this is failing when everything suggests it should work? Would absolutely love any feedback!
Meanwhile, here’s the edit that works:
alias: Create Octopus calendar events
description: ""
triggers:
- trigger: state
entity_id:
- binary_sensor.octopus_energy_a_xxxxxxxx_octoplus_saving_sessions
attribute: next_joined_event_start
id: Saving session
- trigger: state
entity_id:
- >-
binary_sensor.octopus_energy_a_xxxxxxxx_octoplus_free_electricity_session
attribute: next_event_start
id: Free electricity
conditions:
- condition: template
value_template: "{{ trigger.to_state.attributes[trigger.attribute] is not none }}"
actions:
- variables:
start: "{{ trigger.to_state.attributes[trigger.attribute] | as_timestamp }}"
end: |
{% if trigger.id == 'Saving session' %}
{{ trigger.to_state.attributes['next_joined_event_end'] | as_timestamp }}
{% else %}
{{ trigger.to_state.attributes['next_event_end'] | as_timestamp }}
{% endif %}
- action: calendar.create_event
metadata: {}
data:
summary: "{{ trigger.id }}"
start_date_time: "{{ start | timestamp_local }}"
end_date_time: "{{ end | timestamp_local }}"
target:
entity_id: calendar.octoplus_events
- action: notify.telegram_us
metadata: {}
data:
message: >-
Joined a new Octopus {{ trigger.id | lower }} event on {{
(start | timestamp_custom('%d/%m')) }} from {{
(start | timestamp_custom('%H:%M')) }} till {{
(end | timestamp_custom('%H:%M')) }}.
mode: single