Liebes Forum,
gefunden habe ich dazu leider per Suche nichts. Ich habe zwei Teslas. Kann ich den Datenpunkt mqtt.0.openWB.chargepoint.2.get.connected_vehicle.info (oder einen anderen) auch von extern, z.B. aus ioBroker setzen? Hintergrund ist, dass ioBroker (per Geoinformation) weiß, welches Fahrzeug an der OpenWB steht. Diese info würde ich dann einfach gerne in die OpenWB schreiben ...
Danke!
Angeschlossenes Fahrzeug per MQTT setzen
-
Elektromanni
- Beiträge: 46
- Registriert: Di Jan 26, 2021 11:44 am
- Has thanked: 1 time
- Been thanked: 1 time
-
openWB
- Site Admin
- Beiträge: 10264
- Registriert: So Okt 07, 2018 1:50 pm
- Has thanked: 175 times
- Been thanked: 408 times
Re: Angeschlossenes Fahrzeug per MQTT setzen
Wenn du es dir einfach machen willst:
https://wiki.openwb.de/doku.php?id=open ... e_anfragen
Code: Alles auswählen
# Fahrzeug 1 zu Ladepunkt 3 zuordnen
curl -X POST -d "vehicle=1&chargepoint_nr=3" \
"http://IPADRESSE/openWB/simpleAPI/simpleapi.php"Supportanfragen bitte NICHT per PN stellen.
Hardwareprobleme bitte über die Funktion Debug Daten senden mitteilen oder per Mail an support@openwb.de
Hardwareprobleme bitte über die Funktion Debug Daten senden mitteilen oder per Mail an support@openwb.de
-
Elektromanni
- Beiträge: 46
- Registriert: Di Jan 26, 2021 11:44 am
- Has thanked: 1 time
- Been thanked: 1 time
Re: Angeschlossenes Fahrzeug per MQTT setzen
Danke für die Antwort. Falles es auch Andere umsetzen wollen. Hier mein ioBroker-Sript. Es braucht nur den ioBroker-Adapter "Tesla" und dann setzt ioBroker automatisch das richtige Fahrzeug in die WB. VIN, ggfs. Kennzeichen und jedenfalls Koordinaten müssen natürlich angepasst werden:
Code: Alles auswählen
// Auto-Zuordnung des Fahrzeugs am OpenWB-Chargepoint via SimpleAPI.
//
// Funktionsweise:
// - Beim Stecker-Wechsel ODER Tesla-Position-Wechsel ODER alle 5 min:
// - Berechne Haversine-Abstand jedes Tesla zum Haus
// - Auto, das näher als HOME_RADIUS_M (20 m) ist, gilt als "an der Wallbox"
// - Wenn genau EINES nahe ist und die OpenWB ein anderes meldet,
// per HTTP-POST zur SimpleAPI umstellen.
const axios = require('axios');
const HOME_LAT = xxxxxx;
const HOME_LON = yyyyyy;
const HOME_RADIUS_M = 20;
const OPENWB_URL = 'http://10.0.1.220/openWB/simpleAPI/simpleapi.php';
const CHARGEPOINT_NR = 2;
const CARS = [
{ vin: 'xxxx', vehicleId: 2, label: 'Model 3 (Kennzeichen xxxxx)' },
{ vin: 'xxxx', vehicleId: 4, label: 'Model Y (Kennzeichen yyyy)' },
];
// Haversine-Distance in Metern
function distanceM(lat1, lon1, lat2, lon2) {
const R = 6371000;
const toRad = Math.PI / 180;
const dLat = (lat2 - lat1) * toRad;
const dLon = (lon2 - lon1) * toRad;
const a = Math.sin(dLat/2) ** 2 +
Math.cos(lat1 * toRad) * Math.cos(lat2 * toRad) *
Math.sin(dLon/2) ** 2;
return 2 * R * Math.asin(Math.sqrt(a));
}
function getCarLatLon(vin) {
try {
const lat = parseFloat(getState(`tesla-motors.0.${vin}.drive_state.latitude`).val);
const lon = parseFloat(getState(`tesla-motors.0.${vin}.drive_state.longitude`).val);
if (!isFinite(lat) || !isFinite(lon)) return null;
return [lat, lon];
} catch (e) {
return null;
}
}
function getCurrentConnectedVehicleId() {
try {
const raw = getState('mqtt.0.openWB.chargepoint.2.get.connected_vehicle.info').val;
if (!raw) return null;
const obj = JSON.parse(raw);
return obj && obj.id != null ? Number(obj.id) : null;
} catch (e) {
return null;
}
}
async function reassignVehicle(vehicleId, label) {
log(`OpenWB-Auto-Assign: switching to vehicle=${vehicleId} (${label})`);
try {
const res = await axios.post(
OPENWB_URL,
`vehicle=${vehicleId}&chargepoint_nr=${CHARGEPOINT_NR}`,
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
timeout: 5000,
}
);
if (res.data && res.data.success) {
log(` -> OK: ${res.data.message}`);
} else {
log(` -> SimpleAPI antwortete ohne success: ${JSON.stringify(res.data)}`, 'warn');
}
} catch (e) {
log(` -> Fehler beim POST: ${e.message}`, 'error');
}
}
function evaluate() {
const candidates = [];
for (const car of CARS) {
const pos = getCarLatLon(car.vin);
if (!pos) continue;
const d = distanceM(pos[0], pos[1], HOME_LAT, HOME_LON);
if (d <= HOME_RADIUS_M) {
candidates.push({ ...car, distance: d });
}
}
if (candidates.length === 0) {
// Kein Tesla in Reichweite -- nichts tun (vielleicht ein anderes Auto angesteckt
// oder Geofencing kurz aussetzig)
return;
}
if (candidates.length > 1) {
// Beide nah -- mehrdeutig, lieber nichts tun
log(`OpenWB-Auto-Assign: zwei Teslas <= ${HOME_RADIUS_M} m, mehrdeutig — kein Switch`);
return;
}
const target = candidates[0];
const currentId = getCurrentConnectedVehicleId();
if (currentId === target.vehicleId) {
// schon korrekt zugeordnet, nichts tun
return;
}
reassignVehicle(target.vehicleId, target.label);
}
// Trigger: Stecker-Wechsel
on({ id: 'mqtt.0.openWB.chargepoint.2.get.plug_state', change: 'any' }, evaluate);
// Trigger: Tesla-Position-Updates (nur wenn lc tatsaechlich aendert)
for (const car of CARS) {
on({ id: `tesla-motors.0.${car.vin}.drive_state.latitude`, change: 'ne' }, evaluate);
on({ id: `tesla-motors.0.${car.vin}.drive_state.longitude`, change: 'ne' }, evaluate);
}
// Sicherheitsnetz: alle 5 Minuten
schedule('*/5 * * * *', evaluate);
// Initial-Check beim Skript-Start
evaluate();