yp87 / leaf2mqtt

Pull in data from the Nissan Connect platform for the LEAF and publish over MQTT
GNU General Public License v3.0
42 stars 39 forks source link

stats - help needed #32

Closed guni12 closed 2 years ago

guni12 commented 2 years ago

I don't seem to manage to handle the stats - Status nor Command. By creating a sensor like "leaf/{vin}/tripHistory/0/summaries/0" I do get these values, but with dates that I can't control. I have been playing around with leaf/{vin}/command/stats/{TimeRange} (daily or monthly) but it gives me nothing. leaf

Perhaps the command is correct and I'm just stupid. Perhaps you could add something to the ReadMe for people like me?

sensor-freak commented 2 years ago

Are the double quotes part of the payload? If so, try top remove them.

Also, had sucess using payload like update 2022-05-01.

guni12 commented 2 years ago

Oh, I missed that you had answered me directly. Thank you so much for that. Unfortunately I have tried without double quotes as well. It does not seem that the stats get sent by topic stats, but by topic tripHistory. And perhaps this is how I should send the command also somehow. [image: image.png] If you know something about this I'd be very grateful :-)

/Gunvor

Den fre 27 maj 2022 kl 08:28 skrev Franz-Josef Kaiser < @.***>:

Are the double quotes part of the payload? If so, try top remove them.

Also, had sucess using payload like update 2022-05-01.

— Reply to this email directly, view it on GitHub https://github.com/yp87/leaf2mqtt/issues/32#issuecomment-1139323550, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA55KTQIPVN3RIVWGVKOZD3VMBTPLANCNFSM5WZPSXMQ . You are receiving this because you authored the thread.Message ID: @.***>

-- Med Vänlig Hälsning

Gunvor Nilsson

guni12 commented 2 years ago

Sorry, I answered by mail and the image didn't get included here. image I don't think stats is the correct topic.

sensor-freak commented 2 years ago

Oh, stupid me...

The daily stats command does not handle a date argument with the update command. Any arguments given are ignored. Instead it internally always uses the current date as the argument.

The date argument ist handled by the monthly stats only.

I changed this in my own fork, but forgot to mention that.

And the original topic should bei correct, the topics are different from the structure of the json data.

guni12 commented 2 years ago

Sorry to bother you again. Thank you for more clarification and trying to help.

But my dilemma remains. I don't get anything back via the topic stats, not as a sensor either. And the date-info given to me are, as you can see, from 1st August last year.

Do you formulate the sensor like this? leaf/stats/daily/milesPerKwh

If current date would be retrievable, that would be fantastic.

sensor-freak commented 2 years ago

According to the README.md (and my brain) the MQTT topics are like leaf/{vin}/stats/{TimeRange}/kilometersPerKwh. There ist no tripsHistory contained.

So the sensors probably should be defined using that topic name.

I will check next week if I can give additional hints.

guni12 commented 2 years ago

hello again, my mqtt sensor-tests:

- name: leaf_triphistory_distance state_topic: "leaf/SJNFAAZE1U0122615/tripHistory/0/summaries/0" unique_id: leaf_triphistory_distance value_template: "{{ value_json.distance }}"

which renders a value and

- name: leaf_statsdaily state_topic: "leaf/stats/daily/kilometersPerKwh" unique_id: "leaf_statsdaily" value_template: "{{ value_json }}"

which renders unknown.

Where do I go wrong?

sensor-freak commented 2 years ago

Hi,

maybe the result you get is a retained message, that is a message that has been stored in the MQTT broker and that will be delivered whenever one subscribes to the topic leaf/SJNFAAZE1U0122615/tripHistory/0/summaries/0. This would explain why it is a very old date which never changes. Have to used other tools that might have generated such a retained message?

On the other hand, if you use a recent version of leaf2mqtt, the broker should be able to deliver something on the topic leaf/stats/daily/kilometersPerKwh (and also on leaf/SJFN.../stats/daily/kilometersPerKwh). Maybe you should first make sure that the broker indeed publishes those topics, and also that the data value of those topics is a simple float value.

Also, I'm not sure about the part {{ value_json }}: is this sufficient to parse a value like 14.51? (That's a topic value published by my broker.)

Have you tried to use a generic MQTT client (like MQTT.fx, MQTT Explorer, ...) and subscribe to something like leaf/# to make sure that the topics you expect are really published?

To summarize: I don't believe, that the topic containing tripHistory has been created by leaf2mqtt.

Hope this helps. Feel free to bother me again ;-)

guni12 commented 2 years ago

Thank you for letting me bother you. You are a champ!

You helped me some - endeed the tripHistory were some old values retained from last year (now thrown away). Last year I got some success with a node red script, but then it stopped allowing me (HA) to login to home connect.

leaf

MQTT Explorer is the tool I already use. And I get all the other values, like everything under topics battery, climate, cockpitStatus. Sensors like this:

- name: leaf_mileage state_topic: "leaf/cockpit/totalMileage" unit_of_measurement: 'km' state_class: "measurement" unique_id: 'leaf_mileage'

and I removed the unnecessary {{ value_json }} for the stats-experiment above. But no change.

Next year we will probably have another car, so this is not the end of the world. But having the km-value would be really good for automations.

sensor-freak commented 2 years ago

Fine to see some progress.

It seems that the cockpittopic as well as the cockpitStatus are published by the MQTT broker. (These items should contain identical values, but that's a different story). So regarding leaf2mqtt everything might be working perfectly. And the screen dump from your MQTT Explorer indicates that, too.

So the only thing I can think of is the sensor definition. Your example looks somehow like yaml, but I'm not sure about the correct formatting. Also, maybe you can check the logs from Home Assistant (?), if there are any parsing errors. Or do have to specify somehow that the value is a floating point number?

I do not use Home Assistant, so I don't believe that I can help you there.

guni12 commented 2 years ago

Thank you for trying and giving me so much attention!

I don't think my sensors are wrong, I have a whole bunch of them, and when listening to everything from leaf2mqtt inside Home Assistant there is nothing with the topic stats. It was I who noticed a typo some weeks ago, that cockpitStatus was lacking and only cockpit was available, which was corrected after that, with backward compatibility.

Anyhow. I give up now and I am really grateful for your effort.

yp87 commented 2 years ago

Hello @guni12,

The library leaf2mqtt is using behind the scene to talk to the Nissan service connected to your car year and region, does not allow to specify a daily date. So the date you give in the update command will not be used (the library always uses the current day).

In my case, I use another one of the 3 Nissan Api because of my car year and region, I can specify a daily date and it will be used by the library. However, it looks like I have no valid data since October last year (same in the official app) and this will cause the library to not return any data. Something like this could explain the behavior you see in mqtt explorer (no stats entry)

You could verify with the official app if you can get this data.

Otherwise, I have a work around for you: 1) Create an input_number called input_number.yesterday_leaf_total_km. 2) Create an automation that starts at 21h30 and gets the total km from the cockpitStatus and put it in input_number.yesterday_leaf_total_km. 3) Whenever you want to know today's km, you can fetch the latest cockpit status and substract input_number.yesterday_leaf_total_km from it. (This can be put into another input_number)

Maybe there is a way to fetch the historical value of an input_number in home assistant if you need the km from a previous date.

hope this helps!

@sensor-freak thanks for providing help!