#Help with automation to create JSON file to track Tesla Trip and consumption

1 messages · Page 1 of 1 (latest)

rain viper
#

Hello there, the Tesla Automation by https://github.com/alandtse/tesla Alandtse provides enough data to track trip and consumption.
I set up six input number helpers:

  • battery trip start
  • battery trip end
  • odometer trip start
  • odometer trip end
  • datetime trip start
  • datetime trip end
    I populate this helpers listing for change in shift status. From P to D an automation populates the "trip start" helpers, from D to P another automation popluates the "trip ends" .

I could easily create another helper where store the consumption for a trip, but this would be limited because (i) I can store only one value (consumption or date, I'd need two helpers but I'd never be able to "link" date and consumption data) and (ii) I'm not sure how HomeAssistant would store this information.

I asked ChatGPT and it suggested to create a input_text helper to create a JSON file with the information and append the data at every run of the automation, but the code doesn't work.

here it is:
action: - service: input_text.set_value data_template: entity_id: input_text.tesla_trip_log value: > "distance_km": "{{ (states('input_number.tesla_trip_odometer_end') | float - states('input_number.tesla_trip_odometer_start') | float) | round(2) }}" } %} {% set current_log = states('input_text.tesla_trip_log') %} {% if current_log != 'unknown' %} {{ current_log + '\n' + new_trip | tojson }} {% else %} {{ new_trip | tojson }} {% endif %} {% set new_trip = { "timestamp": "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}",
Any help?

GitHub

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login. - alandtse/tesla

languid star
#

But if you do want to go down the path of tracking it with your method, I would suggest trigger-based template sensor, and storing that data in an attribute of that sensor

#

Any entity’s state will be limited to 255 characters, but the attributes don’t have that limit.

rain viper
#

Hi thanks for the reply, I knew of Teslamate but I skipped it because I do not have docker and the machine where HA runs is very limited in power and it work well with only HA, when I tried docker for other things everything was lagging so I put it aside.

Can you elaborate a little bit more about template sensor and attribute?
do you mean I should create a "trip_sensor" template with attributes like km, wh consumed, wh per km and datetime?
Should this be better than saving a JSON file?

languid star
#

You can store in a file too, that is another valid option. It really depends on what you want to do with the data after you store it. Your suggestion was to store it in an entity’s state, which is not a good idea.

#

That link is how you would use the notify service to write an entry to a text file

rain viper
#

thanks, I'll look into it. So no entity's state, yes to text file. Based on your suggestion I should log a JSON into the text file with the information for everyt trip, triggered by an automation which will call the notify service

languid star
#

It would be helpful if you describe what you want to do with the data you record. Look at it yourself? Feed it to some other database? Graph it on a dashboard? Use it to trigger automations?

#

Your main options for output are:

  • attribute of a sensor (probably a list of dictionaries)
  • json into a text file
  • plain text into a text file
rain viper
#

I'd like to use it in a dashboard, as a primary thing, but I wouldn't limit this to dashboard for future uses.
I'm trying to get data as a JSON format into a text file but whith this code

action: notify.send_message target: entity_id: notify.file data: message: >- { "timestamp": "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}", "start_time": "{{ states('input_datetime.tesla_trip_datetime_start') }}", "end_time": "{{ states('input_datetime.tesla_trip_datetime_end') }}", "duration_mins": "{{ ((as_timestamp(states('input_datetime.tesla_trip_datetime_end')) - as_timestamp(states('input_datetime.tesla_trip_datetime_start'))) / 60) | int }}", "distance_km": "{{ (states('input_number.tesla_trip_odometer_end') | float - states('input_number.tesla_trip_odometer_start') | float) | round(2) }}", "consumption_wh": "{{ ((states('input_number.tesla_trip_battery_start') | float - states('input_number.tesla_trip_battery_end') | float) / 100 * 61 * 1000) | round(2) }}" }

I keep getting the error "This action requires field message, which must be provided under 'data:'" under the action tab of developer tool

If I understood correctly that I need to use the file integraton to write into the txt I created

languid star
#

data and message are both indented too much. data should be at the top level. You have it under the target key

#

Use the UI mode to fire a simple message first (one without templates). Then switch to YAML mode and see how it is formatted.