domoticafacilconjota / capitulos

Copy y paste del canal
39 stars 50 forks source link

[AtoNodeRED] Cuenta las personas que hay actualmente en casa [Resuelto] #71

Closed gtrabanco closed 2 years ago

gtrabanco commented 2 years ago

Requisitos

Screenshot 2021-12-08 at 02 11 53

Código de la automatización

[{"id":"a88bf4f353498b06","type":"subflow","name":"People @ home","info":"","category":"","in":[{"x":60,"y":200,"wires":[{"id":"3bd3d58638b67231"}]}],"out":[{"x":820,"y":120,"wires":[{"id":"a5ab5e6c9de58592","port":0}]},{"x":820,"y":200,"wires":[{"id":"a5ab5e6c9de58592","port":1}]},{"x":820,"y":280,"wires":[{"id":"5a3a701ab57f12db","port":1}]}],"env":[{"name":"req_people","type":"str","value":"all","ui":{"icon":"font-awesome/fa-user","label":{"en-US":"Required People"},"type":"select","opts":{"opts":[{"l":{"en-US":"all"},"v":"all"},{"l":{"en-US":"any"},"v":"any"},{"l":{"en-US":"none"},"v":"none"}]}}}],"meta":{"module":"people_home","version":"1.0.0","author":"Gabriel Trabanco Llano <gtrabanco@users.noreply.github.com>","desc":"Used to get if there are people at home."},"color":"#DDAA99","icon":"font-awesome/fa-home","status":{"x":820,"y":380,"wires":[{"id":"ac98ef934def58c9","port":0},{"id":"97de79323b17caca","port":0}]}},{"id":"ac98ef934def58c9","type":"change","z":"a88bf4f353498b06","name":"on","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":340,"wires":[[]]},{"id":"97de79323b17caca","type":"change","z":"a88bf4f353498b06","name":"off","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":420,"wires":[[]]},{"id":"3bd3d58638b67231","type":"ha-get-entities","z":"a88bf4f353498b06","name":"","server":"2934e1c9.95a54e","version":0,"rules":[{"property":"entity_id","logic":"starts_with","value":"person","valueType":"str"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"people","output_results_count":1,"x":190,"y":200,"wires":[["5a3a701ab57f12db"]]},{"id":"5a3a701ab57f12db","type":"function","z":"a88bf4f353498b06","name":"","func":"const checkOption=env.get(\"req_people\");\nconst maximumPeople=msg.people.length;\nvar people = [];\nvar payload = \"off\";\n\nmsg.people.map((p) => {\n    if (p.state == \"home\") {\n        people.push(p.entity_id);\n    }\n})\n\nswitch(checkOption) {\n    case \"all\":\n        if(maximumPeople == people.length) {\n            payload = \"on\";\n        }\n        break;\n        \n    case \"any\":\n        if(people.length > 0) {\n            payload = \"on\";\n        }\n        break;\n    \n    case \"none\":\n        if (people.length == 0) {\n            payload = \"on\";\n        }\n        break;\n}\n\nreturn [ {payload}, {people} ];\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":200,"wires":[["a5ab5e6c9de58592"],[]]},{"id":"a5ab5e6c9de58592","type":"switch","z":"a88bf4f353498b06","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":530,"y":160,"wires":[["ac98ef934def58c9"],["97de79323b17caca"]]},{"id":"7cd69c6d9b705517","type":"tab","label":"Automate Count People @ Home","disabled":false,"info":"This automation counts the current people at home changing the value every time any of the people entity changes its value.","env":[]},{"id":"0835734bc296862d","type":"server-state-changed","z":"7cd69c6d9b705517","name":"Person Changes","server":"2934e1c9.95a54e","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"person","entityidfiltertype":"substring","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"5","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":100,"y":40,"wires":[["a4fca0bcec87be68"]]},{"id":"a4fca0bcec87be68","type":"subflow:a88bf4f353498b06","z":"7cd69c6d9b705517","name":"","x":300,"y":80,"wires":[[],[],["ae08c2891079d200"]]},{"id":"cc2489a95e6583ca","type":"api-call-service","z":"7cd69c6d9b705517","name":"Update People @ home Counter","server":"2934e1c9.95a54e","version":3,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"input_number.people_at_home","data":"msg.people.length","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":850,"y":120,"wires":[["dd32dca0d38d06bd"]]},{"id":"dd32dca0d38d06bd","type":"debug","z":"7cd69c6d9b705517","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1070,"y":120,"wires":[]},{"id":"6781ef0b7fc63bae","type":"inject","z":"7cd69c6d9b705517","name":"Update","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":90,"y":120,"wires":[["a4fca0bcec87be68"]]},{"id":"ae08c2891079d200","type":"change","z":"7cd69c6d9b705517","name":"","rules":[{"t":"set","p":"payload.data.value","pt":"msg","to":"people.length","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":120,"wires":[["cc2489a95e6583ca"]]},{"id":"845dc6634054b4d0","type":"comment","z":"7cd69c6d9b705517","name":"IMPORTANT! - ¡IMPORTANTE!","info":"It is very important to change the \"input_number\" entity by yours created in Configuration > Helpers > Add Helper.\nUse a helper of the number type with a maximum number bigger than all \"people\" entities you have, suggested step size is 1.\n\n--\n\nEs muy importante que cambies la entidad \"input_number\" por la que hayas creado tu en \"Configuración > Ayudantes > Añadir ayudante\".\nUsa un ayudante de tipo número con un número maximo mayor que todas las entidades de tipo \"person\" (personas) que tengas, se sugiere un tamaño de paso de 1.","x":840,"y":80,"wires":[]},{"id":"2934e1c9.95a54e","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Explicación

Esta automatización cuenta el número de personas que hay en casa y cambia el valor de una entidad creada de tipo "input_number" (número).

Notas del autor

Cualquier duda sobre la automatización o propuestas de mejoras son bienvenidas.