Ziel der Übung ist es eine oder mehrere Variablen in ein JSON zu verpacken und das Ganze dann per curl POST in einen Homeassistant-Sensor zu schreiben.
Das valide und funktionale json sieht so aus:
Code: Alles auswählen
curl -X POST http://192.168.113.11:8123/api/states/input_number.strompreis
-H "Authorization: Bearer baxxy_kann_das_nicht123" \
-H "Content-Type: application/json" \
-d '{"state": "0.37", "attributes": {"unit_of_measurement": "€/kWh", "editable": "true", "min": "0", "max": "2", "step": "1", "mode": "box", "icon": "mdi:currency-eur", "friendly_name": "Strompreis_Test"}}' \
Code: Alles auswählen
curl -X POST http://192.168.113.11:8123/api/states/input_number.strompreis -H "Authorization: Bearer baxxy_kann_das_nicht123" -H "Content-Type: application/json" -d "{\"state\": \"0.37\", \"attributes\": {\"unit_of_measurement\": \"€/kWh\", \"editable\": \"true\", \"min\": \"0\", \"max\": \"2\", \"step\": \"1\", \"mode\": \"box\", \"icon\": \"mdi:currency-eur\", \"friendly_name\": \"Strompreis_Test\"}}"
Code: Alles auswählen
var value = 0.36;
string curlcmd = "curl -v -X POST http://192.168.113.11:8123/api/states/input_number.strompreis -H ";
string jsondaten = '"Authorization: Bearer baxxy_kann_das_nicht123" -H "Content-Type: application/json" -d "{\"state\": \"'#value#'\", \"attributes\": {\"unit_of_measurement\": \"€/kWh\", \"editable\": \"true\", \"min\": \"0\", \"max\": \"2\", \"step\": \"1\", \"mode\": \"box\", \"icon\": \"mdi:currency-eur\", \"friendly_name\": \"Strompreis_Test\"}}"';
string combined = curlcmd + jsondaten;
string out;
system.Exec(combined ,&out);
WriteLine(out);
Code: Alles auswählen
{"message":"Invalid JSON specified."}
Wäre super wenn jemand funktionale Hinweise oder besser noch gleich ein funktionale Beispiel hätte, gerne auch mit meinen Beispieldaten.