dbrunt
(Daniel)
1
Currently the MQTT topic looks like this…
I want to update the “May” element at month end with a new value. I’ve tried publishing this to MQTT but it wipes out the rest of the json:
service: mqtt.publish
data:
topic: energy/sensor/car_charger_energy/history
payload: >-
{
"{{ as_timestamp(now()) | timestamp_custom('%b') }}": {{ states("sensor.monthly_energy_cost_car_charger") }}
}
Or do I need to change the structure of the MQTT topic???
petro
(Petro)
3
pull the history in to a sensor’s attribute, then use that attribute when publishing a change to the payload.
dbrunt
(Daniel)
4
Sorry @petro but I had trouble following you.
I’ve ended up with the following…
I manually published homeassistant/sensor/car_charging_energy_cost:
Then added this integration:
mqtt_statestream:
base_topic: homeassistant
publish_attributes: true
publish_timestamps: true
include:
entities:
- sensor.car_charging_energy_cost
and created this automation which hopefully populates each month and again hopefully will update a Last Year total:
alias: Update Car Charging Energy Cost History
description: ''
trigger:
- platform: state
entity_id:
- select.car_charging
from: step_2
to: step_1
condition: []
action:
- if:
- condition: template
value_template: '{{ as_timestamp(now()) | timestamp_custom("%b") == "January" }}'
then:
- service: python_script.set_state
data_template:
entity_id: sensor.car_charging_energy_cost
Last Year: >-
{{ state_attr("sensor.car_charging_energy_cost",
"January")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "February")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "March")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "April")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "May")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "June")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "July")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "August")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "September")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "October")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "November")|float(default=0) +
state_attr("sensor.car_charging_energy_cost", "December")|float(default=0) }}
- service: python_script.set_state
data_template:
entity_id: sensor.car_charging_energy_cost
'{{ as_timestamp(now()) | timestamp_custom("%b") }}': >-
{{ (state_attr("sensor.car_charging_step_1",
"last_period")|float(default=0) *
states("input_number.bchydro_step_1_rate")|float(default=0) +
state_attr("sensor.car_charging_step_2", "last_period")|float(default=0)
* states("input_number.bchydro_step_2_rate")|float(default=0)) |
round(2) }}
mode: single
1 Like
dbrunt
(Daniel)
5
@4331525 I abandoned that approach and went with this simple automation, once I learned about Google Sheets!
alias: 32A Car Charging
description: ""
trigger:
- entity_id:
- binary_sensor.32a_car_charging
platform: state
to: "off"
from: "on"
condition: []
action:
- service: input_number.set_value
data:
value: >-
{{ (states('sensor.32a_car_charging_session_step_1')|float(0) *
states('input_number.bchydro_step_1_rate')|float(0) +
states('sensor.32a_car_charging_session_step_2')|float(0) *
states('input_number.bchydro_step_2_rate')|float(0)) | round(2) }}
target:
entity_id: input_number.car_charging_cost
enabled: true
- service: google_sheets.append_sheet
data:
config_entry: 1b4a46c6cba0677bbfb5a8c53e8618b0
worksheet: Clarity Charging
data:
Date: "{{ now().strftime('%-d-%b-%y') }}"
Step 1: "{{ states('sensor.32a_car_charging_session_step_1') }}"
Step 2: "{{ states('sensor.32a_car_charging_session_step_2') }}"
Cost: "{{ states('input_number.car_charging_cost') }}"
- service: input_number.set_value
data:
value: "{{ states('sensor.32a_car_charging_session_step_1') }}"
target:
entity_id: input_number.32a_car_charger_last_session_step_1
- service: input_number.set_value
data:
value: "{{ states('sensor.32a_car_charging_session_step_2') }}"
target:
entity_id: input_number.32a_car_charger_last_session_step_2
- service: utility_meter.reset
data: {}
target:
entity_id: select.32a_car_charging_session
mode: single
I am also now using the EV Charger Card in Lovelace…