maxill1 / ialarm-mqtt

ialarm to mqtt bridge
MIT License
8 stars 3 forks source link

Help for creating sensors in Home Assistant from the topics #13

Closed Stinocon closed 3 years ago

Stinocon commented 3 years ago

I'm still here asking for help. I told myself that since I already have home assistant on Docker.... One more container wouldn't hurt me and here we are, I would then need help with creating the sensors in Home Assistant.

I have correctly configured the config.json, in fact from the shell of the container I see correctly the zones and also from the MQTT addon (but also with mqtt explorer) I see correctly the updated topics.

Now I would like to create sensors in Home Assistant based on MQTT messages that I receive... I suppose I have to create a sensor with mqtt platform, I tried that but it does not work, what am I doing wrong?

####Sensori antifurto da MQTT####

  - platform: mqtt
    name: "Batteria finestra cucina"
    state_topic: "ialarm/sensors/2/battery"
    availability_topic: "ialarm/alarm/availability"
    payload_available: "online"
    payload_not_available: "offline"

Thank you! :)

EDIT:

but I thought... That if the mqtt discovery is active in theory Home Assistant should automatically create the entities... Correct?

EDIT2:

Ok i've found the problem, i have this error now when i start the container:

Home assistant discovery disabled (empty config.hadiscovery)
Error: Connection refused: Not authorized
    at MqttClient._handleConnack (/usr/src/app/node_modules/mqtt/lib/client.js:1076:15)
    at MqttClient._handlePacket (/usr/src/app/node_modules/mqtt/lib/client.js:365:12)
    at work (/usr/src/app/node_modules/mqtt/lib/client.js:283:12)
    at Writable.writable._write (/usr/src/app/node_modules/mqtt/lib/client.js:294:5)
    at doWrite (/usr/src/app/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (/usr/src/app/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at Writable.write (/usr/src/app/node_modules/readable-stream/lib/_stream_writable.js:334:11)
    at Socket.ondata (_stream_readable.js:719:22)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:298:12) {
  code: 5
maxill1 commented 3 years ago

sorry i missed the help requests...

it sounds like you have a couple of problems related to configuration: 1) bad mqtt config (user, password or ip?): Error: Connection refused: Not authorized

2) it seems that your config.json is missing the "hadiscovery" property: Home assistant discovery disabled (empty config.hadiscovery)

paste this into your config.json (use the default config.json as example):

"hadiscovery": {
        "discoveryPrefix": "homeassistant",
        "topics": {
            "alarmConfig": "${discoveryPrefix}/alarm_control_panel/ialarm/config",
            "sensorConfig": "${discoveryPrefix}/binary_sensor/ialarm/${zoneId}/config",
            "eventsConfig": "${discoveryPrefix}/sensor/ialarm/events/config",
            "bypassConfig": "${discoveryPrefix}/switch/ialarm/${zoneId}/config"
        },
        "code": "",
        "zoneName": "Zona",
        "zones": {
            "problem": {
                "zone bypass": "safety",
                "zone fault": "problem",
                "wireless detector low battery": "battery",
                "wireless detector loss": "connectivity"
            },
            "default": {
                "device_class": "safety"
            },
            "perimetrale": {
                "device_class": "motion"
            },
            "ritardata": {
                "device_class": "door"
            },
            "interna": {
                "device_class": "moving"
            },
            "emergenza": {
                "device_class": "safety"
            },
            "24 ore": {
                "device_class": "gas"
            },
            "incendio": {
                "device_class": "smoke"
            },
            "chiavi": {
                "device_class": "lock"
            }
        },
        "events": {
            "name": "iAlarm last event",
            "icon": "mdi:message-alert"
        },
        "bypass": {
            "name": "Bypass",
            "icon": "mdi:lock-open"
        }
    },

I also suggest you to use MQTTExplorer to see if your broker is receiving stuff: http://mqtt-explorer.com/

Stinocon commented 3 years ago

Hi and thanks for the reply! Actually then I managed to solve part of the problem, in fact I forgot to replace the admin of the mqtt configuration part with my admin.

Now I see correctly everything I need inside MQTTExplorer.

The problem that remains is the discovery one. Even reinserting the config part you posted me the problem remains. I paste all my configuration with the sensitive data omitted... Maybe you notice something wrong that I missed.

{
    "verbose": false,
    "server": {
        "host": "x.x.x.x",
        "port": "80",
        "username": "xxxxx",
        "password": "xxxxxxxxxxxxxxxx",
        "zones": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,33],
        "polling": {
            "status": 30000,
            "events": 10000
        }
    },
    "mqtt": {
        "host": "xxxxxxxxx",
        "port": "1883",
        "username": "xxxxxxxx",
        "password": "xxxxxxxxxxxx",
        "clientId": "ialarm-mqtt",
        "cache": "5m",
        "retain": true
    },
    "topics": {
        "availability": "ialarm/alarm/availability",
        "error": "ialarm/alarm/error",
        "alarm_qos": 2,
        "alarm": {
            "state": "ialarm/alarm/state",
            "command": "ialarm/alarm/set",
            "event": "ialarm/alarm/event",
            "discovery": "ialarm/alarm/discovery",
            "resetCache": "ialarm/alarm/resetcache",
            "bypass": "ialarm/alarm/zone/${zoneId}/bypass"
        },
        "sensors": {
            "topicType": "",
            "state": "ialarm/sensors/state",
            "sensors_qos": 1,
            "zone": {
                "alarm": "ialarm/sensors/${zoneId}/alarm",
                "active": "ialarm/sensors/${zoneId}/active",
                "lowBattery": "ialarm/sensors/${zoneId}/battery",
                "fault": "ialarm/sensors/${zoneId}/fault"
            }
        }
    },
    "payloads": {
        "alarmAvailable": "online",
        "alarmNotvailable": "offline",
        "alarmDecoder": {
            "armAway": [
                "armAway",
                "armedAway",
                "armed_away",
                "arm_away",
                "AA",
                "AwayArm"
            ],
            "armHome": [
                "armHome",
                "armedHome",
                "armed_home",
                "arm_home",
                "SA",
                "HomeArm",
                "StayArm",
                "armStay",
                "armedStay",
                "arm_stay",
                "armed_stay"
            ],
            "disarm": [
                "disarm",
                "disarmed",
                "D"
            ],
            "cancel": [
                "cancel",
                "canceled",
                "C"
            ],
            "triggered": [
                "trigger",
                "triggered",
                "T"
            ]
        },
        "alarm": {
            "armAway": "armed_away",
            "armHome": "armed_home",
            "disarm": "disarmed",
            "cancel": "cancel",
            "triggered": "triggered"
        },
        "sensorOn": "On",
        "sensorOff": "Off"
    },

    "hadiscovery": {
        "discoveryPrefix": "homeassistant",
        "topics": {
            "alarmConfig": "${discoveryPrefix}/alarm_control_panel/ialarm/config",
            "sensorConfig": "${discoveryPrefix}/binary_sensor/ialarm/${zoneId}/config",
            "eventsConfig": "${discoveryPrefix}/sensor/ialarm/events/config",
            "bypassConfig": "${discoveryPrefix}/switch/ialarm/${zoneId}/config"
        },
        "code": "",
        "zoneName": "Zona",
        "zones": {
            "problem": {
                "zone bypass": "safety",
                "zone fault": "problem",
                "wireless detector low battery": "battery",
                "wireless detector loss": "connectivity"
            },
            "default": {
                "device_class": "safety"
            },
            "perimetrale": {
                "device_class": "motion"
            },
            "ritardata": {
                "device_class": "door"
            },
            "interna": {
                "device_class": "moving"
            },
            "emergenza": {
                "device_class": "safety"
            },
            "24 ore": {
                "device_class": "gas"
            },
            "incendio": {
                "device_class": "smoke"
            },
            "chiavi": {
                "device_class": "lock"
            }
        },
        "events": {
            "name": "iAlarm last event",
            "icon": "mdi:message-alert"
        },
        "bypass": {
            "name": "Bypass",
            "icon": "mdi:lock-open"
        }
    },
    "zones": {
        "14": {
            "device_class": "smoke"
        }
    }
}
maxill1 commented 3 years ago

The error "Home assistant discovery disabled (empty config.hadiscovery)" is still there in the logs?

Sorry for the question, but I have to answer to exclude some possible causes: did you restarted ialarm-mqtt after editing config.json?

In MQTT Explorer, do you see a topics that end with "config" like the followings?

Did you change the discovery prefix in home assistant? (default is homeassistant, just like hadiscovery.discoveryPrefix property in config.json )

When ialarm-mqtt sends the discovery messages you should notice some activity in your docker container logs, that looks like this:

sending topic 'homeassistant/switch/ialarm/1/config' : Object with 11 keys
sending topic 'homeassistant/binary_sensor/ialarm/2/config' : Object with 13 keys
sending topic 'homeassistant/switch/ialarm/2/config' : Object with 11 keys
sending topic 'homeassistant/binary_sensor/ialarm/3/config' : Object with 13 keys
sending topic 'homeassistant/switch/ialarm/3/config' : Object with 11 keys
sending topic 'homeassistant/binary_sensor/ialarm/4/config' : Object with 13 keys
sending topic 'homeassistant/switch/ialarm/4/config' : Object with 11 keys
sending topic 'homeassistant/alarm_control_panel/ialarm/config' : Object with 13 keys
sending topic 'homeassistant/sensor/ialarm/events/config' : Object with 7 keys

If needed you can force the sending of new auto discovery topics via mqtt by posting the topic "ialarm/alarm/discovery" with payload "true":

image

Stinocon commented 3 years ago

The error "Home assistant discovery disabled (empty config.hadiscovery)" is still in the container log and yes, after every configuration change I do a docker restart ialarm-mqtt.

The discovery prefix is the default of home assistant, so much so that with other MQTT objects (shelly for example) discovery worked.

In MQTT Explorer I don't see any topic ending with /config actually.

I must say that setting ialarm/alarm/discovery to true through MQTT-Explorer now I see the entities in Home Assistant! Do you think it's enough to have sent it just this once? Or is discovery essential for managing states and stuff? Otherwise I have to remember that if I go to insert a new sensor I have to go and send a manual discovery.

EDIT:

It seems that even after a container restart discovery remains true... But the devices disappear from Home Assistant....

maxill1 commented 3 years ago

The disappearing of entites from home assistant at every restart is not intended.

I don't understand why your instance needed to force update autodiscovery to send messages and "empty config.hadiscovery" makes me think that the config file may be wrong in some part or that your container searches the wrong path.

Can you show me the docker command or compose you use to run the container (also the volume binds)?

I suggest you to start from scratch, copy/paste the original config.json than edit ONLY this two part, and restart the container.

"server": {
        "host": "x.x.x.x",
        "port": "80",
        "username": "xxxxx",
        "password": "xxxxxxxxxxxxxxxx",
        "zones": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,33],
        "polling": {
            "status": 30000,
            "events": 10000
        }
    },
    "mqtt": {
        "host": "xxxxxxxxx",
        "port": "1883",
        "username": "xxxxxxxx",
        "password": "xxxxxxxxxxxx",
        "clientId": "ialarm-mqtt",
        "cache": "5m",
        "retain": true
    },
Stinocon commented 3 years ago

Of course! First of all I created a folder in /opt/ so my working path is /opt/ialarm/.

Inside this folder I have then inserted my config.json file

This is the command that I launch to create the container

docker run --name ialarm-mqtt --restart always -v /opt/ialarm/:/config maxill1/ialarm-mqtt:latest

I'm not really a docker master but I think the core is -v /opt/ialarm/:/config which says "get the configuration inside /opt/ialarm/ and expose it in the container inside /config/

I also tried recreating the container but nothing changes. To recreate it I did

docker stop ialarm-mqtt
docker rm ialarm-mqtt
maxill1 commented 3 years ago

It all seems correct to me.

This won't be the problem, but I would remove the slash from /opt/ialarm/:/config to /opt/ialarm:/config

docker run --name ialarm-mqtt --restart always -v /opt/ialarm:/config maxill1/ialarm-mqtt:latest

I did not catch if you tried to copy/paste the original config.json from scratch or not.

Stinocon commented 3 years ago

Yes, I confirm that even completely recopying the config.json file from the repository and changing only the credentials the problem persists.

Do I need to put something else in my /opt/ialarm/ folder maybe?

maxill1 commented 3 years ago

no no the application read the config.json and then fetch the web panel of the alarm, it doesn't need anything else.

This is what ialarm-mqtt does on start/restart: 1) reset ALL /config by sending an empty payload (this removes all entities from HA) 2) fetch the web panel of ialarm/meian (whatever) and gets all the "zones" you configured 3) push all /config topics with the configured zones 4) HA gets the /config and creates/recreates the entities

I understand that right now you can find the /config on MQTT explorer upon restart, right (i mean number 1-3 on the list)? What is not working is HA not finding them (i mean number 4 on the list)?

Stinocon commented 3 years ago

Mmmm no wait, maybe I misspoke.

Every time the container is restarted this presents me the error that says discovery is disabled and in fact I don't see the entities in Home Assistant. However, if I launch the command ialarm/alarm/discovery from MQTT-Explorer and set it to true, a new entry appears in MQTT-Explorer and actually the entities in Home Assistant are created.

immagine This is MQTT-Explorer without the discovery command

immagine This is MQTT-Explorer with the discovery command

What happens then is that if I restart the container for some reason the discovery entry disappears again and the entities disappear also from Home Assistant.

maxill1 commented 3 years ago

Finally I found some time to direcly check your config.json...hadiscovery value is missing a property: "enabled": true!!

My fault, that property is missing also in default config.json...

Will fix soon.

In the while pull the new latest:

docker pull maxill1/ialarm-mqtt:latest && docker run --name ialarm-mqtt --restart always -v /opt/ialarm:/config maxill1/ialarm-mqtt:latest

or just add:

"hadiscovery": {
    "enabled" : true
   [...other properties]
}
Stinocon commented 3 years ago

Confirmed! Now it works even restarting the container or even the VM!

Thank you very much!

I'm asking a question that doesn't have to do with this issue but maybe you can point me in the right direction? One of the reasons why I want to talk in MQTT with the alarm is the possibility to know the status of the batteries (obviously not the % because I think it is impossible but at least if they need to be changed). Do you think it is possible to create a sensor in Home Assistant for each sensor of the alarm to retrive and know the state of batteries?

Other thing... Occasionally before leaving the house you can't turn on the alarm because probably a sensor is not communicating correctly its status. Obviously on the central alarm I can see the number of the sensor but now that I understand what it is I have to open the app, search for the number, see the name and operate accordingly. Do you think it is possible to get this info? Maybe it's already integrated in the sensor.ialarm_last_event but I ask just in case.

Thanks a lot!

maxill1 commented 3 years ago

The answer for both the question is: yes you can.

I confirm you that this kind of devices only report boolean for low battery, not percentage, anyway you can use "sensor.ialarm_last_event" to check the last event stored in the alarm (the messages you can find under http://192.168.1.x/SystemLog.htm), and if a battery goes down you will surely find it on that sensors a moment after the alarm is finding out, or you can check a property of a "binary_sensor" to do something in an automation. It's up to your imagination.

To share some of this things i made a new wiki page, go and checkout if some of the example may suit your case.

https://github.com/maxill1/ialarm-mqtt/wiki/Home-Assistant-Integration

I'm closing this issue, thank you for the support in finding the "enabled" bug.

P.S. If anyone wants to contribute to the wiki with some of his experience or configuration in HA, it's welcome.