Supla<->MQTT - Sterowanie

Beku
Posty: 453
Rejestracja: wt lis 06, 2018 1:49 pm

W tym wątku opiszę jak można za pomocą brokera MQTT i klienta MQTT dla Supli sterować urządzeniami w Supli.
Generalnie zasada jest nastepująca: klient MQTT dla Supli subskrybuje wybrane tematy i reaguje kiedy te zostaną opublikowane w brokerze MQTT.


Cała konfiguracja dostępna jest w pliku command.yaml znajdującym się w katalogu supla-core/supla-mqtt-client/config. nie musimy jej zmieniać - wystarczy że zrozumiemy na jakiej zasadzie działają już wprowadzone konfiguracje.

Kod: Zaznacz cały

# template for power switch 
# payload: { "ident": { "id": 2 }, "on": 1 }
- topic: 'supla/channels/command/#'
  id: '/ident/id'
  on_off: '/on'
  on_value: 1
  off_value: 0
  
 #template for gate, garage door
 # payload: {"idx": 4, "hi": 1}
 - topic: 'supla/channels/command/#'
   id: '/idx'
   shut: '/hi'

# template for light switch
# payload: { "idx": 4, "on": true }
- topic: 'supla/channels/command/#'
  id: '/idx'
  on_off: '/on'
  on_value: true
  off_value: false

# template for rollershutter
# payload: { "id" : 2, "shut": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  shut: '/shut'

# template for dimmer 
# payload: { "id" : 2, "values": { "on": 1, "brightness": 49 } }
- topic: 'supla/channels/command/#'
  id: '/id'
  brightness: '/brightness'
  on_off: '/on'
  on_value: 1
  off_value: 0

# template for rgblightning 2
# payload: { "id" : 2, "on" : true, colors: {"r": 0, "g": 255, "b": 127 }, "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color_r: '/colors/r'
  color_b: '/colors/b'
  color_g: '/colors/g'
  color_brightness: '/c_brightness'
  on_off: 'on'
  on_value: true
  off_value: false
Jak widać powyżej klient MQTT dla Supli obsługuje sterowanie zasadniczo wszystkim czym w chwili obecnej można sterować w Supli ;) Kolejne rzeczy będą dodawane sukcesywnie.

Definicja komend polega na określeniu ścieżki do poszczególnych wartości w przychodzącej zawartości tematu.

Dajmy na to chcemy sterować oświetleniem. Mamy w Supli włącznik światła o ID = 1

Kod: Zaznacz cały

# template for power switch 
# payload: { "ident": 1, "on": 1 }
- topic: 'supla/channels/command/light/#'
  id: '/ident'
  on_off: '/on'
  on_value: 1
  off_value: 0
Powyższa konfiguracja oznacza:

Jeśli do brokera MQTT zostanie wysłany temat supla/channels/command/light/N (gdzie N to dowolny ciąg znaków - znak # powoduje że wszystko co po jego prawej w temacie jest ignorowane) o zawartości {"ident": 1, "on": 1} to zostanie włączone światło o ID 1 w Supli. Analogicznie jeśli chcemy wyłączyć światło wysyłamy do brokera MQTT pod ten temat payload: {"ident": 1, "on": 0}
Warto zaznaczyć że nie musimy definiować osobnej konfiguracji dla pozostałych włączników światła. Wystarczy że w payload wyślemy pod ten temat {"ident": 2, "on": 1} i jeśli tylko mamy w Supli włącznik światła o ID = 2 to zadziała z automatu.
Ta konfiguracja to szablon.

Podobnie jest to zorganizowane w pozostałych kanałach, w zależności od rodzaju należy wskazać gdzie klient MQTT Supli ma szukać wartości do sterowania.

Zmieniając ten plik należy mieć na uwadze:

1. Payload musi być w formacie JSON
2. Payload musi zawierać identyfikator kanału (chociaż pod dowolną strukturą).
3. Wyszukiwanie w Payload realizowane jest za pomocą JSONPath np.
4. Plik jest w formacie YAML i musi być prawidłowy pod kątem tego formatu. Można to sprawdzić wklejając jego zawartośc np. tutaj: http://www.yamllint.com/
mariusz_70
Posty: 104
Rejestracja: czw lis 10, 2016 5:51 pm
Lokalizacja: Koszalin

@Beku mam problem z bramą. Instalację "Supla MQTT Client" zrobiłem zanim powstał ten wątek, widzę że tutaj w pliku command.yaml jest dodatkowa komenda:

Kod: Zaznacz cały

  
 # template for gate, garage door
 # payload: {"idx": 4, "hi": 1}
 - topic: 'supla/channels/command/#'
   id: '/idx'
   hi: '/hi'  
w moim takiej komendy nie było, dokonałem edycji command.yaml i dodałem ją, ale putty wypluwa błędy:

Kod: Zaznacz cały

login as: pi
pi@192.168.178.150's password:
Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec  5 16:56:34 2019 from 192.168.178.124
pi@raspberrypi:~ $ cd supla-core/supla-mqtt-client/Release
pi@raspberrypi:~/supla-core/supla-mqtt-client/Release $ ./supla-mqtt-client -config /home/pi/supla-core/supla-mqtt-client/config/config_template.yaml
INFO[1575565393.197143] initializing main loop
INFO[1575565393.201578] Client GUID: 316FDD0190594729968AA9ED0C746491
INFO[1575565393.214006] SSL version: OpenSSL 1.1.0l  10 Sep 2019
INFO[1575565393.230918] Connecting...
ERR[1575565664.556841] error while trying get value from payload [error: Name not found]
ERR[1575565664.557227] error while trying get value from payload [error: Not an integer]
ERR[1575565664.557413] error while trying get value from payload [error: Name not found]
ERR[1575565664.557566] error while trying get value from payload [error: Name not found]
ERR[1575565664.557711] error while trying get value from payload [error: Name not found]
ERR[1575565664.557847] error while trying get value from payload [error: Name not found]
ERR[1575565664.557994] error while trying get value from payload [error: Not an integer]  
Dokonałem reinstalacji "Supla MQTT Client" i w command.yaml znowu brakuje w/w wpisu, ponownie dodałem komendę do sterowania bramą ale efekt jest taki sam jak powyżej.
Beku
Posty: 453
Rejestracja: wt lis 06, 2018 1:49 pm

Te błędy są „normalne” nie powinienem ich nawet pokazywać. Biorą się z tego że subskrybuje klient dla wszystkich kanałów ten sam temat i nie wie który szablon wybrać do sterowania wiec próbuje z każdego szablonu sparsować wartości. To nie powinno mieć wpływu na samo sterowanie.


[Edit]:

Zmień w command.yaml dla bramy z

Kod: Zaznacz cały

hi: '/hi'
na

Kod: Zaznacz cały

shut: '/hi'
mariusz_70
Posty: 104
Rejestracja: czw lis 10, 2016 5:51 pm
Lokalizacja: Koszalin

Brama nie działa, wysyłam : {"idx": 4, "hi": 1}

Kod: Zaznacz cały

  # The file contains configuration of channel control commands.
# If the controlled function is not listed below, it will not be controlled.
# The feature configuration scheme is as follows:
#
# topic: string [mandatory] - the topic to be subscribed to
# id: string [mandatory] - jsonPath to toe value denoting the channel identifier
#
# Depending on the type of function, the configuration may/must include:
# 
# on_off: string [mandatory for 20,30,90,130,140,180,190,200,300] - jsonPath to the value 
# indicating whether the device is to be turned on or off
# on_value: string - format of the on value [default 1]
# off_value: string - format of the off value [default 0]
# brightness: string optional for 180, 190, 200 - jsonPath to the value indicating brightness has # to be set from 0 to 100
# color_brightness: string optional for 190, 200 as above for brightness of color 
# color: string -> jsonPath to the color value in HEX format (see below)
# color_r: string -> jsonPath to the red color component
# color_b: string -> jsonPath to the blue color component
# color_g: string -> jsonPath to the green color component
# If device has rgb function the color or color components may be used
# shut: string -> jsonPath to the percentage of blinds opening level from 0 to 100 where 0 means 
# open and 100 closed

# template for power switch 
# payload: { "ident": { "id": 2 }, "on": 1 }
- topic: 'supla/channels/command/#'
  id: '/id'
  on_off: '/on'
  on_value: 1
  off_value: 0

# template for light switch
# payload: { "idx": 4, "on": true }
- topic: 'supla/channels/command/#'
  id: '/idx'
  on_off: '/on'
  on_value: true
  off_value: false

# template for rollershutter
# payload: { "id" : 2, "shut": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  shut: '/shut'

# template for dimmer 
# payload: { "id" : 2, "values": { "on": 1, "brightness": 49 } }
- topic: 'supla/channels/command/#'
  id: '/id'
  brightness: '/brightness'
  on_off: '/on'
  on_value: 1
  off_value: 0

# template for rgblightning
# payload: { "id" : 2, "on" : true, color: "0x0000FF", "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color_r: '/red'
  color_g: '/green'
  color_b: '/blue'
  color_brightness: '/color_brightness'
  brightness: '/brightness'
  on_off: '/on'
  on_value: true
  off_value: false

# template for rgblightning 2
# payload: { "id" : 2, "on" : true, colors: {"r": 0, "g": 255, "b" }, "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color_r: '/colors/r'
  color_b: '/colors/b'
  color_g: '/colors/g'
  color_brightness: '/c_brightness'
  on_off: 'on'
  on_value: true
  off_value: false
  
# template for gate, garage door
# payload: {"idx": 4, "hi": 1}
- topic: 'supla/channels/command/#'
  id: '/idx'
  hi: '/hi'   
Beku
Posty: 453
Rejestracja: wt lis 06, 2018 1:49 pm

mariusz_70 pisze: czw gru 05, 2019 7:48 pm Brama nie działa, wysyłam : {"idx": 4, "hi": 1}

Kod: Zaznacz cały

  # The file contains configuration of channel control commands.
# If the controlled function is not listed below, it will not be controlled.
# The feature configuration scheme is as follows:
#
# topic: string [mandatory] - the topic to be subscribed to
# id: string [mandatory] - jsonPath to toe value denoting the channel identifier
#
# Depending on the type of function, the configuration may/must include:
# 
# on_off: string [mandatory for 20,30,90,130,140,180,190,200,300] - jsonPath to the value 
# indicating whether the device is to be turned on or off
# on_value: string - format of the on value [default 1]
# off_value: string - format of the off value [default 0]
# brightness: string optional for 180, 190, 200 - jsonPath to the value indicating brightness has # to be set from 0 to 100
# color_brightness: string optional for 190, 200 as above for brightness of color 
# color: string -> jsonPath to the color value in HEX format (see below)
# color_r: string -> jsonPath to the red color component
# color_b: string -> jsonPath to the blue color component
# color_g: string -> jsonPath to the green color component
# If device has rgb function the color or color components may be used
# shut: string -> jsonPath to the percentage of blinds opening level from 0 to 100 where 0 means 
# open and 100 closed

# template for power switch 
# payload: { "ident": { "id": 2 }, "on": 1 }
- topic: 'supla/channels/command/#'
  id: '/id'
  on_off: '/on'
  on_value: 1
  off_value: 0

# template for light switch
# payload: { "idx": 4, "on": true }
- topic: 'supla/channels/command/#'
  id: '/idx'
  on_off: '/on'
  on_value: true
  off_value: false

# template for rollershutter
# payload: { "id" : 2, "shut": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  shut: '/shut'

# template for dimmer 
# payload: { "id" : 2, "values": { "on": 1, "brightness": 49 } }
- topic: 'supla/channels/command/#'
  id: '/id'
  brightness: '/brightness'
  on_off: '/on'
  on_value: 1
  off_value: 0

# template for rgblightning
# payload: { "id" : 2, "on" : true, color: "0x0000FF", "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color_r: '/red'
  color_g: '/green'
  color_b: '/blue'
  color_brightness: '/color_brightness'
  brightness: '/brightness'
  on_off: '/on'
  on_value: true
  off_value: false

# template for rgblightning 2
# payload: { "id" : 2, "on" : true, colors: {"r": 0, "g": 255, "b" }, "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color_r: '/colors/r'
  color_b: '/colors/b'
  color_g: '/colors/g'
  color_brightness: '/c_brightness'
  on_off: 'on'
  on_value: true
  off_value: false
  
# template for gate, garage door
# payload: {"idx": 4, "hi": 1}
- topic: 'supla/channels/command/#'
  id: '/idx'
  hi: '/hi'   
Edytowałem moja odpowiedz wyżej.
mariusz_70
Posty: 104
Rejestracja: czw lis 10, 2016 5:51 pm
Lokalizacja: Koszalin

Wielkie dzięki działa!!!
Beku
Posty: 453
Rejestracja: wt lis 06, 2018 1:49 pm

mariusz_70 pisze: czw gru 05, 2019 7:59 pm Wielkie dzięki działa!!!
Super! 😊
mariusz_70
Posty: 104
Rejestracja: czw lis 10, 2016 5:51 pm
Lokalizacja: Koszalin

@Beku walczę kilka dni z RGB - nie mogę zmienić koloru, mam moduł Zamela SLW-01.
W MQTT Explorer mam taki odczyt z modułu:

Kod: Zaznacz cały

{"id": 2, "on": 0, "color": "0xFFFFFF", "color_brightness": 100, "online": 1}
aby wyłączyć moduł wysyłam:

Kod: Zaznacz cały

{"id": 2, "color_brightness": 0 }
włączenie modułu to:

Kod: Zaznacz cały

{"id": 2, "color_brightness": 100 }
i to działa, listwa led zapala się ze 100% jasnością, nie używam parametru "on" bo w module nie ma przekaźnika.
I teraz chcę zmienić kolor z białego (0xFFFFFF) na czerwony (0xFF0000) komenda:

Kod: Zaznacz cały

{"id": 2, "color": "0xFF0000" }
Brak reakcji.
Wysyłam rozszerzoną z parametrem jasności:

Kod: Zaznacz cały

{"id": 2, "color": "0xFF0000", "color_brightness": 50 }
jasność zmienia się na 50%, a kolor bez zmian.
W command.yaml mam taki wpis:

Kod: Zaznacz cały

# template for rgblightning
# payload: { "id" : 2, "on" : true, color: "0x0000FF", "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color: '/color'
  color_brightness: '/color_brightness'
  brightness: '/brightness'
  on_off: '/on'
  on_value: true
  off_value: false
Beku
Posty: 453
Rejestracja: wt lis 06, 2018 1:49 pm

w tej chwili działa tylko sterowanie RGB poprzez podanie RGB jak poniżej:

Kod: Zaznacz cały

# template for rgblightning 2
# payload: { "id" : 2, "on" : true, colors: {"r": 0, "g": 255, "b": 127}, "color_brightness": 50 }
- topic: 'supla/channels/command/#'
  id: '/id'
  color_r: '/colors/r'
  color_b: '/colors/b'
  color_g: '/colors/g'
  color_brightness: '/c_brightness'
  on_off: 'on'
  on_value: true
  off_value: false
Sibikk
Posty: 366
Rejestracja: pn lis 07, 2016 12:42 pm
Lokalizacja: Katowice
Kontakt:

Cześć, czy udało się komuś skonfigurować dimmer w HA tak żeby działało ściemnianie z suwaka? Kombinuję na różne sposoby ale mi nie idzie.
Obecnie mam tak:

Kod: Zaznacz cały

- platform: mqtt
   schema: template
   name: "led1"
   state_topic: "supla/channels/status/dimmer/41349"
   command_topic: "supla/channels/command/41349"
   command_on_template: '{ "id" : 41349, "brightness": 100 }'
   command_off_template: '{ "id" : 41349, "brightness": 0 }'
   state_template: "{{ 'on' if value_json.brightness > (0) else 'off' }}"
   brightness_template: '{{ value_json.brightness }}'
Obrazek
ODPOWIEDZ

Wróć do „MQTT”