How to send monthly energy report using notifications (but not at midnight)

Spot the difference:

Thank you!!! That fixed it!

- binary_sensor:    
  - name: "Close to Home"
    state: '{{ states("proximity.home")|int < states("input_number.close_to_home_trigger")|int }}'

  - name: "Night Mode"
    unique_id: night_mode
    availability: '{{ not is_state("sensor.south_patio_illuminance_lux", "unavailable") }}'
    state: '{{ states("sensor.south_patio_illuminance_lux")|float(default=0) < states("input_number.outside_lux_trigger_point")|float(default=0) }}'
    delay_on: 00:02:00
    delay_off: 00:01:00
- select:
  - name: "Select Destination"
    unique_id: select_destination
    state: "{{ states('input_select.destination') }}"
    options: "{{ state_attr('input_select.destination','options') }}"
    select_option:
      service: input_select.select_option
      target:
        entity_id: input_select.destination
      data:
        option: "{{ option }}"
- sensor:
  - name: "Direction of Travel"
    state: '{{ state_attr("proximity.home", "dir_of_travel") }}'
    
  - name: "Monthly Energy Cost: Car Charger"
    state: '{{ states("sensor.car_charging_step_1")|float(default=0) * states("input_number.bchydro_step_1_rate")|float(default=0) + states("sensor.car_charging_step_2")|float(default=0) * states("input_number.bchydro_step_2_rate")|float(default=0) }}'
    device_class: monetary
    unit_of_measurement: CAD
  - trigger:
    - platform: template
      value_template: "{{ (now() + timedelta(days=1)).day == 1 and now().hour == 23 and now().minute == 59 }}"
    sensor:
      - name: "Last Month Car Charging Total Energy Cost"
        state: "{{ states('sensor.node_50_electric_consumed_kwh') }}"

  - trigger:
    - platform: time_pattern
      seconds: "/5"
    sensor:
      - name: "HASS Agent My PC Idle Time"
        state: '{{ as_timestamp(utcnow()) - as_timestamp(states("sensor.lastactive")) }}'
        unit_of_measurement: "Seconds"
1 Like

This new sensor is working but since it’s value does not survive an HA restart, I am using MQTT to save it which is triggered by an automation…

  - trigger:
    - platform: template
      value_template: "{{ (now() + timedelta(days=1)).day == 1 and now().hour == 23 and now().minute == 59 }}"
    sensor:
      - name: "Car Charging Monthly Energy Cost"
        state: '{{ states("sensor.monthly_energy_cost_car_charger") }}'
alias: Car Charging Monthly Energy Cost
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.car_charging_monthly_energy_cost
condition: []
action:
  - service: mqtt.publish
    data:
      topic: energy/sensor/car_charger_energy
      payload_template: '{{ states("sensor.monthly_energy_cost_car_charger") }}'
mode: single

and use this sensor to display it…

  - platform: mqtt
    name: "Total Car Charger Energy"
    state_topic: "energy/sensor/car_charger_energy"

Phase 2:
I want to save the monthly energy values as attributes like…

jan:
feb:
mar:
apr:
may:
jun:
jul:
aug:
sep:
oct:
nov:
dec:

I think I can use the same automation to publish it for the current month but the templating for the MQTT json attributes has me baffled as I have done very little with MQTT in general, and nothing at all in this manner…

First crack at a template:

'{\"{{ now().timestamp() | timestamp_custom('%B') }}\":\"211.5\"}'

But the publish is not right…