Piloter Home Assistant depuis Node-Red grâce à l'API de HA

System Health

version 2021.1.1
installation_type Home Assistant Container
dev false
hassio false
docker true
virtualenv false
python_version 3.8.7
os_name Linux
os_version 5.4.83-v7l+
arch armv7l
timezone Europe/Paris
Home Assistant Cloud
logged_in false
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok
Lovelace
dashboards 1
mode storage
views 8
resources 0

HA 2021.1.1 on RPI4 4 GB under docker

Piloter Home Assistant depuis Node-Red

Hello,

Je voulais partager avec vous un sujet intéressant (du moins pour moi :grin:) : piloter des devices, entities et autres de Home Assistant à travers Node-Red et l’API de HA

Tout d’abord il faut créer un Token Bearer sur l’interface graphique de HA :
http://ipdevotreha:8123/profile et créer un Long-Lived Access Tokens

image

Donnez un nom à votre token puis OK

image

Sauvegardez votre token dans un KeePass car vous ne le reverrez plus après avoir cilqué sur OK

image

Une fois créé et sauvgardé, mettez votre token de côté et dirigez-vous sur votre page node-red
(PI : je fais tourner node-red sur mon rapsberry pi avec un autre conteneur)

Voilà le flow à utiliser. il faudra remplacer dans le noeud fonction le Bearer eyJ0eXAiOiJK… par le votre créé précédemment

[{"id":"acca563f.6da948","type":"link in","z":"8617ed72.5d487","name":"Cmd ha","links":["2a56342e.80c98c","9569985b.54d498","5d17b152.4b1a68","6ca9f6ef.01ace","35ab2740.d2dfc8","43e7319b.40754","78663c5c.7cabf4","68359306.94ccc4","412615b0.79bfac","bcef7641.0ec85","4c0d6edd.619eb","79debe31.530dd8","78edde81.f1fb08","f42972ca.5648c8","c3b7fc91.44f228","f0c54ab6.3fb548","e74e79f6.f1f1b","39c254f0.4f66fc","cbec2dea.9d075","3151189e.fbec88","d99ac0f.0cd974","c8b890fd.bc79a8","191807a5.6c23a","4c90d72e.9bb568","a566c2.db32e94","fe702b03.5b7e","a4210dfd.85d22","41a85b06.c149cc","162f542a.6cff7c","e02fa63d.2c68d","1e3bf324.c0596d","8fa115d0.7c409","d3726856.ed567","5dad99ac.569ca","2d4ecdcf.74a26a","18b989f3.6be0de","3e146b1d.30d084","98ffc469.3b558","15088965.aaef67","19f83d89.e80c3a","d85faf39.d98568","ba200c39.cc174","833c1bc1.7debe8","182dd63e.ecb622","1ef64b9.fee2a34","3e8b6980.102d2e","91a2db1b.5bad38","7729345f.608fa4","637d98e5.ddee18","33631ea7.f67582","668bf2f6.6ab1a4","d8319d5a.2b13a","430a9de0.44864c","86077415.6ee4d8","b9e25a8.b26f528","5c5b4607.84a4b","dfec0152.fe052","9b337b1e.d83df","3b5d4240.8f42fe","7648b959.3c996","7603ecd3.83f014","18f548f5.90c3af","6bbd7f2.c07e6","ee0b0290.3b921","5be90712.dc323","3d555f6a.c0926","abbc5f2a.73da18","8662a6eb.33fe","ee1b8131.798bc","a55ceb28.51e45","8d080e89.ad8b6","9f1c102c.c6c668","cbdd0d7b.a0b14","2634d02d.952798","993a7b19.31c22","ce1c8b40.109bf","2820efbd.372d58","9e507a33.0fc4d","3c9656ac.8d3272","ab9808b9.30bfa8","aa22a756.89f878"],"x":155,"y":100,"wires":[["d8637cfc.17758","6f598a2f.e71794"]]},{"id":"d8637cfc.17758","type":"function","z":"8617ed72.5d487","name":"Ha headers","func":"msg.headers={};\nmsg.headers={\"Authorization\": 'Bearer eyJ0eXAiOi...'};\nmsg.headers [\"content-type\"] = 'application/json';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":130,"y":195,"wires":[["48acc993.fdcd28","6f598a2f.e71794"]]},{"id":"48acc993.fdcd28","type":"json","z":"8617ed72.5d487","name":"To json","property":"payload","action":"str","pretty":false,"x":340,"y":200,"wires":[["f0001523.2a50c"]]},{"id":"f0001523.2a50c","type":"http request","z":"8617ed72.5d487","name":"cmd devices","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://192.168.1.223:8123/api/services/{{query.device_type}}/{{query.service}}","tls":"","persist":true,"proxy":"","authType":"","x":530,"y":200,"wires":[["df2a0a08.8b3b48"]]},{"id":"df2a0a08.8b3b48","type":"debug","z":"8617ed72.5d487","name":"debug function finale","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","statusVal":"","statusType":"auto","x":280,"y":280,"wires":[]},{"id":"6f598a2f.e71794","type":"debug","z":"8617ed72.5d487","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":370,"y":100,"wires":[]},{"id":"833db7ea.0e82e8","type":"comment","z":"8617ed72.5d487","name":"Cmd devices","info":"","x":90,"y":20,"wires":[]}]

Pensez à faire un deploy pour conserver les modifications

Par exemple, je souhaitais faire un bouton qui revient à l’état d’origine au bout d’un certain temps.
Je suis passé par node-red afin que ce soit transparent dans HA

[{"id":"7bcfb492.596d6c","type":"mqtt in","z":"12339730.c71e41","name":"Ouverture portail ON","topic":"homeassistant/switch/fibaro_system_unknown_type_0204_id_1000_switch/state","qos":"2","datatype":"auto","broker":"d32f05f2.4d0038","x":120,"y":80,"wires":[["b82219b.9f65a68"]]},{"id":"b82219b.9f65a68","type":"switch","z":"12339730.c71e41","name":"Portail State ON","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":378,"y":80,"wires":[["db5cbdbc.ad11c"]]},{"id":"db5cbdbc.ad11c","type":"delay","z":"12339730.c71e41","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":568,"y":80,"wires":[["184d677f.7b10b1"]]},{"id":"184d677f.7b10b1","type":"change","z":"12339730.c71e41","name":"Button OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"entity_id\":\"switch.fibaro_system_unknown_type_0204_id_1000_switch\"}","tot":"str"},{"t":"set","p":"query.device_type","pt":"msg","to":"switch","tot":"str"},{"t":"set","p":"query.service","pt":"msg","to":"turn_off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":748,"y":80,"wires":[["d3726856.ed567"]]},{"id":"d32f05f2.4d0038","type":"mqtt-broker","name":"MQTT BROKER","broker":"192.168.1.223","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Voilà ! J’espère que ça vous aidera ! Si vous avez des questions, n’hésitez pas !
Alors je débute dans le monde de la domotique donc j’essaierai quand même de vous apporter les réponses en fonction de mes connaissances !

2 J'aime

Merci pour le partage :+1:

2 J'aime

Merci pour le partage.

Au lieu de faire directement des requêtes HTTP, tu devrais utiliser node-red-contrib-home-assistant-websocket, pour plusieurs raisons:

  • Les requêtes sont mises à jour au sein des noeuds au fur à mesure des évolutions de HA
  • Tu peux te concentrer sur tes scénarios sans avoir à gérer le protocole de communicaion
  • Tu as une quinzaine de noeuds prêt à l’emploi pour piloter HA (entities, appel de service, évènements…)
1 J'aime

Avec plaisir @evenisse

J’ai testé cette palette. L’idée que j’ai eue est si la personne qui fait le flow home assistant ne le maintient plus, j’ai cette solution universelle qui restera toujours d’actualité !
Mais j’utilise les deux !

Avec 30 contributeurs, je doute que le support s’arrête et quasiment tous les utilisateurs du couple HA/Node-red l’utilisent.
Il y aura forcément des gens pour prendre la suite si ça doit arriver, surtout que le code derrière chaque node est simple à maintenir.